简体   繁体   English

如何在Java中的IBM MQ中实现逻辑排序?

[英]How to implement Logical ordering in IBM MQ in Java?

We have a use case of putting a group of messages with a same groupId but differing by MessageSequenceNumber. 我们有一个用例,将一组具有相同groupId但不同的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 ". 我正在关注IBM MQ v7.5知识中心页面“ 消息组 ”。

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..) . 现在,我想向数组内的所有消息添加一个1唯一的groupID并添加一个序列号(msgSeqNum)(1,2,3 ..)。 How can i do it through the JMS API? 我如何通过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 . 我正在IBM IIB v8知识中心页面“ 在WebSphere MQ消息组中发送消息 ”中寻找代码的JMS版本。

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. David Currie于2006年撰写了一篇不错的IBM developerWorks博客,标题为“使用WebSphere MQ Java和JMS API对消息进行分组”,该博客描述了如何执行您要问的事情,但是看来这是最近被IBM删除的。

I was able to view it via Google's cached copy. 我可以通过Google的缓存副本进行查看。 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. 以下是David在帖子中提供的信息,看来推杆逻辑比起取逻辑要容易得多。 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. 我通过电子邮件与David联系,询问是否将重新发布此博客。

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. 如上所述,放置消息选项MQPMO_LOGICAL_ORDER只是对队列管理器的一条指令,用于自动分配消息组标识符和序列号。 The example in Listing 3 below demonstrates how, in the absence of this option in the JMS API, we can set these properties explicitly. 下面清单3中的示例演示了在JMS API中缺少此选项的情况下,我们如何显式设置这些属性。

Listing 3. Sending a message group using the WebSphere MQ JMS API 清单3.使用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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM