Queue Archive

WebLogic JMS (Point to Point) feature using a Queue

The following article shows a simple usage of WebLogic JMS  Point to Point feature using a Queue.

JMS supports two messaging models: point-to-point (PTP) and publish/subscribe (pub/sub). The messaging models are very similar, except for the following differences:

  • PTP messaging model enables the delivery of a message to exactly one recipient.
  • Pub/sub messaging model enables the delivery of a message to multiple recipients.

The point-to-point (PTP) messaging model enables one application to send a message to another. PTP messaging applications send and receive messages using named Queues. A queue sender (producer) sends a message to a specific queue. A queue receiver (consumer) receives messages from a specific queue.

The following figure illustrates PTP messaging.

Steps to configure JMS Point to Point Feature (JMS  Queue)

1.  Configure JMS Server

a.  Login into the WebLogic Admin Console, navigate to Services –> Messaging –> JMS Servers.

JMS Server acts as a management containers for the queues and topics.

b. Create a JMS Server as below.

c. Target the JMS Server to any one of the WebLogic Servers.

2. Create JMS System Module to hold the Queues / Topics.

a. Navigate to Services  –> Messaging –> JMS Modules from the left panel.

b.  Target the JMS System Module to the server on which the JMS Server is targeted.

3. Create a Sub Deployment.

a. Click on the newly created JMS SystemModule and navigate to the SubDeployments tab

b. Target the Sub Deployment to the created JMS Server.

4.  Create JMS Connection Factory.

a. Under the configuration tab of the JMS SystemModule, click New to add resources like Connection Factories, Queues,Topics.

b. Create a JMS Connection Factory, specify a JNDI name.

c. Target the Connection Factory to the Sub Deployment.

5. Create a JMS Queue (Point to Point Messaging Model)

a. Create a Queue from the configuration tab of the JMS SystemModule.

b. Target the  Queue  to the Sub Deployment.

c . Navigate to the JMS Resources page and you would see the Connection Factory and the JMS Queue created.

6. Testing the setup

a. Open  command prompt, and set the class path.

Note: You can run the setDomainEnv script present under the  <Domain>/bin folder

b. Compile and execute the below, QueueSend.java program to send a message to the queue.

********************************************************

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Hashtable;

import javax.jms.*;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

public class QueueSend

{

public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

public final static String JMS_FACTORY="CF1";

public final static String QUEUE="Queue1";

private QueueConnectionFactory qconFactory;

private QueueConnection qcon;

private QueueSession qsession;

private QueueSender qsender;

private Queue queue;

private TextMessage msg;

public void init(Context ctx, String queueName)

throws NamingException, JMSException

{

qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);

qcon = qconFactory.createQueueConnection();

qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

queue = (Queue) ctx.lookup(queueName);

qsender = qsession.createSender(queue);

msg = qsession.createTextMessage();

qcon.start();

}

public void send(String message) throws JMSException {

msg.setText(message);

qsender.send(msg);

}

public void close() throws JMSException {

qsender.close();

qsession.close();

qcon.close();

}

public static void main(String[] args) throws Exception {

if (args.length != 1) {

System.out.println("Usage: java examples.jms.queue.QueueSend WebLogicURL");

return;

}

InitialContext ic = getInitialContext(args[0]);

QueueSend qs = new QueueSend();

qs.init(ic, QUEUE);

readAndSend(qs);

qs.close();

}

private static void readAndSend(QueueSend qs)     throws IOException, JMSException

{

BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));

String line=null;

boolean quitNow = false;

do {

System.out.print("Enter message (\"quit\" to quit): \n");

line = msgStream.readLine();

if (line != null && line.trim().length() != 0) {

qs.send(line);

System.out.println("JMS Message Sent: "+line+"\n");

quitNow = line.equalsIgnoreCase("quit");

}

} while (! quitNow);

}

private static InitialContext getInitialContext(String url)

throws NamingException

{

Hashtable<String,String> env = new Hashtable<String,String>();

env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

env.put(Context.PROVIDER_URL, url);

return new InitialContext(env);

}

}

 

********************************************************

java QueueSend t3://localhost:7001

c.  Compile and execute the below QueueReceive.java program to retrieve the message from the queue.

***********************************************************

 

import java.util.Hashtable;

import javax.jms.*;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

public class QueueReceive implements MessageListener

{

public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

public final static String JMS_FACTORY="CF1";

public final static String QUEUE="Queue1";

private QueueConnectionFactory qconFactory;

private QueueConnection qcon;

private QueueSession qsession;

private QueueReceiver qreceiver;

private Queue queue;

private boolean quit = false;

public void onMessage(Message msg)

{

try {

String msgText;

if (msg instanceof TextMessage) {

msgText = ((TextMessage)msg).getText();

} else {

msgText = msg.toString();

}

System.out.println("Message Received: "+ msgText );

if (msgText.equalsIgnoreCase("quit")) {

synchronized(this) {

quit = true;

this.notifyAll(); // Notify main thread to quit

}

}

} catch (JMSException jmse) {

System.err.println("An exception occurred: "+jmse.getMessage());

}

}

public void init(Context ctx, String queueName)

throws NamingException, JMSException

{

qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);

qcon = qconFactory.createQueueConnection();

qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

queue = (Queue) ctx.lookup(queueName);

qreceiver = qsession.createReceiver(queue);

qreceiver.setMessageListener(this);

qcon.start();

}

public void close()throws JMSException

{

qreceiver.close();

qsession.close();

qcon.close();

}

public static void main(String[] args) throws Exception {

if (args.length != 1) {

System.out.println("Usage: java examples.jms.queue.QueueReceive WebLogicURL");

return;

}

InitialContext ic = getInitialContext(args[0]);

QueueReceive qr = new QueueReceive();

qr.init(ic, QUEUE);

System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");

synchronized(qr) {

while (! qr.quit) {

try {

qr.wait();

} catch (InterruptedException ie) {}

}

}

qr.close();

}

private static InitialContext getInitialContext(String url)

throws NamingException

{

Hashtable<String,String> env = new Hashtable<String,String>();

env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

env.put(Context.PROVIDER_URL, url);

return new InitialContext(env);

}}

 

***********************************************************

java QueueReceive t3://localhost:7001

Further Reading :

http://download.oracle.com/docs/cd/E12840_01/wls/docs103/jms/fund.html#wp1071729

Cheers,

Wonders Team. 🙂

JBoss JMS Queue configuration

Configuring JMS providers in JBoss:-
********************************
The below example demonstrates a JMS example using Queue Destination (Point – To – Point messaging feature) using the JBoss default Queue.

JBoss defines default JMS queue and Topic Destination defined in $JBOSS_HOME/server/<server-profile>/deploy/messaging/ destinations-service.xml.

You can add/remove destinations to this file, or deploy another *-service.xml descriptor with the destination configurations.

Below are the steps to setup JMS Queues in JBoss:-

1:- Start the JBoss server, select the server type which provides messaging features like all or default

Run.bat –c all


2:- Set the classpath to jbossall-client.jar which is located in the client folder.

set classpath=%CLASSPATH%;F:\jboss-5.1.0.GA\client\jbossall-client.jar

3:- Compile the below QueueSend.java  program.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class QueueSend
{

public final static String JNDI_FACTORY="org.jboss.naming.HttpNamingContextFactory";
public final static String JMS_FACTORY="ConnectionFactory";
public final static String QUEUE="/queue/DLQ";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;

public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}

public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}

public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueSend JBossURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}

private static void readAndSend(QueueSend qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): \n");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow);

}

private static InitialContext getInitialContext(String url)
throws NamingException
{

Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.setProperty(Context.PROVIDER_URL,url);
return new InitialContext(env);
} }

4:- Run the QueueSend.java program as below.
java QueueSend http://127.0.0.1:8080/invoker/JNDIFactory

output:-
*********
Enter message (“quit” to quit):
test
JMS Message Sent: test

5: Compile and execute the below QueueRecieve.java program similar to QueueSend.java.

 

import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class QueueReceive implements MessageListener

{
public final static String JNDI_FACTORY="org.jboss.naming.HttpNamingContextFactory";

public final static String JMS_FACTORY="ConnectionFactory";

public final static String QUEUE="/queue/DLQ";

private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false;

public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
}
System.out.println("Message Received: "+ msgText );
if (msgText.equalsIgnoreCase("quit")) {
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
System.err.println("An exception occurred: "+jmse.getMessage());
}
}

public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
}

public void close()throws JMSException
{
qreceiver.close();
qsession.close();
qcon.close();
}

public static void main(String[] args) throws Exception {

if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueReceive JBossURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueReceive qr = new QueueReceive();
qr.init(ic, QUEUE);

System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");

synchronized(qr) {
while (! qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {}
}
}
qr.close();
}

private static InitialContext getInitialContext(String url)
throws NamingException
{
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,JNDI_FACTORY);
env.setProperty(Context.PROVIDER_URL,url);
return new InitialContext(env);
}

}

 

6. Run the QueueRecieve.java as below.

java QueueReceive http://127.0.0.1:8080/invoker/JNDIFactory


JMS Ready To Receive Messages (To quit, send a “quit” message).
Message Received: test

References:-
http://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/JMS_Examples-A_Point_To_Point_Example.html

NOTE: Most common errors while looking up to JBoss JNDI.
*************************************************

Exception in thread “main” javax.naming.NoInitialContextException:
Cannot instantiate class: org.jboss.naming.HttpNamingContextFactory
[Root exception is java.lang.ClassNotFoundException: org.jboss.naming.HttpNamingContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.(InitialContext.java:197)
at QueueSend.getInitialContext(QueueSend.java:122)
at QueueSend.main(QueueSend.java:90)

Caused by: java.lang.ClassNotFoundException: org.jboss.naming.HttpNamingContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

Solution:-
The problem is with the classpath setting and the classloader is not able to load the class “org.jboss.naming.HttpNamingContextFactory” class.

Set the classpath to point to jbossall-client.jar which would be under $JBOSS_HOME\client\jbossall-client.jar

set classpath=%classpath%;$JBOSS_HOME\client\jbossall-client.jar;

Else, you can run your java applications like

java -cp “$JBOSS_HOME\client\jbossall-client.jar”  QueueReceive

Cheers,

Wonders Team. 🙂