IBM Developer

Tutorial

Streaming messages to topics using alias queues

Distribute streamed messages to multiple subscribers

By Simone Jain

Using streaming queues can help you make the most of your messaging interactions. If you combine streaming queues with topics, you can leverage the publish/subscribe messaging framework. This allows you to distribute interactions carried out over a particular queue to many different systems. Streaming queues allow you to perform a variety of onward processing tasks, such as analytics or fraud detection.

In this tutorial, we will use the IBM MQ Console to stream messages to topics, using an alias queue as our streaming queue.

Prerequisites

Steps

Step 1. Creating a topic to stream messages to

Open the MQ console, and navigate to the Events tab.

IBM MQ Console Events tab showing create button for new topics

Click the Create button.

Provide a topic name (for example, DEV.STREAM) and topic string (for example, dev/stream/). For more information on topic string naming, take a look at the IBM MQ topic string documentation.

IBM MQ Console create topic dialog with DEV.STREAM name and dev/stream/ string

Step 2. Creating an alias queue to redirect messages to our topic

To create an alias queue, go to the Queues tab in the MQ Console.

IBM MQ Console Queues tab displaying queue management interface

Click the Create button. In the next window, choose Alias as your queue type.

IBM MQ Console queue type selection showing Alias option

You can name this queue whatever you'd like. We've used the DEV. prefix in this example to comply with the security assumed for the default developer configuration, for example: DEV.QUEUE.A_STREAM.

Then, set the Base object field to the name of the topic created (in this example, DEV.STREAM). Doing this means that the alias queue will resolve to this topic. Below this, in the Base type field, chose “Topic”.

IBM MQ Console alias queue configuration with base object set to DEV.STREAM

Now, messages for the alias queue DEV.QUEUE.A_STREAM will be published to the DEV.STREAM topic. We now need to configure a queue to stream messages from.

Step 3. Configuring a queue to stream messages from

In the previous tutorial, Setting up a streaming queue in the IBM MQ Console, you configured a queue, DEV.QUEUE.1, to stream messages from. Follow the steps in Step 2 of this tutorial to stream messages from this queue to our alias queue, DEV.QUEUE.A_STREAM.

Ensure that you set the Streaming queue name to your alias queue name (for example,DEV.QUEUE.A_STREAM).

So far, we have:

  • Created a topic
  • Created an alias queue which resolves to the topic
  • Set a queue to stream its messages to the alias queue (and therefore the topic)

IBM MQ streaming queue configuration showing message flow from queue to topic

Let's now connect applications and carry out some work, to see the streaming queue in action.

Step 4. Subscribing to the topic

In order to receive the messages that are published to our new topic, we need an application to subscribe to this topic.

In this step, we'll use JmsSub.java as our example application to subscribe to the topic, which is found in the mq-dev-patterns repository.

First, clone the repo using this command:

git clone <https://github.com/ibm-messaging/mq-dev-patterns>

Then, navigate to the JMS directory in this repo, and follow the steps in the README to build the sample applications. If you already have this repo cloned, ensure that your version is up to date.

Navigate to the env.json file in the mq-dev-patterns directory, and modify it to contain the connection information for your queue, for example:

{
  "MQ_ENDPOINTS": [{
    "HOST": "localhost",
    "PORT": "1414",
    "CHANNEL": "DEV.APP.SVRCONN",
    "QMGR": "QM1",
    "APP_USER": "app",
    "APP_PASSWORD": "passw0rd",
    "QUEUE_NAME": "DEV.QUEUE.1",
    "BACKOUT_QUEUE": "DEV.QUEUE.2",
    "MODEL_QUEUE_NAME": "DEV.APP.MODEL.QUEUE",
    "TOPIC_NAME": "dev/stream/"
  }]
}

Ensure the TOPIC_NAME variable is set to the topic string specified when creating the topic (for example, dev/stream/).

Now, run the program. If you used maven to build the JMS samples, run this command:

java -cp target/mq-dev-patterns-0.1.0.jar com.ibm.mq.samples.jms.JmsSub

Otherwise, compile:

javac -cp ./com.ibm.mq.allclient-9.2.5.0.jar:./javax.jms-api-2.0.1.jar:./json-20230227.jar:. com/ibm/mq/samples/jms/JmsSub.java

And then run:

java -cp ./com.ibm.mq.allclient-9.2.5.0.jar:./javax.jms-api-2.0.1.jar:./json-20230227.jar:. com.ibm.mq.samples.jms.JmsSub

You should see this output in your terminal:

Terminal output showing successful subscription to dev/stream/ topic

This shows that the subscriber is subscribed to the dev/stream/ topic successfully.

Step 5. Putting messages to the queue

In this step, we'll use JmsPut.java as our example application to put messages onto our queue.

In a separate terminal window to your subscriber, run the program. If you used maven to build the JMS samples, run this command:

java -cp target/mq-dev-patterns-0.1.0.jar: com.ibm.mq.samples.jms.JmsPut

Otherwise, compile:

javac -cp ./com.ibm.mq.allclient-9.2.5.0.jar:./javax.jms-api-2.0.1.jar:./json-20230227.jar:. com/ibm/mq/samples/jms/JmsPut.java

And then run:

java -cp ./com.ibm.mq.allclient-9.2.5.0.jar:./javax.jms-api-2.0.1.jar:./json-20230227.jar:. com.ibm.mq.samples.jms.JmsPut

Using the console we can see that 10 messages have been made available on the queue:

IBM MQ Console queue view displaying 10 available messages on DEV.QUEUE.1

Now, go to the terminal running the subscriber. It should look like this:

Terminal output showing subscriber receiving streamed messages from topic

So here, we can see that the messages were put onto DEV.QUEUE.1 as expected, but also streamed to our DEV.STREAM topic, where they were received by our subscribing application.

With the publish/subscribe framework, a history of the messages is not kept within the topic, and the default behaviour is for applications to receive messages from the point they subscribe to the topic. To continue to receive messages even when the subscription isn't actively connected, review the MQ documentation on durable subscriptions.

Summary and next steps

In this tutorial, we looked at how you can use alias queues to stream messages from a queue to a topic, all without making changes to your applications! The application used here is a simplistic example which only outputs received messages to the terminal, but you might choose to connect several applications: one which performs analytics, one which filters and stores the messages to use as test data, or anything else! Crucially, you can do all of this, without interfering with your normal message flows.

Want to know more about IBM MQ advanced development capabilities? Check out the series, “Uniform clustering in IBM MQ.”