Skip to main content

Write a JMS pub/sub application using WebSphere Business Integration Event Broker and WebSphere MQ Real-time transport

Rich Bicheno (Rich_Bicheno@uk.ibm.com), Performance Specialist, IBM Hursley United Kingdom
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.

Summary:  Writing a JMS publish and subscribe application from scratch can be a challenge, particularly given the number of components and products that need to be set up when using WebSphere® Business Integration Event Broker as the JMS provider. This article shows 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. This information provides a good first step for developers that want to write a more advanced JMS application.

Date:  20 Oct 2004
Level:  Intermediate
Activity:  451 views
Comments:  

Introduction

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.

Assumptions

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.

Software environment

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 begin

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.batExample batch file used to launch the sample application
PubSubRealTime.javaMQ 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 .

Creating the DB2 database

To create a database called SampleDB, use the DB2 Control Center:

  1. Select Start => Programs => IBM® DB2 => General Administrative Tools => Control Center to start the Control Center.
  2. Once the Control Center is open, select Tools => Wizards => Create Database Wizard (from the list).
  3. Click Ok.
  4. Choose your instance from the list and click Ok.
  5. In the Wizard, type SampleDB as the Database name.
  6. Click Finish.

Now, use the ODBC Data Source Administrator to create an ODBC connection for the database:

  1. Select Start => Settings => Control Panel => Administrative Tools => Data Sources (ODBC) to open the ODBC Data Source. The ODBC Data Source Administrator tool displays.
  2. Select the System DSN tab and click Add.
  3. From the list, double-click IBM DB2 ODBS Driver.
  4. 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.

Configuring WebSphere MQ

To create the QueueManager(QM):

  1. Create the QM called SampleQM using WebSphere MQExplorer.
  2. Ensure a listener is created on port 1414 and that the QM and listener are started.

To start the tool:

  1. Select Start => Programs => IBM WebSphere MQ => WebSphere MQ Explorer.
  2. Double-click WebSphere MQ. A Queue Managers folder displays.
  3. Right-click the Queue Managers folder and select New => Queue Manager.
  4. In the Create Queue Manager dialogue box, enter SampleQM in the Queue Manager field.
  5. Select Next three times until you reach the panel entitled "Create Queue Manager (Step 4)".
  6. In the dialog box, enter 1414 for the "Listen on port number" field.
  7. 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:

  1. Open a Windows command prompt:
  2. 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
  3. 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.

  1. Create the configuration manager:
    C:\>mqsicreateconfigmgr -i <user> -a <password> -n SampleDB -q SampleQM
  2. Create the broker:
    C:\>mqsicreatebroker SampleBroker -i <user> -a <password> -n SampleDB -q SampleQM
  3. 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:

  1. From the Broker Administration perspective, create a new broker domain.
  2. Add the newly created SampleBroker to the domain.
  3. 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.
  4. Create a new message broker archive called "RealTime". Add the "RealTimeFlow" to the archive and save it.
  5. 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
Message Broker Toolkit

The PubSubRealTime Client

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
Program output

Hints and tips

Extending PubSubRealTime.java

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
DynamicSubscriptionEngine -n nondurableSubscriptionEvents -v true

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
Non-durable subscriptions

Conclusion

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.



Download

DescriptionNameSizeDownload method
PubSubRealTime.zip filePubSubRealTime.zip10 KBFTP|HTTP

Information about download methods


Resources

About the author

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.

Comments



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=18900
ArticleTitle=Write a JMS pub/sub application using WebSphere Business Integration Event Broker and WebSphere MQ Real-time transport
publish-date=10202004
author1-email=Rich_Bicheno@uk.ibm.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers