WebSphere MQ publish/subscribe is used on MQ queues that employ a broker process to perform subscription resolution. In its simplest form:
A publisher is the application that is sending the message.
A subscriber is the application that is receiving the message.
The following queues are involved:
Control queue – where publishers and subscribers send directives to the pub/sub broker. For instance, subscriber registration and deregistration.
Stream queue – where the publisher sends its messages directly. The pub/sub broker reads the messages from the stream queue and distributes them to the appropriate subscriber’s queue.
Subscriber queue – where the subscriber reads its messages directly.
More queues can be involved, depending on the type of
publications.
The pub/sub broker responds to MQRFH messages sent to the control queue. These command messages control how the pub/sub broker processes messages that arrive on the stream queue. For instance, a subscriber can register an interest in a particular topic.
The publisher sends messages directly to the stream queue.
The pub/sub broker reads messages from the stream queue and determines the subscriber queue to which to copy the message. This depends on topics that the subscribers have registered interest in.
The subscriber reads messages directly from the subscriber queue.
Subscribers register “subscriptions,” which means it is interested in one or more “topics”.
This example, which shows the MQ pub/sub process, uses these variables:
declare @BROKER varchar(100) declare @STREAM varchar(100) declare @SUBQ varchar(100) declare @QM varchar(100) select @QM = 'ibm_mq:channel1/tcp/host1(9876)?qmgr=QM' select @BROKER = 'SYSTEM.BROKER.CONTROL.QUEUE' select @STREAM = 'ANIMALS' select @SUBQ = 'MY_ANIMALS'
Publisher registers to send publications to ANIMALS with topics on fish:
select msgsend(NULL, @QM + ',queue=' + @BROKER option 'rfhCommand=registerPublisher' message header 'topics=fish,streamName=' + @STREAM)
Subscriber registers to receive publications published to ANIMALS with topics on fish. The subscriber receives the publications on MY_ANIMALS:
select msgsend(NULL, @QM + ',queue=' + @BROKER option 'rfhCommand=registerSubscriber' message header 'topics=fish' + ',streamName=' + @STREAM + ',queueName=' + @SUBQ’)
Publisher publishes publication to ANIMALS about fish. The MQ pub/sub broker automatically forwards the publication to MY_ANIMALS:
select msgsend('something about fish', @QM + ',queue=' + @STREAM option 'rfhCommand=publish' message header 'topics=fish')
Subscriber reads the forwarded message from MY_ANIMALS:
select msgrecv(@QM + ',queue=' + @SUBQ option 'timeout=30ss')
Figure 2-1 shows the flow of the sample MQ pub/sub process.
Figure 2-1: The MQ publication/subscription process
A message can have one or more topics. WebSphere MQ pub/sub recommends that topics use a hierarchical naming convention as in the examples show below. Subscribers can specify wildcards (such as * and ?) when specifying topics of interest. These are examples of topics:
Sport Sport/Soccer Sport/Tennis
These are examples of how subscribers can specify topics of interest:
Sport/* - Any topic about sports. */Soccer - Any topics about soccer. */Soccer/Trades - Any topics about soccer where a 'trade' is involved.
A retained publication is a type of publication where the MQ pub/sub broker maintains a copy of a message even after it has delivered it to all subscribers. Normally, a publication is deleted after a copy has been delivered to all subscribers. A retained publication allows a subscriber to asynchronously request the retained publication instead of relying on it being delivered by the MQ pub/sub broker. These types of messages normally contain state information, and are also referred to as state publications.