How to implement a JMS PTP application?

How to implement a JMS PTP application?

Problem Statement:

A Computer Company / Shop deals with a Repair shop where the computer laptops are sent for repairing.  The computer company needs an application through which it can get a repair status update from the repair shop. Based on this they can arrange for a delivery collection of the computer products.


JMS Point to Point Model Example
JMS Point to Point Model Example

One of the possible resolution is to create a JMS application and use the Point to Point (PTP) model. In this case the Repair Shop vendor is the sender and the Computer company is the receiver. The sender sends the repair status in the message from time to time and the Queues gets updated with the messages. The receiver on the other hand gets the status updates by reading the queue.


To implement the above solution we will be using Weblogic Application Server to implement the JMS application. First item we will need to create the JMS administered objects like the connection factory and queues through the Weblogic admin console. Please refer previous Idiotechie article which gives a detailed step by step guide on How to create and configure JMS administered objects.
The following diagram gives high level steps for creating a JMS application.

JMS API for Point to Point Model
JMS API for Point to Point Model

The WebLogic Server JNDI Service Provider Interface provides an InitialContext implementation that allows remote Java clients to connect to WebLogic Server. The Java clients must get an object reference for a remote object and invoke operations on the object. This can happen by first creating the environment properties. The properties can be set by using hash tables and put() methods which creates a name value pair.

properties.put(Context.PROVIDER_URL, url);

The properties ‘context.provider_url’ specifies the URL of the Weblogic server that provides the name service. In this case we are using the default URL:


The ‘INITIAL_CONTEXT_FACTORY’ should always be set to the “weblogic.jndi.WLInitialContextFactory”. This setting identifies the factory that actually creates the Context.

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

Step 1: Look up a connection factory in the JNDI namespace
Once the context is created using the getInitialContext() method lookup to obtain the connection factory object by passing the name of the connection factory name. The QCF_NAME argument specifies the connection factory name defined during configuration.

(QueueConnectionFactory) ctx.lookup(QCF_NAME);

Step 2: Create a connection using the connection factory
This can be obtained by calling the createQueueConnection() method and it returns the QueueConnection object.


Step 3: Create a Queue Session

QueueConnection class instance. The acknowledge mode confirms that the messages are acknowledged either when the session has successfully  returned from a call to receive or when the message listener the session has called to process the message successfully returns.

qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

Step 4: Lookup a destination (Queue)
Since the destination has already been configured through the weblogic admin console the JMS client can now look up in the JNDI namespace for the destination name (i.e. the Queue name). This will return the Queue name.

(Queue) ctx.lookup(queueName);

Step 5: Create the message sender using the QueueSession and Queue instances
We can create message sender by passing the Queue reference to the Session methods.


Step 6: Create the message object
The session object is used to create the message object. In this example we are creating a Text message using createTextMessage() method. There are other methods like createBytesMessage() or createMapMessage() to create Byte messages, map messages etc.


The last part of this sequence is to start the connection using the start().


The receiver part of the application works in the similar way as shown in the diagram with few exceptions.
a)Instead of using createSender() as in Sender the Receiver class uses createReceiver() to create a reference to the queue receiver.
b)The receiver part needs to implement the javax.jms.MessageListener interface to receive messages asynchronously.  The client receiver class needs to implement the onMessage() method of the MessageListener class. The onMessage() reads the message which is received at the Queue receiver end. The message checks if the message is of type Text Message and then referenced the text in the msgText variable.
c)Register the queue receiver message listener using setMessageListener() method.

In both the Sender and Receiver classes we need to make sure to explicitly close all the costly resources like connection, sessions etc.
Well that’s it for developing the sender and receiver. That path is all clear for execution. We have created a batch file which automates the whole process of sending and receiving. Alternatively in case of Unix environments we can create separate scripts to automate. There are two scripts “ExecuteJMSSender.bat” which triggers the RepairShopSender class and sends the message and “ExecuteJMSReceiver.bat” for triggering the receiver class and initiates the receiving part of the JMS client application. Both these batch file needs to be modified to customize according to the user’s server configuration and path. The details of what needs to be done are mentioned inside batch file code comments. One of the key things we would like to highlight is about setting the environment when we are using the weblogic server. This can easily be done inside the batch file by calling the setDomainEnv.cmd.
Before we start the execution we will need to start the Weblogic Application server (JMS server) and go to the weblogic server admin console. 

(i) Go to Services -> Messaging -> JMS Modules

Weblogic Admin Console
Weblogic Admin Console

(ii) Click the JMS System Module -> Click the Queue resource -> Go to the Monitoring Tab

(To know how to create the System modules, JMS resources please refer the following article: Create and Configure JMS administered objects).

Queue without messages
Queue without messages

The above screenshot shows that currently there are no messages in the queue.

(iii)Now run the ExecuteJMSSender.bat file.

Please type ‘quit’ for exiting sending message to the queue.

Execute JMS Sender
Execute JMS Sender

(iv)If you now go to the Weblogic Admin console to monitor the Queue, there will be three messages present in the queue.

Messages in Queue
Messages in Queue

(v) Now to execute the JMS client receiver class start the “ExecuteJMSReceiver.bat” file.

This class will display the sequence of the messages being received from the Queue. 

Execute JMS Receiver
Execute JMS Receiver

(vi) If we go back to the weblogic admin console we can see that there are no current messages and that all the messages have been consumed.

Messages consumed by receiver
Messages consumed by receiver

The code for this JMS Application including the batch files can be found attached along with this article. That’s it for today. Please feel free to post your comments/ queries or feedback. I will be happy to answer your queries.

<<Download Sample Code>>

Share This Post

Post Comment