Temporaryqueue and temporarytopic can be seen literally as "temporary" destinations. You can create a session, for example:
Temporaryqueue replyqueue = session. createtemporaryqueue ();
Although they are created by sessions, their life cycle is indeed the entire connection. If two sessions are created on one connection, the temporaryqueue or temporarytopic created by one session can also be accessed by another session. If the two sessions are created by different connections, the temporaryqueue created by one session cannot be accessed by another session.
In addition, they are used to specify the reply destination as jmsreplyto.
In the following example, a connection is created and two sessions are created. One session creates a temporaryqueue, And the other session reads messages 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 session to create a temporaryqueue.
Temporaryqueue replyqueue = session. createtemporaryqueue ();
// Receives the message and returns it to the specified Queue (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 messages on 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 running result is:
Get message: simplemessage
Get reply: replymessage
If:
Session session2 = connection. createsession (true, session. auto_acknowledge );
Changed:
Connection connection2 = factory. createconnection ();
Session session2 = connection2.createsession (true, session. auto_acknowledge );
The following exception is returned:
Exception in thread "Main" javax. JMS. invaliddestinationexception: cannot use a temporary destination from another connection.