简体   繁体   English

C#MQ Api如果在空队列的情况下获取异常,如何获取消息

[英]C# MQ Api How to fetch message without getting exception in case of empty queue

I have to periodically check messages in a queue within Websphere MQ. 我必须定期检查Websphere MQ中队列中的消息。 I haven't found better approach rather than try getting a message and handle 2033 reason code (which is NO_MSG_AVAILABLE) like this: 我没有找到更好的方法,而不是尝试获取消息并处理2033原因代码(这是NO_MSG_AVAILABLE),如下所示:

try
{
    // ...
    inQueue.Get(message);
}
catch (MQException exception)
{
    if (exception.ReasonCode != 2033)
        throw;
}

Is there better way to get message from queue? 有更好的方法从队列中获取消息吗? I think that there might be some openOptions flag that I'm not aware of, that wouldn't throw exception when no message available, but return null instead. 我认为可能有一些我不知道的openOptions标志,在没有可用消息时不会抛出异常,而是返回null。

There are three ways to avoid or reduce this polling mechanism. 有三种方法可以避免或减少这种轮询机制。 Here they are in oder of elegance(the higher the better): 在这里他们优雅(越高越好):

  1. MQGET with wait interval UNLIMITED and MQGMO_FAIL_IF_QUIESCING 具有等待间隔UNLIMITED和MQGMO_FAIL_IF_QUIESCING的MQGET
  2. Get your application be triggered by MQServer 使MQServer触发您的应用程序
  3. Callback function - new with MQ V7 on both sides 回调函数 - 两侧都带有MQ V7的新功能

You are missing the MQC.MQGMO_WAIT flag on MQGetMessageOptions.Options . 你缺少MQC.MQGMO_WAIT上标志MQGetMessageOptions.Options Change it this way: 这样改变:

getOptions = new MQGetMessageOptions {WaitInterval = MQC.MQWI_UNLIMITED, Options = MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING}

Please note that this would make the calling thread to be blocked till a message arrives at the queue or some connection exception occurs. 请注意,这将使得调用线程被阻塞,直到消息到达队列或发生某些连接异常。 MQ has another client called IBM Message Service Client (aka XMS .NET) that provides a JMS specification implementation in .NET. MQ有另一个名为IBM Message Service Client(又名XMS .NET)的客户端,它在.NET中提供JMS规范实现。 This has a nice little Message Listener which gets automatically invoked whenever a message arrives in a queue. 这有一个很好的小Message Listener ,只要消息到达队列就会自动调用它。 Unlike in the above example, the calling thread will not be blocked when Message Listener is used. 与上面的示例不同,使用Message Listener时不会阻止调用线程。

More details on XMS .NET can be found here . 有关XMS .NET的更多详细信息,请访问此处 Samples are also shipped with MQ and for message listener sample, please refer "SampleAsyncConsumer.cs" source file. 样本也附带MQ和消息监听器示例,请参阅“SampleAsyncConsumer.cs”源文件。

I was getting this. 我得到了这个。 I solved it by putting the Message initiator inside the loop: 我通过将Message initiator放在循环中来解决它:

_queueManager = new MQQueueManager(Queuemanager, _mqProperties);

MQQueue queue = _queueManager.AccessQueue(
    Queuename, 
    MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING + MQC.MQOO_INQUIRE);            

string xml = "";

while (queue.CurrentDepth > 0)
{
    MQMessage message = new MQMessage();                
    queue.Get(message);
    xml = message.ReadString(message.MessageLength);
    MsgQueue.Enqueue(xml);                    
    message.ClearMessage();                                
}

There must be something in the Message internally that errors when reusing it for another get. 内部消息中必须存在一些错误,当重新使用它时会出现错误。

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

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