The Publish/Subscribe sample programs

The publish/subscribe sample programs demonstrate the use of the publish and subscribe features in IBM MQ .

There are three C language sample programs illustrating how to program to the IBM MQ publish/subscribe interface. There are some C samples that use older interfaces, and there are Java samples. The Java samples use the IBM MQ publish/subscribe interface in com.ibm.mq.jar and the JMS publish/subscribe interface in com.ibm.mqjms. The JMS samples are not covered in this topic.


C

Find the publisher sample amqspub in the C samples folder. Run it with any topic name you like as the first parameter, followed by an optional queue manager name. For example, amqspub mytopic QM3 . There is also a client version called amqspubc. If you choose to run the client version, first see Configure a queue manager to accept client connections on Windows, UNIX and IBM i for details.

The publisher connects to the default queue manager and responds with the output, target topic is mytopic . Every line you enter into this window from now onwards is published to mytopic .

Open another command window in the same directory, and run the subscriber program, amqssub, supplying it with the same topic name, and an optional queue manager name. For example, amqssub mytopic QM3 .

The subscriber responds with the output, Calling MQGET : 30 seconds wait time . From now onwards, lines you type into the publisher appear in the output of the subscriber.

Start another subscriber in another command window, and watch both subscribers receive publications.

For full documentation of the parameters, including setting options, refer to the sample source code. The values for the subscriber options field is described in the following topic: Options (MQLONG).

There is another subscriber sample amqssbx, which offers additional subscription options as command line switches.

Type amqssbx -d mysub -t mytopic -k to invoke the subscriber using durable subscriptions that are retained after the subscriber has terminated.

Test the subscription by publishing another item using the publisher. Wait for 30 seconds for the subscriber to terminate. Publish some more items under the same topic. Restart the subscriber. The last item published while the subscriber was not running is displayed by the subscriber immediately it is restarted.


C legacy

There is an additional set of C samples which demonstrate queued commands. Some of these samples were originally shipped as part of the MQ0C Supportpac. The capabilities the samples demonstrate are fully supported, for compatibility reasons.

We discourage you from using the queued command interface. It is much more complex than the publish/subscribe API, and there is no compelling functional reason to program complex queued commands. However, you might find the queued approach more suitable, perhaps because we are already using the interface, or because your programming environment makes it easier to build a complex message and call a generic MQPUT, rather than constructing different calls to MQSUB.

The additional samples are located in the pubsub subdirectory in the samples folder.

There are six types of sample listed in Table 1.

Category Programs Comments
RFH1

  • amqssr1a.c
  • amqspr1a.c

Simple publish/subscribe example built using RFH1 format messages.
RFH2

  • amqssr2a.c
  • amqspr2a.c

Simple publish/subscribe example built using RFH2 format messages.
MQAI samples

  • amqsppca.c
  • amqsspca.c

Simple publish/subscribe example built using PCF commands and the MQAI command interface.
MA0C Results service using RFH1

  • amqsgama.c
  • amqsresa.c

Results service built using RFH1 headers
  1. Requires the queues defined in amqsgama.tst and amqsresa.tst
  2. amqsresa must be started before amqsgama

MA0C Results service using RFH2

  • amqsgr2a.c
  • amqsrr2a.c

Results service built using RFH2 headers
  1. Requires the queues defined in amqsgama.tst and amqsresa.tst
  2. amqsresa must be started before amqsgama

Routing exit publish/subscribe sample

  • amqspsra.c

Demonstrates how to change the queue or queue manager destination for a publish/subscribe message in a routing exit.


Sample program for Java

The Java sample MQPubSubApiSample.java combines publisher and subscribers in a single program. Its source and compiled class files are found in the wmqjava samples folder.

If you choose to run in client mode, first see Configure a queue manager to accept client connections on Windows, UNIX and IBM i for details.

Run the sample from the command line using the Java command, if you have a Java environment configured. We can also run the sample from the IBM MQ Explorer Eclipse workspace that has a Java programming workbench already set up.

We might need to change some of the sample program's properties to run it. You do this by providing parameters to the JVM, or editing the source.

The instructions in Running the MQPubSubApiSample Java sample show how to run the sample from the Eclipse workspace.

Parent topic: Use the sample programs on Multiplatforms