Writing subscriber applications

Get started with writing subscriber applications by studying three examples: an IBM® MQ application consuming messages from a queue, an application that creates a subscription and requires no knowledge of queuing, and finally an example that uses both queuing and subscriptions.

In Table 1 the three styles of consumer or subscriber are listed, together with the sequences of IBM MQ function calls that characterize them.
  1. The first style, MQ Publication Consumer, is identical to a point to point MQ program that only does MQGET. The application has no knowledge that it is consuming publications - it is simply reading messages from a queue. The subscription that causes publications to get routed to the queue is created administratively using IBM MQ Explorer or a command.
  2. The second style is the preferred pattern for most subscriber applications. The subscriber application creates the subscription, and then gets publications. The queue management is all performed by the queue manager.
  3. In the third style, the subscriber application elects to open and close the underlying queue that is used for publications as well as issue subscriptions to fill the queue with publications.

One way to understand these styles is to study the example C programs listed in Table 1 for each of the styles. The examples are designed to be run in conjunction with the publisher example found in Writing publisher applications.

Table 1. Point to point vs. subscribe IBM MQ program patterns.
Step MQ message consumer Example 1: MQ Publication consumer Example 2: Managed MQ subscriber Example 3: Unmanaged MQ subscriber
Connect to a queue manager MQCONN MQCONN MQCONN MQCONN
Open queue MQOPEN MQOPEN   MQOPEN
Subscribe     MQSUB MQSUB
Get message(s) MQGET MQGET MQGET MQGET
Close queue MQCLOSE MQCLOSE (MQCLOSE) MQCLOSE
Close subscription     MQCLOSE MQCLOSE
Disconnect from queue manager MQDISC MQDISC MQDISC MQDISC
Using MQCLOSE is always optional, either to release resources, pass MQCLOSE options, or just for symmetry with MQOPEN. Since you are unlikely to need to specify the MQCLOSE options when the subscription queue is closed in the Managed MQ subscriber case, and the symmetry argument is not relevant, the subscription queue is not explicitly closed in Example 2: Managed MQ subscriber.

Another way to understand publish/subscribe application patterns is too look at the interactions between the different entities involved. Lifeline, or UML sequence diagrams are a good way to study interactions. Three lifeline examples are described in Publish/subscribe lifecycles.