Understanding the Sample Classes
If you have written a JMS application, the sample classes JNDIUtil and Tester (
click here to download Java source files and the compiled classes) will be easy to understand. The JMS-administered objects you have created hide any proprietary vendor implementation.
JNDIUtil Class
JNDIUtil includes methods to retrieve objects by name through JNDI lookup (see
Listing 1). You will use methods in this class to retrieve references to the JMS objects you have already defined using MQ Explorer.
Tester Class
The Tester class writes a message to
OUT.QUEUE and reads a message from
IN.QUEUE (see
Listing 2).
The starting point is a connection factory lookup. The factory is used to create a connection:
factory= jndiUtil.getQueueConnectionFactory("TestQM_QCF");
connection = factory.createQueueConnection();
The connection object is used to create a session:
session = connection.createQueueSession( transacted, Session.AUTO_ACKNOWLEDGE);
To write messages to the IN.QUEUE queue, you look up the destination object created (OutputTestQueue):
oQueue= jndiUtil.getQueue("OutputTestQueue");
Finally, you create a QueueSender object to write messages to the queue:
queueSender = session.createSender(oQueue);
TextMessage oMsg = session.createTextMessage();
oMsg.setText("www.devx.com");
queueSender.send(oMsg);
The same procedure applies to reading messages from OUT.QUEUE, but you use a QueueReceiver.
Compiling and Running the Sample Classes
The JAR files required to compile and run the sample classes are automatically added to the
CLASSPATH environment variable when you install WMQ. The needed JARs are located in
C:\Program Files\IBM\WebSphere MQ\Java\lib, including the JARs you need for JMS and JNDI.
Before you run the Tester class, add a test message to IN.QUEUE using MQ Explorer:
- Right-click IN.QUEUE and select Put Test Message.
- Write any text and then click Put Message.
If you haven't placed any message on the IN.QUEUE queue, the Tester class displays: "No messages in queue."
To run the Tester class, add Tester.class and JNDIUtil.class to the CLASSPATH and then, using a command-line console, type:
java devx.articles.mqjms.Tester
The application writes a message to OUT.QUEUE and displays a message retrieved from IN.QUEUE. To check the messages sent to OUT.QUEUE, go to MQ Explorer, right-click OUT.QUEUE, and select Browse Messages.
Java and WMQ MOM in the Enterprise
In a large enterprise environment, being able to capitalize on WMQ's performance and stability, while writing your code to Java standard interfaces, can be a big advantage.