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