简体   繁体   English

循环遍历IBM MQ服务器中的消息

[英]Looping over the messages in IBM MQ server

I have few hundreds of messages in my MQ server(using MQ .NET). 我的MQ服务器中有数百条消息(使用MQ .NET)。

I am trying to read those one by one, however i am having issue in looping thro' that. 我试图逐个阅读,但是我在循环中遇到了问题。 I don't have any count/length property that i can use in this regard. 我没有任何可以在这方面使用的计数/长度属性。

mqQueue - MQQueue mqQMgr - MQ QueueManager mqQueue - MQQueue mqQMgr - MQ QueueManager

   mqQueue = mqQMgr.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_BROWSE);
   mqQueue.Get(mqMsg, mqGetMsgOpts); 
   string readMessage = mqMsg.ReadString(mqMsg.MessageLength);

How can i loop thro' all the messages in the queue and if there is no message i want exit out. 我如何循环'队列中的所有消息,如果没有消息,我想退出。 Thanks in advance. 提前致谢。

Have a look at the sample programs that installed with the WMQ code. 看一下使用WMQ代码安装的示例程序。 By default, these will live in C:\\Program Files (x86)\\IBM\\WebSphere MQ 7.5\\tools\\dotnet\\samples\\cs\\base\\ and the one I think you want is SimpleGet.cs . 默认情况下,它们将存在于C:\\Program Files (x86)\\IBM\\WebSphere MQ 7.5\\tools\\dotnet\\samples\\cs\\base\\ ,我认为你想要的是SimpleGet.cs

The problem you are having is that you are opening the queue repeatedly. 您遇到的问题是您反复打开队列。 That resets the rowse pointer to the head of the queue eac time. 这会将rowse指针重置为队列eac时间的头部。 The sample program shows how to open the queue once, then loop through messages until reaching a certain number or until the queue is empty, whichever comes first. 示例程序显示了如何打开队列一次,然后循环遍历消息,直到达到某个数字或直到队列为空,以先到者为准。

            // create connection
            Console.Write("Connecting to queue manager.. ");
            queueManager = new MQQueueManager(queueManagerName, properties);
            Console.WriteLine("done");

            // accessing queue
            Console.Write("Accessing queue " + queueName + ".. ");
            queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
            Console.WriteLine("done");

            // getting messages continuously
            for (int i = 1; i <= numberOfMsgs; i++)
            {
                // creating a message object
                message = new MQMessage();

                try
                {
                    queue.Get(message);
                    Console.WriteLine("Message " + i + " got = " + message.ReadString(message.MessageLength));
                    message.ClearMessage();
                }
                catch (MQException mqe)
                {
                    if (mqe.ReasonCode == 2033)
                    {
                        Console.WriteLine("No message available");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("MQException caught: {0} - {1}", mqe.ReasonCode, mqe.Message);
                        break;
                    }
                }
            }

The IBM install media with the code samples is downloadable as SupportPac MC75 . 带有代码示例的IBM安装介质可以下载为SupportPac MC75 If for some reason you need a back-level client, they are available from the SupportPacs main page . 如果由于某种原因您需要一个后端客户端,可以从SupportPacs 主页面获取它们。 However, keep in mind that there has been a lot of engineering in the later releases and you are MUCH better off with the latest version. 但是,请记住,在以后的版本中已经有很多工程设计,并且最好使用最新版本。 Any version of MQ client can work with any version of QMgr, but obviously the functionality you get is either that only on the client side (such as the client.ini file) or on the server side, whatever that level of QMgr gives you. 任何版本的MQ客户端都可以使用任何版本的QMgr,但显然您获得的功能是仅在客户端(例如client.ini文件)或服务器端,无论QMgr提供给您什么级别。 In other words, using a V7.5 client with a v7.0 QMgr works fine, but it doesn't give you CHLAUTH rules because the v7.0 QMgr doesn't have those. 换句话说,使用带有v7.0 QMgr的V7.5客户端可以正常工作,但它没有给你CHLAUTH规则,因为v7.0 QMgr没有这些规则。

This is the line i was looking for, moving the cursor to the next message, so that i can read the next message. 这是我正在寻找的线,将光标移动到下一条消息,以便我可以读取下一条消息。

mGetMsgOpts.Options = MQC.MQGMO_WAIT | mGetMsgOpts.Options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_NEXT; MQC.MQGMO_BROWSE_NEXT;

MQGetMessageOptions: MQGetMessageOptions:

queue.Get(message);
Console.WriteLine("Message " + i + " got = " + message.ReadString(message.MessageLength));
mGetMsgOpts.Options = MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_NEXT;

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

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