Monday, 16 April 2018

How to Create a Simple JMS Queue in Weblogic Server 11g

How to Create a Simple JMS Queue in Weblogic Server 11g

This example shows the steps to create a simple JMS queue in webLogic Server 11g for testing purposes. For example, to use with the two sample programs QueueSend.java and QueueReceive.java which will be shown at the end of the article



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.
1. Introduction and Definitions

A JMS queue in Weblogic Server is associated with a number of additional resources:


JMS Server

A JMS server acts as a management container for resources within JMS
modules. Some of its responsibilities include the maintenance of
persistence and state of messages and subscribers. A JMS server is
required in order to create a JMS module.

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.


JMS Module

A JMS module is a definition which contains JMS resources such as
queues and topics. A JMS module is required in order to create a JMS
queue.

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 “/config/jms” Directory.

JMS Client:


A JMS is a JMS applications that either produce messages to destinations or consume messages from destinations.

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.

Persistent Store:

It can be a UserDefined Persistent Store or a Default Persistent store. It is used for storing the Data (Messages).


Subdeployment:

JMS modules are targeted to one or more WLS instances or a cluster.
Resources within a JMS module, such as queues and topics are also
targeted to a JMS server or WLS server instances. A subdeployment is a
grouping of targets. It is also known as advanced targeting.

Connection Factory

A connection factory is a resource that enables JMS clients to
create connections to JMS destinations.

JMS Queue

A JMS queue (as opposed to a JMS topic) is a point-to-point
destination type. A message is written to a specific queue or
received from a specific queue.

The objects used in this example are:




Object Name
Type
JNDI Name
TestJMSServer
JMS Server
TestJMSModule
JMS Module
TestSubDeployment
Subdeployment
TestConnectionFactory
Connection Factory
jms/TestConnectionFactory
TestJMSQueue
JMS Queue
jms/TestJMSQueue



2. Configuration Steps

The following steps are done in the WebLogic Server Console, Start your WebLogic Server an Login to the AdminConsole.
beginning with the left-hand navigation menu.

2.1 Create a JMS Server

Services > Messaging > JMS Servers




Select New
Name: TestJMSServer
Persistent Store: (none)
Target: soa_server1 (or choose an available server)
Finish

The JMS server should now be visible in the list with Health OK.



2.2 Create a JMS Module

Services > Messaging > JMS Modules
Select New
Name: TestJMSModule
Leave the other options empty
Targets: soa_server1 (or choose the same one as the JMS server)
Press Next
Leave “Would you like to add resources to this JMS system
module” unchecked and press Finish.

2.3 Create a SubDeployment

A subdeployment is not necessary for the JMS queue to work, but it
allows you to easily target subcomponents of the JMS module to a
single target or group of targets. We will use the sub-deployment in
this example to target the following connection factory and JMS
queue to the JMS server we created earlier.

Services > Messaging > JMS Modules
Select TestJMSModule
Select the Subdeployments tab and New
Subdeployment Name: TestSubdeployment
Press Next

Here you can select the target(s) for the subdeployment. You can
choose either Servers (i.e. WebLogic managed servers, such as the
soa_server1 or JMS Servers such as the JMS Server created
earlier. As the purpose of our subdeployment in this example is to
target a specific JMS server, we will choose the JMS Server option.
Select theTestJMSServer created earlier

Press Finish

2.4 Create a Connection Factory

Services > Messaging > JMS Modules
Select TestJMSModule and press New
Select Connection Factory and Next
Name: TestConnectionFactory
JNDI Name: jms/TestConnectionFactory
Leave the other values at default
On the Targets page, select the Advanced Targeting button and select TestSubdeployment

Press Finish

The connection factory should be listed on the following page with
TestSubdeployment and TestJMSServer as the target.

2.5 Create a JMS Queue

Services > Messaging > JMS Modules
Select TestJMSModule and press New
Select Queue and Next
Name: TestJMSQueue
JNDI Name: jms/TestJMSQueue
Template:None
Press Next
Subdeployments: TestSubdeployment
Finish

The TestJMSQueue should be listed on the following page with
TestSubdeployment and TestJMSServer.

Confirm the resources for the TestJMSModule. 
Using the Domain Structure tree, navigate to 
soa_domain > Services > Messaging > JMS Modules then select TestJMSModule




You should see the following resources



The JMS queue is now complete and can be accessed using the JNDI
names

jms/TestConnectionFactory and
jms/TestJMSQueue.


3. Testing the setup


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("<Msg_Sender> ");
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);
}
}
Now run the “setWLSEnv.cmd” and then compile and Run the QueueSend program.

java QueueSend t3://localhost:7001

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

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


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<Msg_Receiver> "+ 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);
}
}


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

java QueueReceive t3://localhost:7001

No comments:

Post a Comment

All about WebLogic t3 and t3s Protocol

WebLogic's  implementation of the RMI specification uses a proprietary protocol known as T3. You can think of T3 (and secure T3S) as a l...