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