<html>
<head>
<title>ActiveMQ Java EE MDB using a local transaction Example</title>
<link rel="stylesheet" type="text/css" href="../../common/common.css" />
<link rel="stylesheet" type="text/css" href="../../common/prettify.css" />
<script type="text/javascript" src="../../common/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<h1>Java EE MDB using a local transaction Example</h1>
<p>This example shows you how to send a message to an MDB and deliver it within a local transaction</p>
<p>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.</p>
<p>The example leverages the JBoss Arquillian framework to run a WildFly instance and deploy the MDB.</p>
<h2>Example step-by-step</h2>
<p><i>download WildFly 8.0.0.Final from <a href="http://wildfly.org/downloads/">here</a> and install.</i></p>
<p><i>set the JBOSS_HOME property to point to the WildFly install directory</i></p>
<p><i>type <code>mvn verify</code> from the example directory to run</i></p>
<ol>
<li>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 <code>jndi.properties</code> file in the directory <code>config</code></li>
<pre class="prettyprint">
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
initialContext = new InitialContext(env);
</pre>
<li>We look up the JMS queue object from JNDI</li>
<pre class="prettyprint">
Queue queue = (Queue)initialContext.lookup("jms/queues/testQueue");
</pre>
<li>We look up the JMS connection factory object from JNDI</li>
<pre class="prettyprint">
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/jms/RemoteConnectionFactory");
</pre>
<li>We create a JMS connection</li>
<pre class="prettyprint">
connection = cf.createConnection("guest", "password");
</pre>
<li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
<pre class="prettyprint">
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
</pre>
<li>We create a JMS message producer on the session. This will be used to send the messages.</li>
<pre class="prettyprint">
MessageProducer messageProducer = session.createProducer(queue);
</pre>
<li>We create a JMS text messages that we are going to send.</li>
<pre class="prettyprint">
TextMessage message = session.createTextMessage("This is a text message");
</pre>
<li>We send messages to the queue</li>
<pre class="prettyprint">
messageProducer.send(message);
</pre>
<li>The MDB receives the message<br />
We know the message is a TextMessage so we cast to it.
</li>
<pre class="prettyprint">
TextMessage tm = (TextMessage)message;
</pre>
<li>The MDB gets the text and prints it, we take a quick look at the transaction and throw an exception.
</li>
<pre class="prettyprint">
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, although the message delivery is using a local transaction");
System.out.println("let's throw an exception and see what happens");
throw new RuntimeException("DOH!");
}
}
</pre>
<li>The MDB receives the message again and we print a message.
</li>
<pre class="prettyprint">
System.out.println("The message was redelivered since the message delivery used a local transaction");
</pre>
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
<pre class="prettyprint">
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}
</pre>
</ol>
</body>
</html>