As mentioned earlier, JMSCorrelationID is mainly used to associate multiple messages. For example, when a Message needs to be replied
Set JMSCorrelationID to the ID of the original message. In the following example, three message producers A, B, C and three message consumers A, B, and C are created. Producer A sends A message to consumer A, and consumer A needs to send A message to consumer. B and C are similar to.
The diagram is as follows:
Producer A ----- send ----> consumer A ----- reply ------> producer
Producer B ----- send ----> consumer B ----- reply ------> producer B
Producer C ----- send ----> consumer C ----- reply ------> producer C
It should be noted that all sending and replying requests use the same Queue, which is distinguished by Selector.
Import javax. JMS .*;
Import org. Apache. activemq. activemqconnectionfactory;
Import org. Apache. activemq. Command. activemqqueue;
Public class jmscorrelationidtest {
Private queue;
Private session;
Public jmscorrelationidtest () throws jmsexception {
ActiveMQConnectionFactory factory = new
ActiveMQConnectionFactory ("vm: // localhost ");
Connection connection = factory. createConnection ();
Connection. start ();
Queue = new ActiveMQQueue ("testQueue ");
Session = connection. createSession (false,
Session. AUTO_ACKNOWLEDGE );
SetupConsumer ("ConsumerA ");
SetupConsumer ("ConsumerB ");
SetupConsumer ("ConsumerC ");
SetupProducer ("ProducerA", "ConsumerA ");
SetupProducer ("ProducerB", "ConsumerB ");
Setupproducer ("producerc", "consumerc ");
}
Private void setupconsumer (final string name) throws
Jmsexception {
// Create a consumer that only accepts messages of its own.
Messageconsumer consumer = session. createconsumer (queue,
"Cycler = '" + name + "'");
Consumer. setMessageListener (new MessageListener (){
Public void onMessage (Message m ){
Try {
MessageProducer producer =
Session. createProducer (queue );
System. out. println (name + "get:" +
(TextMessage) m). getText ());
// Reply to a message
Message replyMessage =
Session. createTextMessage ("Reply from" + name );
// Set jmscorrelationid to the ID of the message you just received
Replymessage. setjmscorrelationid (M. getjmsmessageid ());
Producer. Send (replymessage );
} Catch (jmsexception e ){}
}
});
}
Private void setupProducer (final String name, String
ConsumerName) throws JMSException {
MessageProducer producer = session. createProducer (queue );
Producer. setDeliveryMode (DeliveryMode. NON_PERSISTENT );
// Create a message and set a consumer as the consumer name.
Message message = session. createTextMessage ("Message from"
+ Name );
Message. setStringProperty ("Consumer Er", consumerName );
Producer. send (message );
// Message Waiting For reply
MessageConsumer replyConsumer =
Session. createConsumer (queue, "JMSCorrelationID = '" +
Message. getJMSMessageID () + "'");
ReplyConsumer. setMessageListener (new MessageListener (){
Public void onMessage (Message m ){
Try {
System. out. println (name + "get reply:" +
(TextMessage) m). getText ());
} Catch (JMSException e ){}
}
});
}
Public static void main (String [] args) throws Exception {
New JMSCorrelationIDTest ();
}
}
The running result is:
ConsumerA get: Message from ProducerA
ProducerA get reply: Reply from ConsumerA
ConsumerB get: Message from ProducerB
ProducerB get reply: Reply from ConsumerB
ConsumerC get: Message from ProducerC
ProducerC get reply: Reply from ConsumerC