0

So I have request/response queues that I am putting messages on and reading messages off from.

The problem is that I have multiple local instances that are reading/feeding off the same queues, and what happens sometimes is that one instance can read some other instance's reply message.

So is there a way I can configure my JMS, using spring that actually makes the instances read the messages that are only requested by them and not read other instance's messages.

I have very little knowledge about JMS and related stuff. So if the above question needs more info then I can dig around and provide it.

Thanks

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • You may want to have a look at this: http://stackoverflow.com/questions/23342116/ibm-websphere-mq-request-reply-scenario/23359217#23359217. – Shashi Jun 13 '14 at 08:14
  • @Shashi That seems like something that I want to do. But since I don't know much about jms and related stuff, can you please tell me what should I google for if I want to know how this entire stuff works. Like, if I want to add correlation ID, or how to read message with a particular correlation ID, and how to know what the correlation ID would be put by the requesting application. One more thing, I actually want to do this based on the environments. For eg, for local development, I want to do this, but some other env say, production I don't want to. Any idea on this? – Kraken Jun 13 '14 at 08:51
  • 1
    I have added sample here: https://www.ibm.com/developerworks/community/blogs/messaging/entry/jms_request_reply_sample?lang=en – Shashi Jun 17 '14 at 09:06

1 Answers1

2

It's easy!

A JMS message have two properties you can use - JMSMessageID and JMSCorrelationID.

A JMSMessageId is supposed to be unique for each message, so you could do something like this:

Let the client send a request, then start to listen for responses where the correlation id = the sent message id. The server side is then responsible for copying the message id of the request to the correlation id of the response. Something like: responseMsg.setJMSCorrelationID(requestMsg.getJMSMessageID());

Example client side code:

Session session = getSession();
Message msg = createRequest();
MessageProducer mp = session.createProducer(session.createQueue("REQUEST.QUEUE"));
mp.send(msg,DeliveryMode.NON_PERSISTENT,0,TIMEOUT);
// If session is transactional - commit now. 
String msgID = msg.getJMSMessageID();

MessageConsumer mc = session.createConsumer(session.createQueue("REPLY.QUEUE"),
                                            "JMSCorrelationID='" + msgId + "'");
Message response = mc.receive(TIMEOUT);

A more performant solution would be to use dedicated reply queues per destination. Simply set message.setJMSReplyTo(session.createQueue("REPLY.QUEUE."+getInstanceId())); and make sure the server side sends response to requestMsg.getJMSReplyTo() and not to a hard coded value.

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84