Hi,

Jay SenSharma

Jay SenSharma

Java Messaging Service is an API that provides reliable, asynchronous communication between applications in a distributed computing environment. It enables a Java applications that share a messaging system to exchange messages and Simplifies application development by providing a standard interface for creating, sending, and receiving messages.

To Know about JMS Topic Please refer to: Basic JMS Demo using WebLogic Topic

What are major components of JMS system?

A). JMSServer: JMS server is an environment-related configuration entity that acts as management container for JMS queue and topic resources defined within JMS modules that are targeted to specific that JMS server.
B). JMS Modules: JMS modules contain configuration resources, such as standalone queue and topic destinations, distributed destinations, and connection factories. In WebLogic Server these Configurations can be seen inside “<DOMAIN_HOME>/config/jms” Directory.
C). JMS Client: A JMS is a JMS applications that either produce messages to destinations or consume messages from destinations.
D). JNDI : Java Naming And Directory interface,It is a Java API for a directory service that allows Java software clients to discover and look up data and objects via a name.
E). Persistent Store: It can be a UserDefined Persistent Store or a Default Persistent store. It is used for storing the Data (Messages).

Here we are going to see how we can configure WebLogic JMS Server, JMS Modules (Queue/ConnectionFactory) how to Target them and then How to test it using a Simpel Java Programs.

Step1). Start your WebLogic Server an Login to the AdminConsole.

Step2). Create a JMS Server…

Creating JMS Server

Creating JMS Server

Creating JMS Server-2

Creating JMS Server-2

Selecting JMS Server Store Type

Selecting JMS Server Store Type

Configuring JMS Store

Configuring JMS Store

Choosing JMS Store for JMSSErver

Choosing JMS Store for JMSSErver

Targeting to the JMSServer

Targeting to the JMSServer

Activating the JMS Server Configuration

Activating the JMS Server Configuration

Step3). Configuring JMS Module:

Creating JMS Module

Creating JMS Module

Entering JMS Module Name

Entering JMS Module Name

Targetting JMS Module

Targetting JMS Module

Finishing the Module Creation

Finishing the Module Creation

Step4). Creating Connection Factory:

Creating JMS Connection Factory

Creating JMS Connection Factory

Selecting JMS Module to be Created

Selecting JMS Module to be Created

Providing Name/JNDIName to Connection Factory

Providing Name/JNDIName to Connection Factory

Targeting the CnnectionFactory

Targeting the CnnectionFactory

Step5).Creating JMS Queue:

Selecting JMSQueue to be Created

Selecting JMSQueue to be Created

Queue Selection

Queue Selection

Assigning Name/JNDIName to Queue

Assigning Name/JNDIName to Queue

Creating SubDeployment

Creating SubDeployment

Assigning Name to the SubDeployment

Assigning Name to the SubDeployment

Targeting the JMS Queue

Targeting the JMS Queue

Activating the Final Changes

Activating the Final Changes

Step6). Now Write a Simple Java Program to send the Messages to this JMS Queue…like following:

“QueueSend.java”


import java.io.*;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
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="QCF";
public final static String QUEUE="TestQ";

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 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
{
String line="Test Message Body with counter = ";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
boolean readFlag=true;
System.out.println("ntStart Sending Messages (Enter QUIT to Stop):n");
while(readFlag)
{
System.out.print("&lt;Msg_Sender&gt; ");
String msg=br.readLine();
if(msg.equals("QUIT") || msg.equals("quit"))
{
qs.send(msg);
System.exit(0);
}
qs.send(msg);
System.out.println();
}
br.close();
}

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

Step7). Now run the “setWLSEnv.cmd” and then compile and Run the QueueSend program.

java   QueueSend    t3://localhost:7001

Step8). Now Login to AdminConsole to check that the Message is arrived in the JMS Queue or Not?

home—> Services–> Messaging–> JMS Modules–> MySystemModule–>MyQueue–> Monitoring–> MySystemModule –> MyQueue (Check the CheckBox)–> Show Messages(Click This Button)

Monitoring JMS Queue Message In AdminConsole

Monitoring JMS Queue Message In AdminConsole

Step9). Now write the following “QueueReceive.java” program to read  JMS Messages from the JMS Queue.

“QueueReceive.java”

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="QCF";
public final static String QUEUE="TestQ";
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("nt&lt;Msg_Receiver&gt; "+ msgText );
if (msgText.equalsIgnoreCase("quit"))
{
synchronized(this)
{
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
}
catch (JMSException jmse)
{
jmse.printStackTrace();
}
}
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 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 from QueueSender.class).");
// Wait until a "quit" message has been received.
synchronized(qr)
{
while (! qr.quit)
{
try
{
qr.wait();
}
catch (InterruptedException ie)
{}
}
}
qr.close();
}

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

Step10). Now compile and run the QueueRecieve.java program to read the messages from WebLogic JMS  Queue.

java     QueueReceive    t3://localhost:7001

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.