1

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?

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
Vladimir
  • 11
  • 1

2 Answers2

1

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.

mqrus
  • 325
  • 2
  • 10
0

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.

T.Rob
  • 31,522
  • 9
  • 59
  • 103