WebSphere7 MQTopic 多消息检索

发布于 2024-10-21 20:43:17 字数 967 浏览 3 评论 0原文

我设法从给定主题中检索一条消息,但是当我尝试检索另一条消息时,即使该主题上有消息,它也会失败。该代码与 IBM 提供的示例代码相同,只是 destForGet.Get(messageForGet); 处于 while 循环中,并在选项中设置了等待时间。另一个客户将消息发送到该主题,这很有效。第一条消息已收到,但其他消息均未收到。示例代码片段如下:

// bool RunThread is managed from some other thread; irrelevant for this snippet
// MQTopic destForGet is initialized earlier; irrelevant for this snippet
MQMessage messageForGet = new MQMessage();
MQGetMessageOptions mgo = new MQGetMessageOptions();
mgo.Options = MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_SYNCPOINT;
mgo.WaitInterval = 500;
string subName = "SampleSubscription";

while (RunThread)
{
    try
    {
        DestForGet.Get(messageForGet, mgo);
    }
    catch (MQException mqE)
    {
        Console.WriteLine("MQException caught. " + mqE.ToString());
    }
}

如果我在 while 循环内实例化 messageForGet,那么它可以工作,但这似乎非常无效(不断分配/释放内存)。另外,messageForGet.ClearMessage() 对此事没有帮助。有没有一种方法可以从主题中检索多条消息,而无需实例化每条单独的消息?

I manage to retrieve one message from the given topic, but when I try to retrieve another one, it just fails, even though there are messages on the topic. The code is the same as IBM provided sample code, except that destForGet.Get(messageForGet); is in a while loop with some time to wait set in the options. Another client puts messages to the topic, and that works. The first message is received, but all others are not. The sample code snippet is given hereafter:

// bool RunThread is managed from some other thread; irrelevant for this snippet
// MQTopic destForGet is initialized earlier; irrelevant for this snippet
MQMessage messageForGet = new MQMessage();
MQGetMessageOptions mgo = new MQGetMessageOptions();
mgo.Options = MQC.MQGMO_WAIT | MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_SYNCPOINT;
mgo.WaitInterval = 500;
string subName = "SampleSubscription";

while (RunThread)
{
    try
    {
        DestForGet.Get(messageForGet, mgo);
    }
    catch (MQException mqE)
    {
        Console.WriteLine("MQException caught. " + mqE.ToString());
    }
}

If I instantiate messageForGet within the while loop, then it works, but that seems to be very ineffective (to constantly allocate/deallocate memory). Also, messageForGet.ClearMessage() does not help in the matter. Is there a way to retrieve multiple messages from the topic, without instantiating each individual message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑丑阿 2024-10-28 20:43:17

“即使有关于该主题的消息”是什么意思?主题没有深度。与主题消息最接近的 MQ 是保留发布的概念。这是保留最后发布的消息的位置,以便新订阅者可以检索它。如果您发布 10 条消息然后订阅,则行为与您所描述的相符 - 将收到一条消息。

What do you mean by "even though there are messages on the topic"? Topics do not have depth. The closest MQ comes to having messages on the topic is the concept of a retained publication. This is where the last published message is retained so that it can be retrieved by a new subscriber. If you publish 10 messages and then subscribe, the behavior matches what you are describing - one message will be received.

孤云独去闲 2024-10-28 20:43:17

可能发生的情况是,第一条消息设置了消息描述符字段,其中包括消息 ID。在使用相同消息对象的后续 GET 中,MsgID 的存在会导致 WMQ 将其​​用作选择标准。

手册页MQQueue.Get 方法 指出:

此方法采用 MQMessage 对象
作为参数。它使用了一些
对象中的字段作为输入
参数,特别是
messageId 和correlationId,所以是
确保这些设置很重要
根据需要。

MQMessage.ClearMessage() 没有达到预期效果的原因是它对消息有效负载进行操作,而不是对描述符进行操作。根据手册页对于MQMessage.ClearMessage(),该方法:

丢弃消息中的所有数据
缓冲区并设置数据偏移量
归零。

我建议实例化两条消息。留下零长度有效负载和空白消息描述符。然后在每次 GET 之前将空白消息复制到工作消息对象中。或者清除 QMgr 可用作选择器的任何属性,例如消息 ID、相关 ID 等。

What is probably happening is that the first message sets the message descriptor fields, among which is the message ID. On the subsequent GET using the same message object, the presence of the MsgID causes WMQ to use it as a selection criteria.

The manual page on the MQQueue.Get method states that:

This method takes an MQMessage object
as a parameter. It uses some of the
fields in the object as input
parameters, in particular the
messageId and correlationId, so it is
important to ensure that these are set
as required.

The reason that the MQMessage.ClearMessage() does not have the desired effect is that this operates on the message payload, not the descriptor. According to the manual page for MQMessage.ClearMessage(), the method:

Discards any data in the message
buffer and sets the data offset back
to zero.

I would suggest instantiating two messages. Leave one with a zero-length payload and blank message descriptor. Then before each GET copy the blank message into the working message object. Either that or clear any properties which the QMgr can use as selectors such as Msg ID, Correlation ID, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文