Publish/Subscribe Messaging Can Be Simple with WebSphere MQ

Publish/Subscribe Messaging Can Be Simple with WebSphere MQ

essage-oriented middleware (MOM) decouples applications by allowing them to communicate using one of two messaging models: point-to-point messaging and publish/subscribe messaging. The models differ in fundamental ways, but neither requires applications to know the other’s implementation details.

Point-to-point messaging achieves only partial decoupling of applications, because you still need to know where to put messages. Publish/subscribe provides a higher degree of independence and reduces design complexity in many cases, but it requires more coding and configuration. Traditionally, MOM shops have opted for point-to-point messaging to avoid the more complex requirements of publish/subscribe.

Enter IBM WebSphere MQ (WMQ), Version 7. This latest release of WMQ aims to make coding and configuring publish/subscribe easier and more intuitive. A previous DevX article focused on the point-to-point messaging capabilities of WMQ. This article explains WMQ’s publish/subscribe messaging.

Publish/Subscribe Messaging Overview

Publish/subscribe is the messaging-oriented equivalent of the object-oriented observer design pattern. In publish/subscribe messaging, a sender application creates a message containing the information it wants to send and assigns it a topic that denotes the subject of the information. This message is called a publication. WMQ receives the publication, recognizes the topic, and then distributes the publication to interested applications.

Specifically, WMQ publish/subscribe components include:

  • Publisher (information producer): This component generates messages (publications) under a certain subject (topic) and sends the publications to a WMQ queue manager. A publisher knows nothing about the recipient(s) of the information.
  • Figure 1. A Basic Publish/Subscribe Setup: Publish/subscribe includes publishers, subscribers, and a queue manager.
  • Subscriber (information consumer/receiver): This component registers interest in a certain topic(s) with a WMQ queue manager. It also specifies its subscription queue name; that is, the queue where it wants to receive the messages related to this topic. The subscriber knows nothing about the information publishers(s).
  • A publications router: This component passes messages from publishers to topic subscribers. Earlier WMQ versions required a broker component on top of a queue manager to enable publish/subscribe. The broker maintained topics and subscriptions, and it routed publications. In WMQ Version 7, IBM changed this approach significantly. The queue manager is now responsible for topics, subscriptions, and routing publications. Also, MQ Explorer for Version 7 allows you to create topics and subscriptions graphically.

Figure 1 shows a basic MQ publish/subscribe setup.

A publish/subscribe system can have many publishers and many subscribers, and often, two or more queue managers communicating with one another. Also, an application can be both a publisher and a subscriber.

A Publish/Subscribe Scenario

Suppose a company sends notifications to its clients via email and Short Message Service (SMS) based on information received from various information providers (say, news and weather services). If the company adds another provider (say, for entertainment), it will have to configure the new provider to write messages to both SMS and email queues. Figure 2 shows the point-to-point implementation for this use case, and Figure 3 shows the publish/subscribe implementation.


Figure 2. Point-To-Point Implementation for This Use Case: Point-to-point messaging requires sender applications to know the queue names for all receiving applications.
 
Figure 3. Publish/Subscribe Implementation for This Use Case: The email and SMS applications subscribe to the topics generated by providers and automatically receive the information.

As you can see, publish/subscribe is less complex, and adding a new provider requires less effort. When the email and SMS applications subscribe to the topics generated by providers, they automatically receive the information. The providers know nothing about the email and SMS queues.

Set Up Your Own Publish/Subscribe Use Case

In this section, you will create a publish/subscribe setup resembling part of the use case in Figure 3. You will create a publisher for the news topic and two subscriptions for the email and SMS applications using the following procedure:

  1. Create a queue manager.
  2. Create a news topic.
  3. Create subscription queues.
  4. Create subscriptions.
  5. Create Java Messaging Services (JMS)-administered objects.
  6. Write publisher and subscriber classes.
  7. Run the classes.

This tutorial uses MQ Version 7 on Windows XP. (Download a trial version from the IBM web site.) Installation on Windows is straightforward. Just keep all the default values as you go through the installation wizard, but be sure to perform the installation as a user from the administrators group.

Don’t worry if you haven’t used WMQ before. You will use the Eclipse-based WebSphere MQ Explorer, an easy GUI for administering WMQ, to perform basic definitions.

Start WebSphere MQ Explorer as follows: Start ?> All Programs ?> IBM WebSphere MQ ?> WebSphere MQ Explorer. If this is the first time you are starting MQ Explorer, a welcome screen will appear. Just close the welcome screen.

Create a Queue Manager

  1. In MQ Explorer’s Navigator view, right-click Queue Managers and then select New ?> Queue Manager. This starts the “Create Queue Manager” wizard.
  2. On Step 1, enter TestQM as the queue manager name and then click Next (see Figure 4).
  3. Click Next on the wizard screens for Steps 2 and 3. At Step 4, make sure “Create listener configured for TCP/IP” is checked and the entered port number is free (see Figure 5), and then click Finish.

Figure 4. Create Queue Manager Wizard, Step 1: Enter the queue manager name.
 
Figure 5. Create Queue Manager Wizard, Step 4: Check the port number for the queue manager’s listener.

Create a News Topic

  1. Under TestQM, right-click Topics and then select New ?> Topic to start the “New Topic” wizard.
  2. Type News.Topic as the topic name (see Figure 6). This represents the WMQ administrative object used to manage the topic. Click Next.
  3. On the topic properties page, type News in the “Topic string” field (see Figure 7). This string is used when creating JMS administered objects to refer to the News topic. Click Finish.

Figure 6. Create Topic, Step 1: Enter the topic’s administrative object name.
 
Figure 7. Create Topic, Step 2: Enter the topic’s string name.

Create Subscription Queues

Next, you will set up news publications to be delivered to Subscription queues. Create two queues for the email and SMS applications:

  1. Under TestQM, right-click Queues then select New ?> Local Queue to start the “New Local Queue” wizard.
  2. In the name field, type Email.Queue and click Finish.
  3. Repeat the above steps to create another queue called SMS.Queue.

Create Subscriptions

In order to receive a topic’s publications, subscribers have to register interest in the topic. They use subscriptions to accomplish this. You can can create subscriptions programmatically or through MQ Explorer. Through the following steps, you will create two subscriptions using MQ Explorer:

  1. Under TestQM, right-click Subscriptions and then select New ?> Subscription to start the “New Subscription” wizard.
  2. In the name field, type EmailSubscription and click Next (see Figure 8).

  3. In the “Change properties” page, press the Select button next to the “Topic name” and then select News.Topic from the displayed topics list. Press OK. On the same page, type Email.Queue in the destination name field (see Figure 9). Click Finish.

  4. Figure 8. Create Subscription, Step 1: Enter the subscription name.
     
    Figure 9. Create Subscription, Step 2: Select topic and enter subscription queue.

  5. Repeat the above steps to create another subscription called SMSSubscription with the topic set to News.Topic and the destination name set to SMS.Queue.

A Quick Test for Your Publish/Subscribe Setup

Perform the following to verify you have correctly completed the setup for the News topic:

  1. Under TestQM, click Topics.
  2. In the right pane, right click News.Topic and select “Test Publication” to open the test window.
  3. In the message data field, type “publish/subscribe test” (or any other text), click the “Publish message” button, and then click the Close button. This should distribute the message to the subscriber queues Email.Queue and SMS.Queue.
  4. Under TestQM, click Queues.
  5. In the right pane, right click Email.Queue and select “Browse Messages.” When the browse messages window opens, search the “Message Data” column for the text you entered. You should also see the same message in SMS.Queue.
  6. Right click Email.Queue and select “Clear Messages.” Click the Clear button in the “Clear queue” window. Do the same for SMS.Queue.

Create JMS-Administered Objects

JMS applications require you to wrap vendor-specific objects in JMS-administered objects. To store JMS-administered objects, create a file-based Java Naming and Directory Interface (JNDI) namespace. Start by adding a new initial context:

  1. Right-click JMS-Administered Objects and then select Add Initial Context.
  2. On Screen 1:
    • Select “File System” for “Where is the JNDI located.”
    • For the Bindings directory, browse to C:JNDI-Directory (This directory has to exist).
    • Keep note of the factory class and provider URL; you will use them in the Java code (see Figure 10).
    • Figure 10. Add Initial Context Wizard: You will use the factory class and provider URL in the sample class.
    • Click Finish.

Next, create a JMS Connection factory and destinations under the Initial Context.

Create a Connection Factory

  1. Right-click Connection Factories and then select New ?> Connection Factory. On the first screen, type TestQMConnectionFactory in the name field and click Next (see Figure 11). You will use TestQMConnectionFactory in the JNDI lookup.
  2. Click Next on Step 2, Step 3, and Step 4.
  3. On the last page, select the connection tab (on the left) and then click the Select button to select TestQM as “Base queue manager” (See Figure 12). Click Finish.

Figure 11. Create Connection Factory, Step1: Type a factory name.
 
Figure 12. Create Connection Factory, Final Step: Select TestQM as the base queue manager.

Create Destinations

JMS destinations wrap WMQ topics and queues. Create a destination corresponding to the News topic:

  1. Right-click destinations and select New ?> Destination to start the “New Destination” wizard.
  2. In the first step, enter NewsTopic in the Name field and change the Type field to Topic (See Figure 13). Click Next.
  3. Click Next on the second screen.
  4. On the last page (Change properties), type News in the Topic field and then click Finish (See Figure 14). This is the topic string you defined when you created the News topic.

Figure 13. Create Topic Destination, First Step: Make sure you change the Type field to Topic.
 
Figure 14. Create Topic Destination, Final Step: Make sure you type News (not News.Topic).

Create two destinations corresponding to the subscriber queues:

  1. Right-click destinations and then select New ?> Destination to start the new “Destination wizard.”
  2. In the first step, type EmailQueue in the Name field and then click Next.
  3. Click Next on the second screen.
  4. On the last page, click the Select button next to the “Queue Manager” field and select TestQM from the list.
  5. Click the Select button next to the Queue field and select Email (See Figure 15). Queue from the list. Click Finish.
  6. Repeat the above steps to create another Destination called SMSQueue for SMS.Queue. Your destinations should look like the ones in Figure 16.

Figure 15. Create Queue Destination, Last Step: Select queue manager and queue.
 
Figure 16. A Third Destination Called SMSQueue for SMS.Queue: You have created three destinations.

Coding JMS Publishers and Subscribers

After you have defined your topics and subscriptions through the MQ Explorer, writing publisher and subscriber applications is easy. To publish a message, you must write a message to a topic (just as you would with a queue). Subscribers simply read messages from queues.

The downloadable source code for this article uses generic JMS interfaces, which treat queues and topics uniformly as destinations. It provides compiled classes that include a News publisher and two subscribers (corresponding to the Email and SMS applications).

The following are brief explanations of the classes.

JNDIUtil Class

This class includes methods to retrieve objects by name through JNDI lookup (see Listing 1). You will use methods in this class to retrieve references to the JMS objects you have already defined.

NewsPublisher Class

This class publishes messages to the News topic (see Listing 2). The starting point is a connection factory lookup. You use the factory to create a connection:

factory= jndiUtil.getConnectionFactory("TestQMConnectionFactory"); connection = factory.createConnection();

You use the connection object to create a session:

session = connection.createSession( transacted, Session.AUTO_ACKNOWLEDGE);

To publish messages, retrieve a News destination object, create a MessageProducer, and then send messages:

destination = jndiUtil.getDestination("NewsTopic");producer = session.createProducer(destination);TextMessage message = session.createTextMessage("No News is Good News!");producer.send(message);

EmailSubscriber and SMSSubscriber

These two classes represent the email application and the SMS application (see Listing 3 and Listing 4). Because you already defined subscriptions in MQ Explorer, the two applications simply read messages from the subscriber queues.

The code is similar to the NewsPublisher class, except that it uses a MessageConsumer to retrieve messages from queues:

consumer = session.createConsumer(destination);TextMessage iMsg = (TextMessage) consumer.receive(1000);

Compiling and Running the Sample Classes

The JAR files required to compile and run the sample classes are automatically added to the CLASSPATH environment variable when you install WMQ. The required JARs are located in C:Program FilesIBMWebSphere MQJavalib, including the JARs you need for JMS and JNDI. You just need to add the sample classes from the code download to your CLASSPATH environment variable.

To publish messages, run the NewsPublisher:

java devx.articles.pubsub.NewsPublisher

Run both subscribers after your run the NewsPublisher to retrieve messages. Each class should retrieve its own copy of the messages published by NewsPublisher:

java devx.articles.pubsub.EmailSubscriberjava devx.articles.pubsub.SMSSubscriber

You’re done. Your sample classes should compile and run successfully.

So the next time you’re designing messaging-based applications, don’t be so quick to dismiss publish/subscribe. Remember this article and how quickly you were able to get started with publish/subscribe messaging.

devx-admin

devx-admin

Share the Post:
Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1