# Java EE MDB using a local transaction Example

This example shows you how to send a message to an MDB and deliver it within a local transaction

The example will send deploy a simple MDB and demonstrate sending a message and the MDB consuming it, throwing an exception and the message being re delivered.

## JBoss AS configuration

Please refer to HornetQ Quickstart guide to install it in JBoss AS 5

## Example step-by-step

To deploy and start the server, simply type `./build.sh deploy` (or `build.bat deploy` on windows) from the example directory

To run the example, simply type `./build.sh` (or `build.bat` on windows) from the example directory

To remove the example profile, simply type `./build.sh undeploy` (or `build.bat undeploy` on windows) from the example directory

** make sure that JBOSS_HOME is set to the JBoss installation 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 `jndi.properties` file in the directory `config`

               initialContext = new InitialContext();

2. We look up the JMS queue object from JNDI

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

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 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 JMS message producer on the session. This will be used to send the messages.

              MessageProducer messageProducer = session.createProducer(queue);

7. We create a JMS text messages that we are going to send.

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

8. We send messages to the queue

               messageProducer.send(message);

9. The MDB receives the message
   We know the message is a TextMessage so we cast to it.

               TextMessage tm = (TextMessage)message;

10. The MDB gets the text and prints it, we take a quick look at the transaction and throw an exception.

                System.out.println("message " + text + " received");

                 if (!textMessage.getJMSRedelivered())
                 {
                    //Step 11. On first delivery get the transaction, take a look, and throw an exception
                    Transaction tx = tm.getTransaction();

                    if (tx != null)
                    {
                       System.out.println("something is wrong, there should be no global transaction: " + tx);
                    }
                    else
                    {
                       System.out.println("there is no global transaction, altho the messge delivery is using a local transaction");
                       System.out.println("lets throw an exception and see what happens");
                       throw new RuntimeException("DOH!");
                    }
                 }

11. The MDB receives the message again and we print a message.

               System.out.println("The message was redelivered since the message delivery used a local transaction");

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