# JMS Transactional Session Example

This example shows you how to use a transacted Session with HornetQ.

Firstly 2 messages are sent via the transacted sending session before being committed. This ensures that both message are sent

Secondly the receiving session receives the messages firstly demonstrating a message being redelivered after the session being rolled back and then acknowledging receipt of the messages via the commit method.

## 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 and destination objects 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 the JMS queue object from JNDI

               Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");

3. We look-up the JMS connection factory object from JNDI

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

4. We create a JMS connection

               connection = cf.createConnection();

5. 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();

6. We create a JMS session. The session is created as transacted.

               Session session = connection.createSession(true, Session.SESSION_TRANSACTED);

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

    	   MessageProducer messageProducer = session.createProducer(queue);

8. We create a message consumer

    	   MessageConsumer messageConsumer = session.createConsumer(queue);

9. We create 2 text messages

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

10. We send the text messages to the queue

               messageProducer.send(message1);
               messageProducer.send(message2);

11. We receive the message, it will return null as the transaction is not committed.

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

12. We commit the session

               session.commit();

13. We receive the messages again

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

14. We roll back the session, this will cause the received message canceled and redelivered again

               session.rollback();

15. We receive the message again, we will get two messages. Nothing more, nothing less

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

16. We commit the session

               session.commit();

17. We receive the message again. Nothing should be received

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

18. 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();
                  }
               }