本文介绍了如何浏览所有消息中的Websphere MQ队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览队列的整个深度时,我遇到了一个令人沮丧的问题.我了解需要在打开选项中使用MQOO_BROWSE选项打开队列.然后,在第一次读取时,使用获取消息选项MQGMO_BROWSE_FIRST进行GET.最后,后续的GET应该使用MQGMO_BROWSE_NEXT选项.

I've run into a frustrating problem browsing a queue all the way through its depth. I understood that the queue needs to be opened with the MQOO_BROWSE option among the open options. Then on the first read do a GET using the Get Messsage Option MQGMO_BROWSE_FIRST. Finally, the subsequent GET's should use the MQGMO_BROWSE_NEXT option.

问题是,我的尝试仅能检索第一条消息!在第二次GET上,即使队列中有5条消息,即使使用MQGMO_BROWSE_NEXT,该方法也会抛出MQRC_NO_MSG_AVAILABLE!

The problem is, my attempts worked only to retrieve the first message! Upon the second GET, even with MQGMO_BROWSE_NEXT, the method threw MQRC_NO_MSG_AVAILABLE, even though there were 5 messages in the queue!

这是我使用的代码:

IList<string> Messages = new List<string>();
_queueManager = new MQQueueManager(QueueManagerName);
int openOptions = MQC.MQOO_BROWSE  // open queue for browsing
_queue = QManager.AccessQueue(QueueName, openOptions);

MQGetMessageOptions mqGetMsgOpts = new MQGetMessageOptions();
mqGetMsgOpts.Options = MQC.MQGMO_BROWSE_FIRST;

MQMessage msg = new MQMessage();

_queue.Get(msg, mqGetMsgOpts);
MQGetMessageOptions mqGetNextMsgOpts = new MQGetMessageOptions();
mqGetNextMsgOpts.Options = MQC.MQGMO_BROWSE_NEXT;

try
{
    while (true)
    {
        string messageText = msg.ReadString(msg.MessageLength);
        Messages.Add(messageText);
        _queue.Get(msg, mqGetNextMsgOpts);
    }
}
catch (MQException ex)
{
    // Handle it
}

推荐答案

这很令人沮丧,但是我能够依靠在SO上发布的问题和答案来验证自己的做法是正确的.不幸的是,没有一个答案可以解决浏览器随后的GET问题,我感到很困惑.在尝试了许多毫无用处的其他途径之后,答案就来到了我,而当我终于走上这条路时,这非常简单.我决定发布有效的解决方案问答.

This was frustrating, but I was able to rely upon questions and answers posted here on SO to verify that I was on the right path. Unfortunately, none of the answers addressed subsequent GETs with browse, and I was stumped. The answer came to me after trying a number of fruitless other paths, and it was very simple when I finally came to it. I decided to post the Q&A of my solution that worked.

很明显,一旦对GET进行了GET,除非重新初始化,否则GET无法获得下一条消息,并且会立即引发MQRC_NO_MSG_AVAILABLE异常.在执行GET之前重新初始化MQMessage实例可以解决此问题.我通过在while循环中的GET之前添加所需的代码行来修改了上面的代码:

Apparently, once having done a GET into an MQMessage, unless you re-initialize it, the GET can't get the next message, and it throws the MQRC_NO_MSG_AVAILABLE exception immediately. Re-initializing the MQMessage instance before performing the GET solves the problem. I modified the code above by adding the needed line of code just before the GET in the while loop:

while (true)
{
    string messageText = msg.ReadString(msg.MessageLength);
    Messages.Add(messageText);
    msg = new MQMessage();
    _queue.Get(msg, mqGetNextMsgOpts);
}

进行了此更改后,例程将浏览队列中的所有消息.

Once I made this change, the routine browsed all the messages on the queue.

这篇关于如何浏览所有消息中的Websphere MQ队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:36