问题
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.
回答1:
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.
I was able to view it via Google's cached copy. 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();
来源:https://stackoverflow.com/questions/46242936/how-to-implement-logical-ordering-in-ibm-mq-in-java