Reputation: 691
Can I filter and get a message from a queue by its CorrelationId even if that message is not the first in the queue?
Upvotes: 0
Views: 1796
Reputation: 15283
Yes. You have to use MQGMO_MATCH_CORREL_ID
matching option on MQGetMessageOptions
.
MQMessage getMsg = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.MatchOptions = MQC.MQMO_MATCH_CORREL_ID;
// Copy correlationID of the message you want to receive
getMsg.CorrelationId = correlationId;
queue.Get(getMsg, gmo);
Edit:
CorrelationId is used to relate two messages, typically a request and reply message. So it's done this way.
1) Client application sends the request message. After sending the message caches the messageId of the sent message.
2) Use this messageId as a correlationId for message selection.
recvdResponseMsg.CorrelationId = requestMsg.MessageId;
gmo.MatchOptions = MQC.MQMO_MATCH_CORREL_ID;
3) In the server application(which process the request message), when sending response message, just copy messageId of the request message to correlationId of the response message.
responseMsg.CorrelationId = requestMsg.MessageId;
Upvotes: 1