In-depth understanding of JMS (11): temporaryqueue and temporarytopic

Source: Internet
Author: User

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.

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.