Name

JMSCorrelationID — Purpose: Routing

Synopsis

The JMSCorrelationID provides a header for associating the current message with some previous message or application-specific ID. In most cases, the JMSCorrelationID will be used to tag a message as a reply to a previous message. The following code shows how the JMSCorrelationID is set and used along with the JMSReplyTo and JMSMessageID headers to send a reply to a message:

public void onMessage(Message message){
    try {
        TextMessage textMessage = (TextMessage)message;
        Topic replyTopic = (Topic)textMessage.getJMSReplyTo(  );
        Message replyMessage = session.createMessage(  );
        String messageID = textMessage.getJMSMessageID(  );
        replyMessage.setJMSCorrelationID(messageID);
        publisher.publish(replyTopic, replyMessage);
    } catch (JMSException jmse){jmse.printStackTrace(  );}
}

When the JMS client receives the reply message, it can match the JMSCorrelationID of the new message with the corresponding JMSMessageID of the message it sent, so that it knows which message received a reply. The JMSCorrelationID can be any value, not just a JMSMessageID. The JMSCorrelationID header is often used with application-specific identifiers. Our example in Chapter 4 uses the JMSCorrelationID as a way of identifying the sender. The important thing to remember, however, is that the JMSCorrelationID does not have to be a JMSMessageID, although it frequently is. If you decide to use your own ID, be aware that an application-specific JMSCorrelationID must not start with ...

Get Java Message Service now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.