Activemq of Temporaryqueue and Temporarytopic

Source: Internet
Author: User

Temporaryqueue and temporarytopic, literally, can be seen as "temporary" destinations. Can be created by session, for example:
Temporaryqueue replyqueue = Session.createtemporaryqueue ();

Although they are created by the session, their lifetime is indeed the whole connection. If you create two sessions on a connection, a session creates a temporaryqueue or
Temporarytopic can also be accessed by another session. If these two sessions are created by different connection, then the Temporaryqueue created by a session cannot be accessed by another session.
In addition, their main function is to specify the destination of the reply, that is, as Jmsreplyto.
In the following example, a connection is created, then two sessions are created, one session creates a temporaryqueue, and the other session reads the message on this temporaryqueue.
Import javax.jms.*;
Import Org.apache.activemq.ActiveMQConnectionFactory;
Import Org.apache.activemq.command.ActiveMQQueue;

public class Temporaryqueuetest {
public static void Main (string[] args) throws Exception {
Activemqconnectionfactory factory = new Activemqconnectionfactory ("Vm://localhost");
Connection Connection = Factory.createconnection ();
Connection.start ();

Queue queue = new Activemqqueue ("TestQueue2");
Final session session = Connection.createsession (false, Session.auto_acknowledge);
Use the session to create a temporaryqueue.
Temporaryqueue replyqueue = Session.createtemporaryqueue ();

Receives the message and replies to the specified queue (that is, replyqueue)
Messageconsumer Comsumer = session.createconsumer (queue);
Comsumer.setmessagelistener (New MessageListener () {
public void OnMessage (Message m) {
try {
System.out.println ("Get Message:" + ((TextMessage) m). GetText ());
MessageProducer producer = Session.createproducer (M.getjmsreplyto ());
Producer.send (Session.createtextmessage ("Replymessage"));
} catch (JMSException e) {}
}
});

Use the same connection to create another session to read the message on the Replyqueue.
Session Session2 = Connection.createsession (true, Session.auto_acknowledge);
Messageconsumer Replycomsumer = Session2.createconsumer (replyqueue);
Replycomsumer.setmessagelistener (New MessageListener () {
public void OnMessage (Message m) {
try {
System.out.println ("Get reply:" + ((TextMessage) m). GetText ());
} catch (JMSException e) {}
}
});
MessageProducer producer = Session.createproducer (queue);
TextMessage message = Session.createtextmessage ("Simplemessage");
Message.setjmsreplyto (Replyqueue);
Producer.send (message);
}
}

The result of the operation is:
Get Message:simplemessage
Get Reply:replymessage

If you will:
Session Session2 = Connection.createsession (true, Session.auto_acknowledge);
Change to:
Connection Connection2 = Factory.createconnection ();
Session Session2 = Connection2.createsession (true, Session.auto_acknowledge);
You will get an exception similar to the following:
Exception in thread "main" Javax.jms.InvalidDestinationException:Cannot use a temporary destination from another connecti On

Activemq of Temporaryqueue and Temporarytopic

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.