问题
I have used wmqjava sample program to connect to MQ manager and put message in REQUEST queue and get response from RESPONSE queue.
I can put and get the messages. I couldn't get the correct message when there is muliple messages arrived in RESPONSE QUEUE.
Please advice whether there is a way to correlate request and response in this approach.
Please also advice whether this approach is recommended one.
回答1:
Correlation id is the best way to correlate request and response messages. You will need to do the following:
At the requester end:
1) Cache the message id of the request message after the request message is sent.
2) While getting the message specify MQMO_MATCH_CORREL_ID in MQGetMessageOptions.matchOptions field and specify the above message id as the matching value.
At the responder end:
1) Retrieve the message id from the request message.
2) While sending the response message, set the Correlation Id of the response message with message id of request message.
This is how you can correlate request and response messages. Little snippet for requester.
// Send request message
MQMessage requestMessage = new MQMessage();
requestMessage.writeUTF("Request Message");
reqQueue.put(requestMessage);
// Receive response
MQMessage responseMessage = new MQMessage();
responseMessage.correlationId = requestMessage.messageId;
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.matchOptions = CMQC.MQMO_MATCH_CORREL_ID;
respQueue.get(responseMessage,gmo);
来源:https://stackoverflow.com/questions/14607195/issue-in-correlating-request-message-to-resp-message-in-java-client-to-access-mq