1

We have a use case of putting a group of messages with a same groupId but differing by MessageSequenceNumber. This is used to group the messages for a logical ordering so that on the receiver side, receiver can group all of the messages based on group order. I was following the IBM MQ v7.5 Knowledge Center page "Message groups".

I have a code written to put the messages : -

public boolean writeMessage(String[] messages, String queueName) {          

                Session session = getQueueConnection().createSession(true,
                    Session.AUTO_ACKNOWLEDGE);


            Destination destination = session.createQueue(queueName);
            messageProducer = session.createProducer(destination);
            for (int i = 0; i < messages.length; i++) {
                TextMessage message = session.createTextMessage(messages[i]);
                messageProducer.send(message);
            }

            // Commit the send (Actually put messages to Queue)
            session.commit();
            return true;
}

Now , I want to add a 1 unique groupID to all the messages which are inside an array and add a sequence number(msgSeqNum) (1,2,3..) . How can i do it through the JMS API? I am looking for JMS version of the code on the IBM IIB v8 Knowledge center page "Sending messages in a WebSphere MQ message group.

JoshMc
  • 10,239
  • 2
  • 19
  • 38
Neer1009
  • 304
  • 1
  • 5
  • 18
  • 1
    What is your question? – geneSummons Sep 15 '17 at 15:34
  • i have edited the question. did you understand now ? – Neer1009 Sep 15 '17 at 15:59
  • Have you seen this: https://stackoverflow.com/questions/7915500/message-groups-in-websphere-mq ? I think it means you are looking to leverage "proprietary functionality" that is dependent on the WebSphere MQ implementation of JMS, not standard JMS. If you are using a WebSphere MQ implementation of JMS, you should be able to do what you want to do. If not, you probably have to roll-your-own solution for the functionality you require. – geneSummons Sep 15 '17 at 17:28
  • @geneSummons what you were referring is getter program to filter based on groupId. I was referring to putter program to achieve it. – Neer1009 Sep 16 '17 at 03:38

1 Answers1

3

There was a good IBM developerWorks blog written by David Currie in 2006 titled "Grouping messages using the WebSphere MQ Java and JMS APIs" that described how to do what you are asking, however it appears this was recently removed by IBM.


Wayback Machine link to "Grouping messages using the WebSphere MQ Java and JMS APIs"


Below is the information that was provided by David in the post, it appears the putting logic is much simpler to implement compared to the getting logic. I am only including the putting logic code here since this is what you inquired about. I reached out to David via email to ask if this blog will be republished.

Sending a message group

Let's start by looking at the sending application. As mentioned above, the put message option MQPMO_LOGICAL_ORDER was simply an instruction to the queue manager to automatically allocate message group identifiers and sequence numbers. The example in Listing 3 below demonstrates how, in the absence of this option in the JMS API, we can set these properties explicitly.

Listing 3. Sending a message group using the WebSphere MQ JMS API

MQConnectionFactory factory = new MQConnectionFactory();
factory.setQueueManager("QM_host")
MQQueue destination = new MQQueue("default");
destination.setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ);
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(destination);
 
String groupId = "ID:" + new BigInteger(24 * 8, new Random()).toString(16);
 
for (int i = 1; i <= 5; i++) {
 
    TextMessage message = session.createTextMessage();
    message.setStringProperty("JMSXGroupID", groupId);
    message.setIntProperty("JMSXGroupSeq", i);
 
    if (i == 5) {
        message.setBooleanProperty("JMS_IBM_Last_Msg_In_Group", true);
    }
 
    message.setText("Message " + i);
    producer.send(message);
 
}
 
connection.close();
JoshMc
  • 10,239
  • 2
  • 19
  • 38
  • thanks . this is what i was looking for. Let me also know if David replies back with the new link of his blog. – Neer1009 Sep 16 '17 at 03:41
  • Glad that it helps. Note that if you google the Blog Title you can probably get the full cached copy as well and save it off. I'll update this if I get info on a new location. – JoshMc Sep 16 '17 at 03:44
  • Not sure about the reason behind this issue. Some times in our code , this code ( String groupId = "ID:" + new BigInteger(24 * 8, new Random()).toString(16); ) is generating 47 chars in place of 48 chars. Therefore, it is breaking with this error :- com.ibm.msg.client.jms.DetailedJMSException: JMSCMQ1044: String is not a valid hexadecimal number - 'e3c06a7f3c833d68385d43f6f0f0dcd7c8ec8152a2606ab'. – Neer1009 Sep 26 '17 at 18:35
  • @Neer1009 I'm not sure why that is, is it possible that the left most hex number should be "0e" and the method being used is outputting this with put padding the 0? I'm not a java developer but could you wrap this in something that would pad with a 0 if less than 48 characters? – JoshMc Sep 27 '17 at 00:32
  • You could also look for other methods to come up with unique 48 character hex strings. I searched on Stackexchange and found the answer from @javaPlease42 to the question "[java- how to generate a random hexadecimal value within specified range of values](https://stackoverflow.com/questions/11094823/java-how-to-generate-a-random-hexadecimal-value-within-specified-range-of-value/20123992#20123992)" promising. – JoshMc Sep 27 '17 at 04:56
  • I found a version of the blog post on [Wayback Machine](https://web.archive.org/web/20150425043049/http://www.ibm.com/developerworks/websphere/library/techarticles/0602_currie/0602_currie.html) – darkmnemic Oct 21 '21 at 15:53
  • @darkmnemic Thank you, I added the link in the answer. – JoshMc Oct 25 '21 at 07:37