Professional Documents
Culture Documents
09 May 2006
applications. Sample applications are provided along with instructions for running
those applications using the installed products.
Objectives
Prerequisites
This tutorial is intended for Java programmers who need to understand how to write
and test JMS programs using WebSphere MQ and Rational Application Developer.
Intermediate knowledge of Java and introductory knowledge of JMS is assumed.
System requirements
The instructions in this tutorial are for use in a Windows environment, though
readers knowledgeable in other operating systems can probably adapt them for use
in their operating system.
The system requirements for the products used in the tutorial can be found through
the following links:
• WebSphere MQ
• Rational Application Developer
Section 2. Introduction
The Java Message Service (JMS) provides a standard, portable way for Java
programmers to access MOM products. The key to JMS portability is that the API is
defined as a set of interfaces; MOM vendors deliver a provider that implements
those interfaces for their particular product. Additionally, JMS resources are
accessed through the Java Naming and Directory Interface (JNDI), providing
additional portability and abstraction. Using JMS, Java programmers retrieve JMS
resources, called administered objects, from JNDI and access them through the
standard JMS interfaces, relieving them from having any knowledge of the details of
the underlying messaging system. WebSphere MQ has a built-in JMS provider.
Let's get to some hands-on stuff! In this section, you go through the steps to install
the software needed to complete this tutorial.
Install WebSphere MQ
4. Click Software Requirements on the navigation bar on the left, then click
+ next to WebSphere Eclipse Platform Version 3.0.1. (See Figure 1).
10. After accepting the license agreement terms, select Custom as the setup
type.
12. You need to change the default installation folders, so click Change.
13. Enter C:\WSMQ in the Folder name: field. (See Figure 2.)
16. Accept all defaults. (Do NOT set up the default configuration.)
17. On the last screen of the Prepare WebSphere MQ Wizard, uncheck all
options and click Finish.
The SupportPacs consist of .zip files that contain JAR files and documentation.
You'll need these JAR files to set up JMS-administered objects and run the sample
programs later.
2. The download page for SupportPac MS0N also has links to download
SupportPacs ME01 and MS0B.
3. Once you have the three files (ms0n.zip, me01.zip, and ms0b.zip)
downloaded, extract each file to a folder named MQTools. (I put the
MQTools folder on my Windows desktop, but you can put it where it suits
you.)
2. When all four files have been downloaded, launch extractor.exe to extract
the installation image to a directory on your system.
3. The extractor will provide a default location for the installation image. You
can change this to any directory that suits you.
5. Once the installation image is created, select the checkbox Start the
installation wizard now and click Finish.
7. Once the installer has started, accept the license agreement. Change the
installation directory to C:\RAD (See Figure 3).
10. Ensure the Launch Agent Controller install check box is checked and
click Finish.
11. When the Agent Controller installer starts, accept all defaults until the
Java Runtime dialog.
You now have all of the software installed. In the next section, you'll install the
sample code.
5. Click OK.
7. Switch to the Java perspective by selecting Window from the main menu,
then selecting Open Perspective > Java.
10. In the From zip file: field, enter the location of the MQP2P.zip file
extracted previously. (See Figure 6.)
11. Select the checkbox next to the MQP2P project, and click Finish.
the user interfaces for the programs using standard Abstract Windowing Toolkit
(AWT) facilities. (Because no JMS code is in these classes, we don't need to spend
any time going over them.)
QSender class
The first class with JMS code to look at is QSender. This class is used by
QSenderUI to send messages to a queue. Three methods in this class contain JMS
code: setConnection(), setQueue(), and sendMessage().
setConnection(), shown in Listing 1.
Note that in this method, and all the other methods, containing JMS code that we will
look at, all code is in terms of JMS interfaces -- there is no WebSphere MQ-specific
code anywhere.
try {
if (connection != null)
connection.stop();
if (sender != null)
sender.close();
Queue queue = (Queue) getInitContext().lookup(queueName);
sender = session.createSender(queue);
sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sender.setPriority(4);
sender.setTimeToLive(0);
connection.start();
} catch (Throwable e) {
setExceptionMessage(e);
throw e;
}
}
Listing 3. sendMessage()
QReceiver class
Now, take a look at the QReceiver class. It also contains setConnection() and
setQueue() methods. Because the setConnection() method is identical to that
in QSender, I'll skip over it here. Below is the setQueue() method, shown in
Listing 4.
Listing 4. setQueue()
The obvious difference in the setQueue() method here and the setQueue()
method in QSender is that a QueueReceiver is created instead of a
QueueSender. But there is one more thing to look at. Notice the
setMessageListener() call. The QReceiver class is an implementation of the
javax.jms.MessageListener interface. By setting a MessageListener in the
QueueReceiver, messages that are received at the queue are passed
asynchronously to the MessageListener object. This allows the
MessageListener to process the messages as they arrive, and the receiver does
not have to query the Queue for new messages.
Listing 5. setQueue()
This method is called by JMS each time that a message arrives on the Queue that is
being listened to. Here, the text of the message is used to set the message property
of the QReceiver object.
The other methods of QSender and QReceiver classes are fairly self-explanatory
and take care of JNDI access, bound properties, and exception handling.
2. In the Navigator view on the left, right-click Queue Managers and select
New > Queue Manager.
7. Click Finish.
of a separate JNDI server. You can run the JMS Admin GUI from within Application
Developer.
First you'll need to set up a Java project which includes the JMS Admin GUI JAR file.
1. From the main menu, select File > New > Project.
4. Click Next.
Now you can run the tool and create the JMS administered objects.
2. Right-click JMSAdminGUI.class, and select Run > Run. (See Figure 9.)
4. Click New. Ensure that the Run dialog looks like Figure 10.
12. Navigate to the MQTools folder and select the three JAR files there. (Use
Ctrl + click to select multiple files.)
• com.ibm.mq.pcf.jar
• jmsadmingui.jar
• mqcontext.jar
13. The JAR selection dialog should look like Figure 11.
17. The JAR selection dialog should look like Figure 12.
20. Click the Create a new config profile radio button, then click OK.
21. Type WMQ in the File Name field, then click Create.
24. You JNDI Configuration dialog should look like Figure 13.
26. From the main menu, select Object > New > Queue.
27. Type EnderQ in the Object Name: field, then click OK.
Run QReceiverUI
1. Right-click QReceiverUI in the MQP2P project, and select Run > Run.
3. Click New. Ensure that the Run dialog looks like Figure 14.
• -Dsun.boot.library.path=C:\RAD\eclipse\jre\bin;C:\WSMQ\Java\li
Other than the space between the arguments, there should be no other
spaces or carriage returns. Be sure to include the hyphen before each
argument. The arguments will wordwrap in the VM arguments text area.
This is OK as long as you do not type spaces (other than the ones
between the arguments) or carriage returns.
9. Navigate to the MQTools folder and select the following JAR files:
• com.ibm.mq.pcf.jar
• mqcontext.jar
15. Type Ender in the Connection field and press Enter or click Set.
16. Type EnderQ in the Queue field and press Enter or click Set.
Run QSenderUI
To create the configuration for QSenderUI, you can duplicate the QReceiverUI
configuration, then change the configuration name and main class.
6. Click Apply. Ensure the run dialog looks like Figure 15.
7. Click Run.
8. Type Ender in the Connection field and press Enter or click Set.
9. Type EnderQ in the Queue: field and press Enter or click Set.
1. Make both the Queue Sender and Queue Receiver windows visible.
2. Type text into the Message field of Queue Sender and press Enter or
click Set.
3. You should see the message text appear in the Queue Receiver window.
4. When you are finished, close both windows by clicking the X in the
upper-right corner.
1. From the main menu, select Window > Open Perspective > J2EE.
8. Click Buses.
9. Click New.
13. Click Bus members from the Additional properties list. See Figure 16.
16. Click Save at the top of the page, then click Save.
17. Click the BeanBus link at the top of the page. (See Figure 17.)
1. On the navigation bar to the left, click Resources > JMS Providers >
Default messaging. (See Figure 18.)
3. Click New.
7. Click OK.
16. Click Save at the top of the page, then click Save.
18. Right-click WebSphere Application Server V6.0 in the Servers view and
select Restart > Start.
In this section, you will import projects that have the sample programs repackaged
as application clients. Once you've completed that, you can run the programs to use
the built-in messaging of the test environment.
3. In the From zip file field, enter the location of the EMQP2P.zip file
extracted previously from the sample code download file.
5. Click Finish.
If you look at the application client projects, you will see that each one has a Main
class with a main() method. The main() method of the Main class in EQSender
calls QSenderUI.main(). Likewise, the main() method of the Main class in
EQReceiver calls QReceiverUI.main().
Run EQReceiver
3. Click New.
5. Click Run.
6. Type jms/BeanQCF in the Connection field and press Enter or click Set.
7. Type jms/MyBeanQ in the Queue field and press Enter or click Set.
Run EQSender
3. Click New.
5. Click Run.
7. Type jms/MyBeanQ in the Queue field and press Enter or click Set.
1. Make both the Queue Sender and Queue Receiver windows visible.
2. Type text into the Message: field of Queue Sender and press Enter or
click Set.
3. You should see the message text appear in the Queue Receiver window.
4. When you are finished, close both windows by clicking the X in the
upper-right corner.
In the next tutorial, you'll look at JMS publish-subscribe programs. You will also look
at some other WebSphere MQ configurations that can make your programs more
flexible.
Downloads
Description Name Size Download method
sample code i-mqrad1code.zip 36KB HTTP
Resources
Learn
• In "Introducing the Java Message Service" (developerWorks, June 2004), learn
the basics for writing JMS applications.
• Learn more about the Java Message Service at Sun's site.
• Visit the WebSphere MQ Roadmap to learn more about WebSphere MQ.
• Visit the "Rational Application Developer Roadmap" to learn more about
Rational Application Developer.
• "Developing a standalone Java application for WebSphere MQ"
(developerWorks, Feb 2005) shows how to develop an application that sends
and receives messages using WebSphere MQ that does not require being
deployed in a J2EE application server.
• In "Using WebSphere MQ with WebSphere Application Server Community
Edition" (developerWorks, Jan 2006), learn how to configure WebSphere
Application Server Community Edition to use WebSphere MQ as its JMS
provider.
• "Grouping messages using the WebSphere MQ Java and JMS APIs"
(developerWorks, Feb 2006) provides examples of WebSphere MQ's message
group support using JMS.
• Stay current with developerWorks technical events and webcasts.
Get products and technologies
• Download a free trial version of WebSphere MQ Version 6.0.
• Download a free trial version of Rational Application Developer Version 6.0.
• Download the SupportPac MS0N - GUI version of the existing JMS Admin tool.
This page also has download links to the other SupportPacs used in this
tutorial: ME01 - WebSphere MQ Initial Context Factory and MS0B - Java
classes for PCF.
• Build your next development project with IBM trial software, available for
download directly from developerWorks.
Discuss
• Participate in the discussion forum for this content.
• Participate in developerWorks blogs and get involved in the developerWorks
community.
Trademarks