The Jmscorrelationid and selector of ACTIVEMQ

Source: Internet
Author: User

As mentioned earlier, Jmscorrelationid is primarily used to correlate multiple messages, such as when a message needs to be replied to, usually the jmscorrelationid of the reply message is set to the ID of the original message. In the following example, three message producer A,b,c and three message consumer a,b,c are created. Producer A sends a message to consumer a and needs a message from consumer A to reply to it. B, C is similar to a.
The schematic is as follows:
Producer a-----send----consumer a-----reply------) Producer A
Producer b-----send----consumer b-----reply------) Producer B
Producer C-----send----consumer C-----reply------) Producer C

It is important to note that all sends and replies use the same queue, which is differentiated by selector.
Import javax.jms.*;
Import Org.apache.activemq.ActiveMQConnectionFactory;
Import Org.apache.activemq.command.ActiveMQQueue;

public class Jmscorrelationidtest {

Private queue queue;
Private session 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 that belong to itself
Messageconsumer consumer = session.createconsumer (queue, "receiver=" + 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 property receiver for the consumer's name.
Message message = Session.createtextmessage ("message from" + name);
Message.setstringproperty ("receiver", 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 result of the operation 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

The Jmscorrelationid and selector of ACTIVEMQ

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.