Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jms correlation id

Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Could anyone please advise a newbie as to the recommended way to use correlation id-s ? I considered several solutions, but wanted to know what's the standard & effiecinet way. Thanx in advance.

I'm attempting to send a message (call it reqMsg) into one queue, and listen for an a-synchronious reply (replyMsg) arriving into another queue.
I was hoping to rely on correlation id's, receiver & selector. However i was having difficulty with the following:

(1) Is the correlationId on the reply supposed to be equal to the original request's *messageid* or *correlation id* ?
Namely, is it:

(2) If it's the former (reply.correlationId=req.messageId), then it appears one may end up with a race-condition. Namely, I was hoping to do something on the lines of:

// On application startup, create & start 2 global connection:
QueueConnection reqCon= ... // connection to "outgoing requests" queue
QueueConnection replyCon= ... // connection to "incoming replies" queue
reqCon.start(); // single connection for

// On user-specific thread, post an request & register listener for reply.
// First send request:
QueueSession reqSession = reqCon.createQueueSession(...)
Queue reqQueue= reqSession.createQueue(...);
QueueSender sender= session.createSender(reqQueue);
TextMessage reqMsg= session.createTextMessage();

// Then register listener for reply, on another queue:
QueueSession replySession = replyCon.createQueueSession(...)
Queue replyQueue= replySession.createQueue(...);
QueueReceiver receiver = replySession.createReceiver(replyQueue, reqMsg.getJmsMessageId() );

And here's the problem. The selector needs to know the messageId of the original reqMsg. But this id is only assinged *after* sending (beforehand, message id is null). Thus I have a race condition: the receiver may miss the reply, if it after sending reqMsg but before receiver is registered.
Is there a simple solution ? Or is it customary to open a new connection each time (which would solve the problem since you can start it only after registering listener) ?

Thanks a lot
Ade Barkah
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both solutions are commonly used.

For a simple request/response, correlating to the messageId works perfectly fine. There is no race condition: any response will simply remain queued until the client registers with the matching selector.

If you need to chain multiple request/response sequences together (as part of a "conversation") then using a separate correlationId makes more sense.

-Ade Barkah
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic