In-depth understanding of JMS (10): JMSCorrelationID and Selector

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.