This article provides a simple JMS publish and subscribe (pub/sub) application with instructions showing how to set up a WebSphere MQ Java™ Message Service (JMS) Client. It also describes how to set up WebSphere Business Integration Event Broker and WebSphere MQ and how to then run the sample application.
The sample application specifically shows how to use the WebSphere MQ Real-time Transport, which is a lightweight protocol optimized for use with non-persistent messaging. This transport is used exclusively by JMS clients and provides high levels of scalability and message throughput.
You should have a background in:
- Writing JMS pub/sub applications
- Working with Java and JMS
- Understanding WebSphere Business Integration Event Broker
This article assumes that WebSphere Business Integration Event Broker and WebSphere MQ are installed as per product documentation.
Important: When you install WebSphere MQ, ensure you select the option to install the WebSphere MQ Java component. This ensures you will have the necessary WebSphere MQ JMS files installed.
You also need to run the configuration manager and broker on the same machine and make sure that they share the same DB2® database.
The software levels used for this article and the sample application are detailed below:
- Operating System
- Windows® 2000
- Queue Manager
- WebSphere MQ 5.3.6
- Message Broker
- WebSphere Business Integration Event Broker 5.0.3
- Database
- DB2 UDB 8. FixPack 2
Before you attempt to follow the instructions below, download the zip file (PubSubRealTime.zip) attached to this article. Create a folder called C:\PubSubRealTime and unzip the file to it.
Once you have unzipped the file, you should see the following two files in the table below:
Table 1. File names and description
| Name of file | Description |
RunPubSubRealTime.bat | Example batch file used to launch the sample application |
PubSubRealTime.java | MQ JMS sample |
These two files are described in more detail later.
Configuring WebSphere Business Integration Event Broker
Before you can configure WebSphere Business Integration Event Broker, you need to set up the prerequisite components, the DB2® database and the WebSphere MQ queue manager.
This section sets up the components and the broker. All of these sections must be completed to have a working system. You can find more detailed explanations on the following steps in Chapter 5 of the Redbook, WebSphere Business Integration Message Broker Basics .
To create a database called SampleDB, use the DB2 Control Center:
- Select Start => Programs => IBM® DB2 => General Administrative Tools => Control Center to start the Control Center.
- Once the Control Center is open, select Tools => Wizards => Create Database Wizard (from the list).
- Click Ok.
- Choose your instance from the list and click Ok.
- In the Wizard, type SampleDB as the Database name.
- Click Finish.
Now, use the ODBC Data Source Administrator to create an ODBC connection for the database:
- Select Start => Settings => Control Panel => Administrative Tools => Data Sources (ODBC) to open the ODBC Data Source. The ODBC Data Source Administrator tool displays.
- Select the System DSN tab and click Add.
- From the list, double-click IBM DB2 ODBS Driver.
- In the dialogue box that displays, enter SampleDB as the Data source name and choose SAMPLEDB from the drop-down list for the Database alias.
To create the QueueManager(QM):
- Create the QM called SampleQM using WebSphere MQExplorer.
- Ensure a listener is created on port 1414 and that the QM and listener are started.
To start the tool:
- Select Start => Programs => IBM WebSphere MQ => WebSphere MQ Explorer.
- Double-click WebSphere MQ. A Queue Managers folder displays.
- Right-click the Queue Managers folder and select New => Queue Manager.
- In the Create Queue Manager dialogue box, enter SampleQM in the Queue Manager field.
- Select Next three times until you reach the panel entitled "Create Queue Manager (Step 4)".
- In the dialog box, enter 1414 for the "Listen on port number" field.
- Click Finish. The Queue Manager and listener will now be created.
To create WebSphere MQ JMS queues, create the required WebSphere MQ queues so you can use this queue manager with JMS:
- Open a Windows command prompt:
- Change to the WebSphere MQ install directory. Where <MQ Install Dir> is the MQ installation directory; for example, on Windows, the default install location is:
C:\Program FIles\IBM\WebSphereMQ\
C:\> cd < WebSphere MQ Install Dir>\Java\bin - Run the mqsc script (
MQJMS_PSQ.mqsc) to create the JMS Queues:
C:\> runmqsc SampleQM < MQJMS_PSQ.mqsc
Creating and starting Broker and Configmgr
Run the following commands from a command prompt, where <user> is the user id in use, <password> is the users password.
- Create the configuration manager:
C:\>mqsicreateconfigmgr -i <user> -a <password> -n SampleDB -q SampleQM - Create the broker:
C:\>mqsicreatebroker SampleBroker -i <user> -a <password> -n SampleDB -q SampleQM - Start the broker and configuration manager:
C:\>mqsistart configmgr
C:\>mqsistart SampleBroker
Creating and deploying the message flow
To do this, you will need to open the Message Brokers Toolkit and complete the following steps:
- From the Broker Administration perspective, create a new broker domain.
- Add the newly created SampleBroker to the domain.
- Create a new message flow project called "MyMsgFlows"and a new message flow called "RealTimeFlow". In the message flow, add a Real-timeOptimizedFlow node whose port is set to 2029 (right-click on node and select properties to set). This node is shown in Figure 1.
- Create a new message broker archive called "RealTime". Add the "RealTimeFlow" to the archive and save it.
- Drag and drop the broker archive on to the default execution group of the SampleBroker to deploy the flow.
Your toolkit view should now look like Figure 1, below:
Figure 1. Message Broker Toolkit
Now that we have the broker setup and a flow deployed, we need to set up and run a client to publish and subscribe to the broker using the WebSphere MQ Real-time transport. Attached to this article is an example WebSphere MQ JMS client application called PubSubRealTime.java .
The PubSubRealTime application creates a JMS Publisher and Subscriber -- the publisher sends a set number of messages that are then received by the subscriber. You should examine the code and read the comments to ensure you are familiar with what it is doing before you try to run it.
Running the PubSubRealTime Client
- Setting the classpath
- To compile and run the PubSubRealTime program, you will need to add the following jars to your classpath; they can be found in the
"WMQ Install Dir"\java\lib directory.- com.ibm.mq.jar
- com.ibm.mqjms.jar
- connector.jar
- jms.jar
- rmm.jar
- Program parameters
Valid parameters for the PubSubRealTime program are:
usage: java PubSubRealTime -h <brokerHost> -p <brokerPort> -n <numMessagesToSend> -t <topicName>
These are explained in more detail below:
Table 2. Parameters and descriptions for PubSubRealTime program
| Parameter | Description |
-h <brokerHost> | The hostname or IP Address of the machine where the broker is running. If this flag is not set the default value is localhost. |
-p <brokerPort> | The port that the Broker is listening on, this is the port number that the Real-timeOptimizedFlow node is set to. If this flag is not set the default value is 2029. |
-n <numMessagesToSend> | The number of messages that the publisher will send. If this flag is not set the default value is 1. |
-t <topicName> | The name of the topic that the subscriber will subscribe to and the publisher will publish on. If this flag is not set the default value is "default/topic1". |
Using RunPubSubRealTime.bat
For convenience, we have included a windows batch file called RunPubSubRealTime.bat to compile and run the PubSubRealTime program. This batch file must be edited to suit your environment. The following lines must be changed to point to the Java and WebSphere MQ locations on your machine:
Listing 1. Change these lines to point to the Java and WebSphere MQ locations on your machine
set JAVA_PATH=c:\jdk1.3\bin\ set PATHTOMQJARS=C:\Program Files\IBM\WebSphere MQ\Java\lib |
If you are running the PubSubRealTime program on a different machine to the broker, then you will also need to change the -h parameter to tell the program the broker's machine, as by default this is set to localhost. Once the batch file has been edited, you can run it from a command prompt:
Listing 2. Run from a command prompt
C:\PubSubRealTime\ > RunPubSubRealTime.bat |
Program output
When you run the RunPubSubRealTime.bat, you should see the output as shown below. This shows that five messages have been sent and received.
Figure 2. Program ouput
The example does not use Java Naming and Directory Interface (JNDI), this was deliberate to try to keep the code and instructions as simple as possible. However the provider specific code has been restricted to just one method, getTopicConnection. This method could be altered to set up any JMS provider or to use JNDI to retrieve the Administered Objects.
The example could also be easily altered so that you can run several publisher or subscribers at once or run the publishers and subscribers on different machines.
Problems creating the broker or configmgr
Ensure the user has access to the DB2® database, if you installed DB2 with a different user, then you will need to specify this user as well when you create the broker and configuration manager using the -u and -p flags (see product documentation for details). Also ensure that the WebSphere Business Integration Event Broker Security Wizard has been run for your user to ensure it is a member of the required groups.
How to see non-durable subscriptions in the WebSphere Business Integration Event Broker Toolkit
You may wish to see the subscriptions made to the broker by the sample program -- by default, the toolkit will not show subscriptions created using the WebSphere MQ Real-time transport. To show the subscriptions, set the property to tell the broker to display non-durable subscriptions. With the broker still running, from a command prompt, run:
Listing 3. Set the following property to show the subscriptions
C:\>mqsichangeproperties SampleBroker -e default -o |
Now, use these commands to restart the broker:
Listing 4. Use these commands to restart the Broker
C:\>mqsistop SampleBroker C:\>mqsistart SampleBroker |
In the toolkit, you can view subscriptions that are using the WebSphere MQ Real-time transport by opening the Broker Domain view from the Broker Administration Perspective. Also, double-click on the subscriptions field. Figure 3 below shows the toolkit displaying subscriptions, while the PubSubRealTime program is running. You can see the topic being subscribed to is: sample/mytopic. The subscriptions beginning with $ are system subscriptions and can be ignored.
To refresh the list of subscriptions, click the button circled in Figure 3 below.
Figure 3. Non-durable subscriptions
This article has shown you how to set up and run a simple JMS pub/sub application using WebSphere Business Integration Event Broker with the WebSphere MQ Real-time transport. You can use the information provided in this article as a good first step in writing a more advanced JMS application.
| Description | Name | Size | Download method |
|---|---|---|---|
| PubSubRealTime.zip file | PubSubRealTime.zip | 10 KB | FTP |
Information about download methods
- Share your questions and views on this article with the author
and other readers in the WebSphere Business Integration Event Broker forum
- Discuss this article with the author and other readers in the WebSphere MQ forum
- WebSphere Business Integration information library
- Visit developerWorks WebSphere Business Integration zone for business integration resources.
- Browse for books on these and other technical topics.
Rich Bicheno has worked as a developer for WebSphere MQSeries Everyplace for three years. For the past year, Rich worked as a Performance Specialist with the WebSphere Business Integration Event Broker performance team in IBM Hursley. Rich works with the development teams in evaluating new releases of WebSphere Business Integration Event Broker and with customers to provide fee-based consultancy on design, configuration, and tuning issues relating to this product.





