Simple JMS Client Application

The JMS client applications are simple and standalone programs that run outside the server as class files. For the JMS application client ,let’s create three different basic tasks. They are,

  1. Creating connection and a session
  2. Creating message producers and consumers
  3. Sending and receiving messages

Here we are using J2EE platform for the working of JMS application. The following steps describe the working of JMS application.

  1. Setting your environment to run J2EE clients and applications
  2. A point-to-point example that uses synchronous receives
  3. A publish/subscribe example that uses a message listener
  4. Running JMS client programs on multiple systems

Each step contains two programs. That is it includes one sends messages and one receive messages.

1)      Setting your environment to run J2EE clients and applications, first you must set the environment variable. For windows we are using the following environment variable.

Variable name                                                              Values

A)    %JAVA_HOME%            Directory in which the JavaTM 2 SDK, Standard Edition, version 1.3.1, is installed

B)    %J2EE_HOME%             Directory in which the J2EE SDK 1.3.1 is installed, usually C:\j2sdkee1.3.1

C)    %CLASSPATH%             Include the following .;%J2EE_HOME%\lib\j2ee.jar;%J2EE_HOME%\lib\locale

D)    %PATH%                     Include %J2EE_HOME%\bin

For UNIX we are using the following variables.

Variable Name                                                         Values

A)    $JAVA_HOME                  Directory in which the Java 2 SDK, Standard Edition, version 1.3.1, is installed

B)    $J2EE_HOME                  Directory in which the J2EE SDK 1.3.1 is installed, usually $HOME/j2sdkee1.3.1

C)    $CLASSPATH                  Include the following .:$J2EE_HOME/lib/j2ee.jar:$J2EE_HOME/lib/locale

D)    $PATH                       Include $J2EE_HOME/bin

2)      A point-to-point example that uses synchronous receives

Here we are uses the receive method to consume messages synchronously. We are using the following steps for perform the PTP example.

2.1)   Writing the PTP client programs

SimpleQueueSender.java sending program performs the following steps.

  1. Performs a Java Naming and Directory InterfaceTM (JNDI) API lookup of the QueueConnectionFactory and queue
  2. Creates a connection and a session
  3. Creates a QueueSender
  4. Creates a TextMessage
  5. Sends one or more messages to the queue
  6. Sends a control message to indicate the end of the message stream
  7. Closes the connection in a finally block, automatically closing the session and QueueSender

SimpleQueueReceiver.java is receiving program and perform the following operations.

  1. Performs a JNDI API lookup of the QueueConnectionFactory and queue
  2. Creates a connection and a session
  3. Creates a QueueReceiver
  4. Starts the connection, causing message delivery to begin
  5. Receives the messages sent to the queue until the end-of-message-stream control message is received
  6. Closes the connection in a finally block, automatically closing the session and QueueReceiver

In the receive method we never define no argument or an argument 0, the method blocks infinitely until a message arrioves.

Eg:-

Message m = queueReceiver.receive();
Message m = queueReceiver.receive(0);

If we want to use a time synchronous receive we are using the following codes.

Eg:-     Call the receive method with a timeout argument greater than 0:

Message m = queueReceiver.receive(1); // 1 millisecond

Call the receiveNoWait method, which receives a message only if one is available:

Message m = queueReceiver.receiveNoWait();

The SimpleQueueReceiver program uses an indefinite while loop to receive messages, calling receive with a timeout argument. Calling receiveNoWait would have the same effect.

2.2)   Compiling the PTP clients

For compiling the PTP we are using the following steps.

    1. Make sure that you have set the environment variables
    2. At a command line prompt, compile the two source files:
    3. javac SimpleQueueSender.java
    4. javac SimpleQueueReceiver.java

2.3)   Starting the JMS provider

This can be done in two ways.

    1. Use J2EE SDK 1.3.1, the JMS provider is the SDK.
    2. Through command line

J2ee –verbose

Wait until the server displays the message “J2EE server startup complete.

2.4)   Creating the JMS administered objects

For the compilation purpose we are using the j2eeadmin command to create a queue named MyQueue. The last argument specifies the kind of destination to create.

j2eeadmin -addJmsDestination MyQueue queue

“j2eeadmin –listJmsDestination” command helps to check the queue has been created or not.

This example uses the default QueueConnectionFactory object supplied with the J2EE SDK 1.3.1. With a different J2EE product, you might need to create a connection factory yourself.

2.5)   Running the PTP clients

Run the clients as follows.

1.         Run the SimpleQueueSender program, sending three messages. You need to define a value for jms.properties.

o          On a Microsoft Windows system, type the following command:

o          java -Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleQueueSender MyQueue 3

o          On a UNIX system, type the following command:

o          java -Djms.properties=$J2EE_HOME/config/jms_client.properties SimpleQueueSender MyQueue 3

The output of the program looks like this:

Queue name is MyQueue

Sending message: This is message 1

Sending message: This is message 2

Sending message: This is message 3

2.         In the same window, run the SimpleQueueReceiver program, specifying the queue name. The java commands look like this:

o          Microsoft Windows systems:

o          java -Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleQueueReceiver MyQueue

o          UNIX systems:

o          java -Djms.properties=$J2EE_HOME/config/jms_client.properties SimpleQueueReceiver MyQueue

The output of the program looks like this:

Queue name is MyQueue

Reading message: This is message 1

Reading message: This is message 2

Reading message: This is message 3

2.6)   Deleting the queue

You can delete the queue you created as follows:

j2eeadmin -removeJmsDestination MyQueue

3.         Now try running the programs in the opposite order. Start the SimpleQueueReceiver program. It displays the queue name and then appears to hang, waiting for messages.

4.         In a different terminal window, run the SimpleQueueSender program. When the messages have been sent, the SimpleQueueReceiver program receives them and exits.

A publish/subscribe example that uses a message listener

Here we are discussing how to publshing and subscribing programs in a pub/sub that heps to uses a message listner to consume message asynchronously. The following section describes how to compile and run the programs using J2EE SDK 1.3.1.

1)                  Writing the pub/sub client programs

The publishing program, SimpleTopicPublisher.java, performs the following steps:

  1. Performs a JNDI API lookup of the TopicConnectionFactory and topic
  2. Creates a connection and a session
  3. Creates a TopicPublisher
  4. Creates a TextMessage
  5. Publishes one or more messages to the topic
  6. Closes the connection, which automatically closes the session and TopicPublisher

The receiving program, SimpleTopicSubscriber.java, performs the following steps:

  1. Performs a JNDI API lookup of the TopicConnectionFactory and topic
  2. Creates a connection and a session
  3. Creates a TopicSubscriber
  4. Creates an instance of the TextListener class and registers it as the message listener for the TopicSubscriber
  5. Starts the connection, causing message delivery to begin
  6. Listens for the messages published to the topic, stopping when the user enters the character q or Q
  7. Closes the connection, which automatically closes the session and TopicSubscriber

The message listener, TextListener.java, follows these steps:

  1. When a message arrives, the onMessage method is called automatically.
  2. The onMessage method converts the incoming message to a TextMessage and displays its content

2)   Compiling the pub/sub clients

To compile the pub/sub example, do the following.

1.   Make sure that you have set the environment variables shown in Table 4.1.

2.   Compile the programs and the message listener class:

3.   javac SimpleTopicPublisher.java

4.   javac SimpleTopicSubscriber.java

5.   javac TextListener.java

3)    Starting the JMS provider

If you did not do so before, start the J2EE server in another terminal window:j2ee -verbose

Wait until the server displays the message “J2EE server startup complete.”

4)   Creating the JMS administered objects

For creating the default TopicConnectionFactory object we compiled the clients use the j2eeadmin command to create a topic named MyTopic.

Eg:-

 j2eeadmin -addJmsDestination MyTopic topic

To verify that the queue has been created, use the following command:

j2eeadmin -listJmsDestination

5)     Running the pub/sub clients

Run the clients as follows.

1.         Run the SimpleTopicSubscriber program, specifying the topic name. You need to define a value for jms.properties.

o          On a Microsoft Windows system, type the following command:

o          java -Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleTopicSubscriber MyTopic

o          On a UNIX system, type the following command:

o          java -Djms.properties=$J2EE_HOME/config/jms_client.properties SimpleTopicSubscriber MyTopic

The program displays the following lines and appears to hang:

Topic name is MyTopic

To end program, enter Q or q, then <return>

2.         In another terminal window, run the SimpleTopicPublisher program, publishing three messages. The java commands look like this:

o          Microsoft Windows systems:

o          java -Djms.properties=%J2EE_HOME%\config\jms_client.properties SimpleTopicPublisher MyTopic 3

o          UNIX systems:

o          java -Djms.properties=$J2EE_HOME/config/jms_client.properties SimpleTopicPublisher MyTopic 3

The output of the program looks like this:

Topic name is MyTopic

Publishing message: This is message 1

Publishing message: This is message 2

Publishing message: This is message 3

In the other window, the program displays the following:

Reading message: This is message 1

Reading message: This is message 2

Reading message: This is message 3

Enter Q or q to stop the program.

6)    Deleting the topic and stopping the server

    1. You can delete the topic you created as follows:
    2. j2eeadmin -removeJmsDestination MyTopic
    3. If you wish, you can stop the J2EE server as well:
    4. j2ee -stop
    5. Running JMS client programs on multiple systems

JMS client programs can communicate with each other when they are running on different systems in a network.

Eg:- We want to run the SimpleProducer program on one system, earth, and the SimpleSynchConsumer program on another system, jupiter.

  1. Create two new connection factories
  2. Edit the source code
  3. Recompile the source code and update the client JAR files

Before you begin, start the server on both systems:

  1. Start the Application Server on earth and log in to the Admin Console.
  2. Start the Application Server on jupiter and log in to the Admin Console.

Creating Administered Objects for Multiple Systems

To run these programs, you must do the following:

  1. Create a new connection factory on both earth and jupiter
  2. Create a destination resource and physical destination on both earth and jupiter

Create a new connection factory on jupiter as follows:

  1. In the Admin Console, expand the Resources node, then expand the JMS Resources node.
  2. Select the Connection Factories node.
  3. On the JMS Connection Factories page, click New. The Create JMS Connection Factory page appears.
  4. In the JNDI Name field, type jms/JupiterConnectionFactory.
  5. Choose javax.jms.ConnectionFactory from the Type combo box.
  6. Select the Enabled checkbox.
  7. Click OK.

Create a new connection factory with the same name on earth as follows:

  1. In the Admin Console, expand the Resources node, then expand the JMS Resources node.
  2. Select the Connection Factories node.
  3. On the JMS Connection Factories page, click New. The Create JMS Connection Factory page appears.
  4. In the JNDI Name field, type jms/JupiterConnectionFactory.
  5. Choose javax.jms.ConnectionFactory from the Type combo box.
  6. Select the Enabled checkbox.
  7. In the Additional Properties area, find the AddressList property. In the Value field, replace the name of your current system with the name of the remote system (whatever the real name of jupiter is), as follows: mq://sysname:7676/,If the JMS service on the remote system uses a port number other than the default (7676), change the port number also.
  8. ClicK OK.

If you have already been working on either earth or jupiter, you have the queue on one system. On the system that does not have the queue, perform the following steps:

1) Use the Admin Console to create a physical destination named PhysicalQueue, just as you did in Creating JMS Administered Objects.

2) Use the Admin Console to create a destination resource named jms/Queue and set its Name property to the value PhysicalQueue.