JMS Temporary Queue Example

This example shows you how to use a TemporaryQueue with HornetQ. First a temporary queue is created to send and receive a message and then deleted. Then another temporary queue is created and used after its connection is closed to illustrate its scope.

A TemporaryQueue is a JMS queue that exists only within the lifetime of its connection. It is often used in request-reply type messaging where the reply is sent through a temporary destination. The temporary queue is often created as a server resource, so after using, the user should call delete() method to release the resources. Please consult the JMS 1.1 specification for full details.

Example step-by-step

To run the example, simply type mvn verify from this directory

  1. First we need to get an initial context so we can look-up the JMS connection factory from JNDI. This initial context will get it's properties from the client-jndi.properties file in the directory ../common/config
  2.            initialContext = getContext();
            
  3. We look-up JMS connection factory from JNDI
  4.            ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
            
  5. We Create a JMS connection
  6.            connection = cf.createConnection();
            
  7. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  8.            connection.start();
            
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  11. We create a Temporary Queue
  12.            Queue tempQueue = session.createTemporaryQueue();
            
  13. We create a JMS message producer to the temporary queue. This will be used to send the messages.
  14. 	        MessageProducer messageProducer = session.createProducer(tempQueue);
            
  15. We create a JMS text message to send
  16.            TextMessage message = session.createTextMessage("This is a text message");
            
  17. We send the message to the temporary queue
  18.            messageProducer.send(message);
            
  19. We create a message consumer of the temporary queue
  20.            MessageConsumer messageConsumer = session.createConsumer(tempQueue);
            
  21. We receive the message from the temporary queue
  22.            message = (TextMessage) messageConsumer.receive(5000);
            
  23. We close the consumer and producer before destroying the temporary queue
  24.            messageConsumer.close();
               messageProducer.close();
            
  25. We delete the temporary queue
  26.            tempQueue.delete();
            
  27. We create another temporary queue
  28.            TemporaryQueue tempQueue2 = session.createTemporaryQueue();
            
  29. We close the connection
  30.            connection.close();
            
  31. We create a new connection
  32.            connection = cf.createConnection();
            
  33. We create a new session
  34.            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  35. We try to access the tempQueue2 outside its lifetime, this will cause exception thrown
  36.            
             try
             {
                messageConsumer = session.createConsumer(tempQueue2);
                throw new Exception("Temporary queue cannot be accessed outside its lifecycle!");
             }
             catch (InvalidDestinationException e)
             {
                System.out.println("Exception got when trying to access a temp queue outside its scope: " + e);
             }
               
            
  37. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  38.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }