IBM Support

MQ: You want to put a message into a queue and you want to generate duplicate messages into other queues

Question & Answer


Question

Scenarios that are addressed in this technote:
  1. You are using point-to-point (queues) with your putting and getting applications. You want to use IBM MQ publish/subscribe under the covers and you do not want your putting and getting applications to be aware of publish/subscribe. That is, the application that puts messages into a queue continues to put messages into a queue, and the getting applications keep getting messages from a queue.
  2. You are using point-to-point and you want to create "duplicate" messages that are stored in queues Q1 and Q2, when the message is put into the queue DESTINATION.

Answer

  • Update September 2021: If you are using IBM MQ 9.2.3 or higher, the Streaming Queue feature can be used to duplicate messages to another queue. This feature provides similar capabilities to the approach described in this technote, with much simpler setup, but with the limitation that you can create only a single copy of each message. You can read more about Streaming Queues in the IBM MQ Documentation.

The following example uses the concepts of "topic objects" and "topic alias" introduced in IBM WebSphere MQ 7.

With the setup described in the following example, you can put a message onto a queue and a copy of the message is placed on queues Q1 and Q2.

You can use the IBM MQ Explorer or the amqsput sample to put a message to the DESTINATION queue. This queue is an alias for a topic, and therefore the message is published into topic T1. This action causes the subscribers SUB1 and SUB2 to receive the messages (which are stored in queues Q1 and Q2 respectively).

Alternatively, you could publish directly into topic T1, and the messages are received in Q1 and Q2.

Use runmqsc to define the following objects:

define ql(Q1)
define ql(Q2)
define qalias(DESTINATION) target(T1) targtype(topic)
define topic(T1) topicstr('TOPIC1') usedlq(NO)
define sub(SUB1) topicstr('TOPIC1') dest(Q1)
define sub(SUB2) topicstr('TOPIC1') dest(Q2)

Note (Thanks to Tim Zielke for this feedback):
The reason for the non-default usedlq(NO) in the statement define topic is that most IBM MQ administrators would not want this duplicated message configured to spill onto the DLQ if a queue becomes full.

Example of running amqsput and amqsget:

a) Open 3 windows:

window 1: get messages from queue Q1

amqsget Q1 QMGR

window 2: get messages from queue Q2

amqsget Q2 QMGR

window 3: put a message into the queue DESTINATION

amqsput DESTINATION QMGR
Sample AMQSPUT0 start
target queue is DESTINATION
TEST
Sample AMQSPUT0 end

b) Observe that Q1 and Q2 receive the same message:

amqsget Q1 QMGR
Sample AMQSGET0 start
message <TEST>
no more messages
Sample AMQSGET0 end

amqsget Q2 QMGR
Sample AMQSGET0 start
message <TEST>
no more messages
Sample AMQSGET0 end

c) NOTE: Strictly speaking, the messages received by Q1 and Q2 are NOT identical. Even though they have the same payload (text string 'TEST') and same characteristics, each message has a unique message ID.

The following example code browses the messages by using amqsbcg Queue QMGR.

Notice that the only differences in this particular example are the last 2 characters from MsgId and CorrelId. In your case, more characters in this field could be different.

Message received by Q1:

  MsgId :    X'414D5120514D5F3730312020202020207064DF4E20003603'
                                                             **
  CorrelId : X'414D5120514D5F3730312020202020207064DF4E20001E08'
                                                             **

Message received by Q2:

  MsgId :    X'414D5120514D5F3730312020202020207064DF4E20003602'
                                                             **
  CorrelId : X'414D5120514D5F3730312020202020207064DF4E20001E0E'
                                                             **

c.1) Message received by Q1

****Message descriptor****

StrucId : 'MD ' Version : 2
Report : 0 MsgType : 8
Expiry : -1 Feedback : 0
Encoding : 546 CodedCharSetId : 437
Format : 'MQSTR '
Priority : 0 Persistence : 0
MsgId : X'414D5120514D5F3730312020202020207064DF4E20003603'
CorrelId : X'414D5120514D5F3730312020202020207064DF4E20001E08'
BackoutCount : 0
ReplyToQ : ' '
ReplyToQMgr : 'QMGR '
** Identity Context
UserIdentifier : 'rivera '
AccountingToken :
X'16010515000000BA1E06D2603C3514885EDBC2EF03000000000000000000000B'
ApplIdentityData : ' '
** Origin Context
PutApplType : '26'
PutApplName : 'QMGR '
PutDate : '20111207' PutTime : '20215396'
ApplOriginData : ' '

GroupId : X'000000000000000000000000000000000000000000000000'
MsgSeqNumber : '1'
Offset : '0'
MsgFlags : '0'
OriginalLength : '-1'

**** Message ****

length - 4 bytes

00000000: 5445 5354 'TEST '

c.2) Message received by Q2

****Message descriptor****

StrucId : 'MD ' Version : 2
Report : 0 MsgType : 8
Expiry : -1 Feedback : 0
Encoding : 546 CodedCharSetId : 437
Format : 'MQSTR '
Priority : 0 Persistence : 0
MsgId : X'414D5120514D5F3730312020202020207064DF4E20003602'
CorrelId : X'414D5120514D5F3730312020202020207064DF4E20001E0E'
BackoutCount : 0
ReplyToQ : ' '
ReplyToQMgr : 'QMGR '
** Identity Context
UserIdentifier : 'rivera '
AccountingToken :
X'16010515000000BA1E06D2603C3514885EDBC2EF03000000000000000000000B'
ApplIdentityData : ' '
** Origin Context
PutApplType : '26'
PutApplName : 'QMGR '
PutDate : '20111207' PutTime : '20215396'
ApplOriginData : ' '

GroupId : X'000000000000000000000000000000000000000000000000'
MsgSeqNumber : '1'
Offset : '0'
MsgFlags : '0'
OriginalLength : '-1'

**** Message ****

length - 4 bytes

00000000: 5445 5354 'TEST '

+++ end +++


 

[{"Line of Business":{"code":"LOB45","label":"Automation"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSYHRD","label":"IBM MQ"},"ARM Category":[{"code":"a8m0z00000008ObAAI","label":"Components and Features->Administration"}],"ARM Case Number":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Version(s)"}]

Product Synonym

WMQ MQ MQSeries

Document Information

Modified date:
02 October 2021

UID

swg21574670