Kraken
Kraken

Reputation: 24213

Configure JMS for multiple clients feeding off same queue

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

Upvotes: 0

Views: 1169

Answers (1)

Petter Nordlander
Petter Nordlander

Reputation: 22279

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.

Upvotes: 2

Related Questions