如何在Java中的IBM MQ中实现逻辑排序? [英] How to implement Logical ordering in IBM MQ in Java?

查看:161
本文介绍了如何在Java中的IBM MQ中实现逻辑排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用例,将一组具有相同groupId但不同的MessageSequenceNumber的消息放入.这用于按逻辑顺序对消息进行分组,以便在接收方,接收方可以根据组顺序对所有消息进行分组.我正在关注IBM MQ v7.5知识中心页面"

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;
}

现在,我想为数组内的所有消息添加一个1唯一的groupID,并添加一个序列号(msgSeqNum)(1,2,3 ..).我如何通过JMS API做到这一点?我正在IBM IIB v8知识中心页面"

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.

推荐答案

David Currie在2006年撰写了一篇不错的IBM developerWorks博客,标题为使用WebSphere MQ Java和JMS API对消息进行分组",该博客描述了如何执行操作.问,但是看来这是最近被IBM删除的.

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.

我能够通过Google的缓存副本进行查看.以下是David在帖子中提供的信息,看来推杆逻辑比起取逻辑要容易得多.我只在此处包括放置逻辑代码,因为这是您要查询的内容.我通过电子邮件与David联系,询问是否将重新发布此博客.

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.

发送消息组

让我们先查看发送应用程序.正如刚才提到的, 放置消息选项MQPMO_LOGICAL_ORDER只是一条指令 到队列管理器以自动分配消息组 标识符和序列号.下面清单3中的示例 演示了在JMS API中缺少此选项的情况下,我们如何可以 显式设置这些属性.

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.

清单3.使用WebSphere MQ JMS API发送消息组

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();

这篇关于如何在Java中的IBM MQ中实现逻辑排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆