WebSphere7 MQTopic 多消息检索
我设法从给定主题中检索一条消息,但是当我尝试检索另一条消息时,即使该主题上有消息,它也会失败。该代码与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“即使有关于该主题的消息”是什么意思?主题没有深度。与主题消息最接近的 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.
可能发生的情况是,第一条消息设置了消息描述符字段,其中包括消息 ID。在使用相同消息对象的后续 GET 中,MsgID 的存在会导致 WMQ 将其用作选择标准。
手册页
MQQueue.Get
方法 指出: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: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 forMQMessage.ClearMessage()
, the method: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.