Professional Documents
Culture Documents
I Mqrad2 PDF
I Mqrad2 PDF
13 Jun 2006
In this second installment in the series, use IBM Rational® Application Developer and
IBM WebSphere® MQ configurations to make your Java™ Message Service (JMS)
programs more flexible. And while you're at it, learn more about JMS
publish/subscribe programming.
Objectives
In this tutorial, you learn how to:
Prerequisites
If you are a Java programmer who needs to understand how to write and test JMS
programs using WebSphere MQ and Rational Application Developer, this tutorial is
for you. It's assumed that you have intermediate knowledge of Java and an
introductory knowledge of JMS. The instructions in this tutorial also assume that you
have performed the instructions in Part 1, since they build on the configurations and
sample code created and tested in that tutorial.
System requirements
The instructions in this tutorial are for use in a Windows® environment, although
users 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
Continuing from Part 1
In Part 1 of this tutorial series, you get a brief overview of enterprise messaging
systems and JMS. And you get descriptions of the latest tools from IBM for JMS
development and testing, including detailed instructions on obtaining, installing, and
configuring those tools. You're provided with sample programs and instructions to
help you use the tools and understand how they work.
This tutorial picks up where Part 1 left off, to further explore WebSphere MQ and
JMS programming with Rational Application Developer (Application Developer). The
instructions in this tutorial assume you have performed the instructions in the first
tutorial, since they build on the configurations and sample code created and tested
in that tutorial.
Acknowledgements
I want to acknowledge and thank Mr. Gareth Matthews of IBM Hursley Labs, author
of this SupportPac, for making this new version available in time for this tutorial.
1. Launch Application Developer from the Start menu. (From here on, the
instructions assume that Application Developer is running.)
3. Switch to the Java perspective using the perspective switch button in the
top right corner. (See Figure 2.)
4. From the main menu, select Window > Run History > JMSAdminGUI.
6. Click OK.
14. Type localhost in the HostName: field. The dialog should look like
Figure 4.
1. In Application Developer, select Run > Run History > QReceiverUI from
the main menu.
3. Type EnderQ in the Queue: field and press Enter or click Set.
4. From the main menu, select Run > Run History > QSenderUI.
6. Type EnderQ in the Queue: field and press Enter or click Set.
7. Make both the Queue Sender and Queue Receiver windows visible.
8. Type text into the Message: field of Queue Sender and press Enter or
click Send.
10. When finished, close both windows by clicking the X in the top right
corner.
These instructions are for use on a single computer. I encourage you to run the
QReceiverUI and QSenderUI programs on another computer where WebSphere MQ
is not installed to truly demonstrate client transport. Make sure that the host property
of the QueueConnectionFactory has the name of the computer where WebSphere
MQ is installed and that the name can be resolved to an IP address through DNS or
the operating system HOSTS filed. You can also provide an IP address for the host
property. The queue manager that JMSAdminGUI uses as its provider URL will also
have to be configured for client transport.
This is where you will define a second queue manager on your computer and a local
queue on that queue manager, which will be the destination for the remote queue.
5. Type 1415 in the Listen on port number: field (queue manager Ender is
already using the default port of 1414, so this queue manager must use a
different one).
6. Click Finish.
8. Right-click on Queues and select New > Local Queue. (See Figure 5.)
Now, on the original queue manager, you need to create a transmission queue and
a remote queue, pointing it to the destination queue on the second queue manager.
1. Right-click on Queues under Ender and select New > Local Queue.
2. Type Petra in the Name: field. (By convention, transmission queues are
named for the remote queue manager for which they will be used to
communicate.)
3. Click Next.
5. Click Finish.
6. Right-click on Queues under Ender and select New > Remote Queue
Definition.
8. Click Next.
11. Type Petra in the Transmission queue field. (See Figure 7.)
Communications channels
The mechanism for transporting messages between queue managers is called a
communications channel. To create a communications channel, a channel definition
must be created on each queue manager -- a sender channel, on the queue
manager which is sending messages, and a receiver channel on the queue
manager, which is receiving messages. Each end of the communications channel
has the same name, usually indicating the direction of message flow.
4. Click Finish.
7. Click Next.
11. Right-click the Ender_2_Petra sender channel and select Start. (See
Figure 10.)
1. In Application Developer, select Run > Run from the main menu.
6. Type Petra in the Connection: field and press Enter or click Set.
7. Type PetraQ in the Queue: field and press Enter or click Set.
8. From the main menu, select Run > Run History > QSenderUI.
9. Type Ender in the Connection: field and press Enter or click Set.
10. Type RemotePetraQ in the Queue: field and press Enter or click Set.
11. Make both the Queue Sender and Queue Receiver windows visible.
12. Type text into the Message: field of Queue Sender and press Enter or
click Send.
13. You should see the message text appear in the Queue Receiver window.
14. When you are finished, close both windows by clicking the X in the top
right corner.
Section 6. Publish/subscribe
In the next two sections, you configure and run sample programs demonstrating
JMS publish/subscribe capabilities. In the first section, you will run the programs
using WebSphere MQ as the JMS provider. In the second section, you will run the
programs using the built-in JMS provider provided with the WebSphere test
environment included with Application Developer.
applications. You will run a supplied command file to set up the broker and then start
the broker for operation.
3. Press Enter.
3. In Application Developer, from the main menu select File > Import.
5. In the From zip file: field, enter the location of the MQPubSub.zip file
extracted previously.
6. Select the checkbox next to the MQPubSub project, and click Finish.
ChatUser class
The call to close() ensures that any active JMS objects have been properly shut
The next method to look at is setTopic(). This method is called when a Topic
name is entered in the GUI, as shown in Listing 2.
The call to stop() ensures that the TopicConnection is stopped and that any
active TopicPublisher and TopicSubscriber objects are closed. The Topic
object is then retrieved from JNDI using the topicName parameter. Next, the
TopicPublisher and TopicSubscriber objects are created using the
respective TopicSession objects. The message listener for asynchronous
message delivery is set for the TopicSubscriber; ChatUser implements the
MessageListener interface, and the onMessage() method of ChatUser will be
called asynchronously as messages arrive for the subscribed topic. The
TopicConnection is then started and sendEnterMessage() is called, which
publishes the message announcing that the user has entered the chat room.
try {
internalPublishMessage(getUserName() + ": " + message);
} catch (Throwable e) {
setExceptionMessage(e);
throw e;
}
}
As you can see, publishMessage() simply takes the String passed in,
prepends the user name to it, and passes the String to a private method,
internalPublishMessage(). In internalPublishMessage(), a
TextMessage is created with the text parameter, and the TopicPublisher is
used to publish the message.
This method is called by JMS each time a message is published to the subscribed
topic . The text of the message is used to set the message property of the
ChatUser object. This code should look familiar as it is exactly the same code as
the onMessage() method in QReceiver. With JMS programming, receiving a
message sent to a queue and receiving a message published to a topic are exactly
the same.
ChatMonitor class
The ChatMonitor class is very similar to the ChatUser class, except that it has no
message-publishing facilities. The setConnection() and setTopic() methods
are much like those in ChatUser, except for the absence of code pertaining to
publishing, so we won't go over those in detail. However, let's take a look at the
onMessage() method, shown in Listing 5.
8. Click OK.
9. From the main menu, select Object > New > Topic.
1. Right-click ChatUI in the MQPubSub project, and select Run > Run.
3. Click New.
9. Navigate to the MQTools folder and select the following JAR files:
• com.ibm.mq.pcf.jar
• mqcontext.jar
15. Type User1 in the User Name: field of the Chat window and press Enter
or click Set.
16. Type EnderTCF in the Connection: field and press Enter or click Set.
17. Type Java in the Topic: field and press Enter or click Set.
18. The message "User1 has entered the chat room" should display.
19. In Application Developer, from the main menu, select Run > Run
History>ChatUI.
20. In this second Chat window, type User2 in the User Name: field and
press Enter or click Set.
21. Type EnderTCF in the Connection: field and press Enter or click Set.
22. Type Java in the Topic: field and press Enter or click Set.
23. You will see the message "User2 has entered the chat room" in both chat
windows.
24. In the Message: field of either window, type any text and press Enter or
click Send. The message text displays in both windows, prepended with
the name of the user that published the message.
25. Try changing the topic in each of the windows to one of the other topics
created with JMSAdminGUI. Don't try AllComputers, AllBooks, or
AllTopics, however, because publishing is not allowed to wildcard (*)
topics, only subscribing.
26. Also, try running additional instances of ChatUI with different user
names.
27. Change the topic on one of the Chat windows to Java before proceeding
with the next steps.
35. Type EnderTCF in the Connection: field of the chat monitor window and
press Enter or click Set.
36. Type AllComputers in the Topic: field and press Enter or click Set.
37. Switch to the chat window with the Java topic in use, ensuring that the
chat monitor window is visible and send a message.
38. The message text will display in the chat monitor window, prepended with
the topic to which the message was published and the user that published
the message.
39. Switch to the chat window with the Java topic in use, and type Linux in
the Topic: field, then press Enter or click Set.
41. The message text will display in the chat monitor window.
42. In the chat window with the Linux topic in use, type Cyberpunk and
press Enter or click Set.
44. You will not see the message in the chat monitor window.
45. In the chat monitor window, type AllTopics and press Enter or click
Set.
46. Switch to the chat window with the Cyberpunk topic in use, and send a
message.
47. You will see the message in the chat monitor window.
48. When you are finished, close all chat and chat monitor windows by
clicking the X in the upper-right corner.
1. In Application Developer, from the main menu, select File > Import.
3. In the From zip file: field, enter the location of the EMQPubSub.zip file
extracted previously.
9. Click JMS topic connection factory from the Connection Factories list.
17. Click Default messaging from the navigation bar to the left.
26. Follow steps 19 through 25 to create additional topics, all using the
BeanBus bus, as shown in Table 2.
1. When you have created all of the topics, click Save at the top of the
screen.
2. Click Save.
1. Right-click EChat in the Project Explorer view and select Run > Run.
6. Type User1 in the User Name: field of the chat window and press Enter
or click Set.
8. Type jms/Java in the Topic: field and press Enter or click Set.
9. The message, "User1 has entered the chat room" will display.
10. In Application Developer, from the main menu, select Run > Run
History>EChat.
11. In this second chat window, type User2 in the User Name: field and
press Enter or click Set.
12. Type jms/BeanTCF in the Connection: field and press Enter or click
Set.
13. Type jms/Java in the Topic: field and press Enter or click Set.
14. You will see the message, "User2 has entered the chat room" display in
both chat windows.
15. In the Message: field of either window, type any text and press Enter or
click Send. The message text displays in both windows, prepended with
the name of the user that published the message.
16. Try changing the topic in each of the windows to one of the other topics
created with JMSAdminGUI. Don't try jms/AllComputers or jms/AllBooks,
however, because publishing is not allowed to wildcard (*) topics, only
subscribing.
17. Also, try running additional instances of EChat with different user names.
18. Change the topic on one of the chat windows to Java before proceeding
with the next steps.
26. Type jms/BeanTCF in the Connection: field of the chat monitor window
and press Enter or click Set.
27. Type jms/AllComputers in the Topic: field and press Enter or click
Set.
28. Switch to the chat window with the jms/Java topic in use, ensuring that
the chat monitor window is visible and send a message.
29. The message text will display in the chat monitor window, prepended with
the topic to which the message was published and the user that published
the message.
30. Switch to the chat window with the jms/Java topic in use, and type
jms/Linux in the Topic: field. Next, press Enter or click Set.
32. The message text will display in the chat monitor window.
33. In the chat window with the jms/Linux topic in use, type jms/Cyberpunk
and press Enter or click Set.
35. You will not see the message in the chat monitor window.
36. When you are finished, close all chat and chat monitor windows by
clicking the X in the top-right corner.
37. Stop the server test environment before closing Application Developer.
Section 9. Summary
In this tutorial, you picked up where the preceding tutorial left off and explored some
additional topics in JMS programming, including WebSphere MQ client transport,
WebSphere MQ remote queuing, and JMS publish/subscribe programming.
In the next tutorial, you'll look at using WebSphere MQ as the JMS provider in
WebSphere Application Server. You will also explore JMS programming using the
unified domain interfaces provided in JMS 1.1.
Downloads
Description Name Size Download method
sample code i-mqrad2code.zip 47KB HTTP
Resources
Learn
• In "Introducing the Java Message Service" (developerWorks, June 2004), learn
the basics for writing JMS applications.
• Learn more about Java Message Service at Sun's site.
• Visit the "WebSphere MQ Roadmap" to learn more about WebSphere MQ.
• Check out the "Rational Application Developer Roadmap."
• Learn more about remote queuing in the WebSphere MQ Intercommunication
manual.
• In "Deploying publish and subscribe applications into the Service Integration
Bus" (developerWorks, Aug 2005), learn how to configure IBM WebSphere
Application Server V6 to use the Service Integration Bus as its Java Message
Service (JMS) provider for publish/subscribe applications.
• "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.
• Download the newest version of SupportPac ME01 - WebSphere MQ Initial
Context Factory. This newest version is required to run the sample code in this
tutorial.
• 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.