# 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 `./build.sh` (or `build.bat` on windows) 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`

               initialContext = getContext();

2. We look-up JMS connection factory from JNDI

               ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

3. We Create a JMS connection

               connection = cf.createConnection();

4. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started

               connection.start();

5. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.

               Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

6. We create a Temporary Queue

               Queue tempQueue = session.createTemporaryQueue();

7. We create a JMS message producer to the temporary queue. This will be used to send the messages.

    	        MessageProducer messageProducer = session.createProducer(tempQueue);

8. We create a JMS text message to send

               TextMessage message = session.createTextMessage("This is a text message");

9. We send the message to the temporary queue

               messageProducer.send(message);

10. We create a message consumer of the temporary queue

               MessageConsumer messageConsumer = session.createConsumer(tempQueue);

11. We receive the message from the temporary queue

               message = (TextMessage) messageConsumer.receive(5000);

12. We close the consumer and producer before destroying the temporary queue

               messageConsumer.close();
               messageProducer.close();

13. We delete the temporary queue

               tempQueue.delete();

14. We create another temporary queue

               TemporaryQueue tempQueue2 = session.createTemporaryQueue();

15. We close the connection

               connection.close();

16. We create a new connection

               connection = cf.createConnection();

17. We create a new session

               session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

18. We try to access the tempQueue2 outside its lifetime, this will cause exception thrown

             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);
             }

19. 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

               finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }