JMS Transactional Session Example

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

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 mvn verify 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
  2.            initialContext = getContext();
            
  3. We look-up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");
            
  5. We look-up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  10.            connection.start();
            
  11. We create a JMS session. The session is created as transacted.
  12.            Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
            
  13. We create a JMS message producer on the session. This will be used to send the messages.
  14. 	   MessageProducer messageProducer = session.createProducer(queue);
            
  15. We create a message consumer
  16. 	   MessageConsumer messageConsumer = session.createConsumer(queue);
            
  17. We create 2 text messages
  18.            TextMessage message1 = session.createTextMessage("This is a text message1");
               TextMessage message2 = session.createTextMessage("This is a text message2");
            
  19. We send the text messages to the queue
  20.            messageProducer.send(message1);
               messageProducer.send(message2);
            
  21. We receive the message, it will return null as the transaction is not committed.
  22.            TextMessage receivedMessage = (TextMessage) messageConsumer.receive(5000);
            
  23. We commit the session
  24.            session.commit();
            
  25. We receive the messages again
  26.            receivedMessage = (TextMessage) messageConsumer.receive(5000);
            
  27. We roll back the session, this will cause the received message canceled and redelivered again
  28.  
               session.rollback();
            
  29. We receive the message again, we will get two messages. Nothing more, nothing less
  30.  
               receivedMessage = (TextMessage) messageConsumer.receive(5000);
               receivedMessage = (TextMessage) messageConsumer.receive(5000);
            
  31. We commit the session
  32.  
               session.commit();
            
  33. We receive the message again. Nothing should be received
  34.  
               receivedMessage = (TextMessage) messageConsumer.receive(5000);
            
  35. 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
  36.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }