IBM MQ Family

IBM MQ Little Gem #35: Grouped subscriptions

By Morag Hughson posted 08-28-2018 19:14

  
This is part of a series of small blog posts which will cover some of the smaller, perhaps less likely to be noticed, features of IBM MQ. Read other posts in this series.

You're probably familiar with the basic principals of IBM MQ Publish/Subscribe. An application (or administrator) creates a subscription and can request that all the publication messages sent to that subscription go to a particular queue. An application then reads that queue to receive the messages.

It is also possible to have more than one subscription using the same destination queue. This makes a lot of sense, an application may well be interested in publication messages from a number of different topics but there is no need to make the message consuming part of the application more complicated by having more than one queue to consume from, so all the messages for one consumer can be directed to the same subscription destination queue.

This post is going to look at a specific case of multiple subscriptions using the same queue, the case where the subscriptions are made to topics which overlap.

Imagine subscriptions to the following two wild carded topics:

News/Sports/#/Liverpool
News/#/London/#

Both subscriptions are directed to the same destination queue.

Now imagine a publication with the results from a football match sent to topic:

News/Sports/London/Chelsea/Liverpool

The above two subscriptions would both match this publication and so two copies of it would be sent to the destination queue.

Grouped_Subscriptions.jpgYou can avoid getting two copies of this publication by grouping together the two subscriptions in the following way.

MQSD SubDesc1 = {MQSD_DEFAULT};
MQSD SubDesc2 = {MQSD_DEFAULT};

MQOPEN(hConn,
       &od,
       OpenOptions,
       &hObj,
       &CompCode,
       &Reason);

SubDesc1.Options = MQSO_CREATE
                 | MQSO_DURABLE
                 | MQSO_GROUP_SUB     /* This is a group subscription */
                 | MQSO_WILDCARD_TOPIC
                 | MQSO_SET_CORREL_ID;
SubDesc1.ObjectString.VSPtr = "News/Sports/#/Liverpool";
SubDesc1.ObjectString.VSLength = MQVS_NULL_TERMINATED;
SubDesc1.SubName.VSPtr = "Liverpool Results";
SubDesc1.SubName.VSLength = MQVS_NULL_TERMINATED;
/* This is the ID of the group */ memcpy(SubDesc1.SubCorrelId, MY_GROUP_CORREL_ID, MQ_CORREL_ID_LENGTH); MQSUB(hConn, &SubDesc1, &hObj, /* Queue opened above */ &hSub1, &CompCode, &Reason); SubDesc2.Options = MQSO_CREATE | MQSO_DURABLE | MQSO_GROUP_SUB /* This is a group subscription */ | MQSO_WILDCARD_TOPIC | MQSO_SET_CORREL_ID; SubDesc2.ObjectString.VSPtr = "News/#/London/#"; SubDesc2.ObjectString.VSLength = MQVS_NULL_TERMINATED; SubDesc2.SubName.VSPtr = "London News"; SubDesc2.SubName.VSLength = MQVS_NULL_TERMINATED; /* This is the ID of the group */
memcpy(SubDesc1.SubCorrelId, MY_GROUP_CORREL_ID, MQ_CORREL_ID_LENGTH); MQSUB(hConn, &SubDesc2, &hObj, /* Queue opened above */ &hSub1, &CompCode, &Reason);

This capability is only available through the programming API, i.e. using MQSUB, it is not available through the administrative interface, i.e. DEFINE SUB.

Sending a message to our (slightly contrived) topic string, News/Sports/London/Chelsea/Liverpool, which matches both subscriptions results in only one message being delivered to our queue:-

AMQ8409: Display Queue details.
   QUEUE(LIVERPOOL.AND.LONDON)             TYPE(QLOCAL)
   CURDEPTH(1)

And this is recorded against only one of the subscriptions (the most significant subscription in terms of wildcard matching):-

AMQ8099: WebSphere MQ subscription status inquired.
   SUB(London News)
   SUBID(414D51204D5147312020202020202020F284845B25DA9007)
   NUMMSGS(1)                              SUBTYPE(API)
   TOPICSTR(News/#/London/#)
AMQ8099: WebSphere MQ subscription status inquired.
   SUB(Liverpool Results)
   SUBID(414D51204D5147312020202020202020F284845B25DA900A)
   NUMMSGS(0)                              SUBTYPE(API)
   TOPICSTR(News/Sports/#/Liverpool)

So, if you have applications that create multiple overlapping subscriptions, you don't have to find and discard the duplicate messages in your application, you can ask IBM MQ not to send you any in the first place.


Morag Hughson is an MQ expert. She spent 18 years in the MQ Devt organisation before taking on her current job writing MQ Technical education courses with MQGem. She also blogs for MQGem. You can connect with her here on IMWUC or on Twitter and LinkedIn.


#Little-Gem
#gwc
#MQ
#ChampionsCorner
0 comments
17 views

Permalink