<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kandy Software, Inc. &#187; atomicity</title>
	<atom:link href="http://www.kandysoftwareinc.com/tech-articles/tag/atomicity/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kandysoftwareinc.com</link>
	<description></description>
	<lastBuildDate>Thu, 20 Jan 2011 04:03:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Building reliable JMS applications</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/jms-reliable</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/jms-reliable#comments</comments>
		<pubDate>Fri, 01 Feb 2008 02:31:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JEE]]></category>
		<category><![CDATA[JMS]]></category>
		<category><![CDATA[atomicity]]></category>
		<category><![CDATA[consistency]]></category>
		<category><![CDATA[durability]]></category>
		<category><![CDATA[isolation]]></category>
		<category><![CDATA[transaction]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=99</guid>
		<description><![CDATA[Reliability is defined in terms of ACID properties. How to build a message consumer by adhering to these properties is explained. Also discussed is a method to control rollback process.]]></description>
			<content:encoded><![CDATA[<p>[print_link]</p>
<h3><strong>Introduction</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/jms-reliable.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/jms-reliable.png" alt="" title="jms-reliable" class="alignleft size-medium wp-image-116" /></a>This article assumes you have some understanding of JMS internals. If not, you might want to <a href="tech-articles/jms-internals">start here</a>. Here we describe what reliability means and how to build reliable JMS applications. To understand the source code you must be familiar with EJBs, <a href="http://xdoclet.sourceforge.net/xdoclet/index.html">XDoclet</a> and <a href="http://ant.apache.org/" target="_blank">Ant</a>. The application used in the article showcases some nifty features like message selectors, dependancy injection via Xdoclet and Ant, EJB ENC etc.</p>
<h3><strong>Reliability</strong></h3>
<p>Reliability in computer science can be summed up in four words &#8211; Atomicity, Consistency, Isolation, Durability (<ahref="http://en.wikipedia.org/wiki/ACID" target="_blank">ACID</a>). We will examine each of these properties and build a JMS application by applying them. For the purpose of demostration let&#8217;s build a MessageConsumer that receives only even numbers. We begin with a MessageDrivenBean. The source code discussed here is attached to this article.</p>
<p>We create a new class called <code>DemoBean</code> and implement <code>MessageDrivenBean</code> and <code>MessageListener</code>. We inherit EJB behavior from <code>MessageDrivenBean</code>. The tags on top of the class are <a href="http://xdoclet.sourceforge.net/xdoclet/index.html">XDoclet</a> tags which help us automatically create deployment descriptor files required by the EJB container. For more information on how to use Xdoclet <a href="http://xdoclet.sourceforge.net/xdoclet/using.html" target="_blank">please see here</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * A simple message consumer
 * 
 * @ejb.bean name=&quot;DemoBean&quot; display-name=&quot;Demo MDB to demonstrate JMS features&quot;
 *           jndi-name=&quot;ejb/Demo&quot; description=&quot;Listens to 'Demo' messages&quot;
 *           destination-type=&quot;javax.jms.Queue&quot;
 *           acknowledge-mode=&quot;Auto-acknowledge&quot;
 *           transaction-type=&quot;Container&quot;
 *           message-selector=&quot;Type='Even'&quot;
 * 
 * @ejb.env-entry name=&quot;MAX_RETRIES&quot; type=&quot;java.lang.Integer&quot;
 *                value=&quot;${max.request.retries}&quot; description=&quot;If an application
 *                error occurs, message will be rolled back to the Queue and
 *                retried this many times.&quot;
 * 
 * @jboss:destination-jndi-name name=&quot;${jndi.queue.name}&quot;
 * @websphere.mdb listenerPort=&quot;${jndi.queue.listener}&quot;
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DemoBean <span style="color: #000000; font-weight: bold;">implements</span> MessageDrivenBean, MessageListener <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>6540893609236685101L<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> MessageDrivenContext messageDrivenContext <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The following simple ant target creates all necessary boilerplate code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ejbdoclet&quot;</span> <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;Generates deployment descriptors etc.&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ejbdoclet&quot;</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;xdoclet.modules.ejb.EjbDocletTask&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;classpath</span> <span style="color: #000066;">refid</span>=<span style="color: #ff0000;">&quot;project.classpath&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/taskdef<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ejbdoclet</span> <span style="color: #000066;">ejbSpec</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> <span style="color: #000066;">destDir</span>=<span style="color: #ff0000;">&quot;${gensrc.dir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${src.dir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;**/*Bean.java&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;deploymentdescriptor</span> <span style="color: #000066;">destDir</span>=<span style="color: #ff0000;">&quot;${ejb.descriptors.dir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/deploymentdescriptor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;websphere</span> <span style="color: #000066;">destDir</span>=<span style="color: #ff0000;">&quot;${ejb.descriptors.dir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/websphere<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jboss</span> <span style="color: #000066;">Version</span>=<span style="color: #ff0000;">&quot;3.0&quot;</span> <span style="color: #000066;">destDir</span>=<span style="color: #ff0000;">&quot;${ejb.descriptors.dir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jboss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ejbdoclet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The <code>@ejb.bean</code> tag in the Java class creates the following code in ejb-jar.xml file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message-driven</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #339933;">&lt;![CDATA[Listens to 'Demo' messages]]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Demo MDB to demonstrate JMS features<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ejb-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>DemoBean<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ejb-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ejb-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.ksi.demo.ejb.DemoBean<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ejb-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;transaction-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Container<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/transaction-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message-selector<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #339933;">&lt;![CDATA[Type='Even']]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/message-selector<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;acknowledge-mode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Auto-acknowledge<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/acknowledge-mode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message-driven-destination<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;destination-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.jms.Queue<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/destination-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/message-driven-destination<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;env-entry<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #339933;">&lt;![CDATA[If an application error occurs, message will be rolled back to the Queue and retried this many times.]]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;env-entry-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MAX_RETRIES<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/env-entry-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;env-entry-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java.lang.Integer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/env-entry-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;env-entry-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #339933;">&lt;![CDATA[4]]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/env-entry-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/env-entry<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/message-driven<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The <code>transaction-type</code> tag in the ejb-jar file lets the EJB container start and manage <i>transaction</i>s for us. The <code>message-selector</code> tag lets the Connection know that only messages assigned a property &#8216;Type&#8217; and its value &#8216;Even&#8217; be sent to this bean. The tag <code>subscription-durability</code> tells the JMS Container to persist the messages in case of a container failure. In addition to these we create an variable (available only in the ENC of this bean) called MAX_RETRIES. </p>
<p>As you may have noticed the values of MAX_RETRIES, jboss destination-jndi-name and websphere.mdb listenerPort are enclosed in ${}. These are properties defined in <code>build.properties</code> available to Ant. Ant populates these to generate ejb-jar file. We inject this depedancy at run time. We will discuss MAX_RETRIES later. Since we implement MessageListener, we inherit onMessage() method. When a message arrives in a queue, the JMS container invokes the <code>onMessage()</code> of MDB to pass the message object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * This is called when there is a message in the Queue.
 * 
 * @ejb.transaction type=&quot;Required&quot;
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMessage<span style="color: #009900;">&#40;</span>Message msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Message:&quot;</span> <span style="color: #339933;">+</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		MapMessage message <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>MapMessage<span style="color: #009900;">&#41;</span> msg<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Enumeration</span> e <span style="color: #339933;">=</span> message.<span style="color: #006633;">getMapNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Hashtable</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Hashtable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">hasMoreElements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> e.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span> val <span style="color: #339933;">=</span> message.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			data.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>key, val<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Since the client posts a MapMessage, we cast the generic Message interface to MapMessage and retrieve data stored in the map. This is the basic work flow of a JMS application. You can run ant, deploy the EAR file in JBoss or WebSphere to test this behavior.</p>
<h4><strong>Atomicity</strong></h4>
<p>Atomicity refers to the ability of JMS application to gaurantee that all tasks within a <i>transaction</i> are performed or none at all. A <i>transaction</i> is a group of tasks that run as one single unit of work. In other words all the tasks are performed successfully or none at all. So when a failure occurs (OutOfMEmoryErrors, connection failures etc.) tasks in a transaction are undone from where the failure occurred. In this scenario, tasks that need to be performed are the following:</p>
<ol>
<li>Receive message from the queue</li>
<li>Print the payload in message (the even number)</li>
<li>Acknowledge that message was received and that all processing was done</li>
</ol>
<p>For our bean to be reliable, we require <b>all</b> these tasks to be performed. In other words, message needs to be processed <b>atomically</b>. When a failure occurs while message is being processed, we would like to <i>put</i> the message back in the queue so it can be picked up again for processing (hopefully after the cause of the failure has been fixed). This process is called a <i>rollback</i>. There are two ways a transaction can be performed in a EJB container:</p>
<ul>
<li>by the MDB (Bean Managed Transaction)</li>
<li>by the container (Container Managed Transaction)</li>
</ul>
<p>In Bean Managed Transaction, as the name suggests, the bean is responsible for the three tasks. However, message receipt (first task) that causes the bean to be invoked is not part of the transaction. An example of bean managed transaction.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onMessage<span style="color: #009900;">&#40;</span>Message msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	UserTransaction ut <span style="color: #339933;">=</span> messageDrivenContext.<span style="color: #006633;">getUserTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		ut.<span style="color: #006633;">begin</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Message:&quot;</span> <span style="color: #339933;">+</span> msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		MapMessage message <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>MapMessage<span style="color: #009900;">&#41;</span> msg<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">Enumeration</span> e <span style="color: #339933;">=</span> message.<span style="color: #006633;">getMapNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Hashtable</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Hashtable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">hasMoreElements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> e.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span> val <span style="color: #339933;">=</span> message.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			data.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>key, val<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		ut.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			ut.<span style="color: #006633;">rollback</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SystemException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> EJBException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rollback failed&quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> EJBException <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Transaction failed: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The following picture shows demarcation of a baen managed transaction.</p>
<table border="0">
<tr>
<td>
<a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/bmt.PNG" title="Bean Managed Transaction demarcation"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/bmt.PNG" border="0" alt="Bean Managed Transaction demarcation" /></a>
</td>
</tr>
<tr>
<td>
<center><i>Figure showing boundaries of a bean managed transaction</i></center>
</td>
</tr>
</table>
<p>Since this is not what we want, we request the EJB container to manage transactions for us. The <code>transaction-type</code> tag in the ejb-jar file does exactly this. The EJB container&#8217;s transaction manager gaurantees that message will be delivered to the MDB, and when the processing is done, automatically acknowledges the JMS container which then discards the message. If for some reason an acknowledgement is not sent, the JMS container resends the message to the MDB.</p>
<table border="0">
<tr>
<td>
<a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/cmt.PNG" title="Container Managed Transaction demarcation"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/cmt.PNG" border="0" alt="Container Managed Transaction demarcation" />
</td>
</tr>
<tr>
<td>
<center><i>Figure showing boundaries of a container managed transaction</i></center>
</td>
</tr>
</table>
<p>Hence the three steps are executed as one unit of work or in other words atomically.</p>
<h4><strong>Consistency</strong></h4>
<p>Consistency refers to the property of an application being in a legal state when the transaction begins and when it ends. This means that a transaction moves the state of entities from one consistent state to another. For an application to be reliable, when a problem occurs the application needs to revert to the last consistent state. In this DemoBean, lets simulate a failure condition by throwing an exception at random.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>122
123
124
125
126
127
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Randomly throw an exception so that the message will be rolled</span>
<span style="color: #666666; font-style: italic;">// back.</span>
<span style="color: #003399;">Random</span> random <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> JMSException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Something bad happened!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>There are two things we can do to revert to a previous consistent state: (a) re-throw the exception to let the transaction manager know that there has been a problem or (b) do not acknowledge the message. Re-throwing an exception automatically rolls the transaction back which puts the message back in the queue. This is the advantage of using an MDB. However, if the problem persists, we keep getting into the same failure scenario. So this does not help a lot. We want to be able to control the rollback process so that the same message is not tried repeatedly draining the resources. For this reason we write a method to control the rollback process:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>130
131
132
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JMSException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		rollback<span style="color: #009900;">&#40;</span>msg, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Inside the <code>rollback()</code> method we check for Delivery Count in the header field. In JBoss this field is <code>JMS_JBOSS_REDELIVERY_COUNT</code> and in WebSphere this is <code>JMSXDeliveryCount</code>. If the message has been delivered more than MAX_RETRIES times, instead of rolling the message back we send an email to a human agent. MAX_RETRIES is a variable available in the Environment Naming Context of this bean. Is is injected at build time using Ant.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">InitialContext</span> context <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InitialContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		maxRetries <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;java:comp/env/MAX_RETRIES&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NamingException</span> e2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e2.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>message.<span style="color: #006633;">propertyExists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JMSXDeliveryCount&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			retryAttempt <span style="color: #339933;">=</span> message.<span style="color: #006633;">getIntProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JMSXDeliveryCount&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JMSException e2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> messageID <span style="color: #339933;">=</span> message.<span style="color: #006633;">getJMSMessageID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>retryAttempt <span style="color: #339933;">&lt;</span> maxRetries.<span style="color: #006633;">intValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			messageDrivenContext.<span style="color: #006633;">setRollbackOnly</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Message &quot;</span> <span style="color: #339933;">+</span> messageID <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; rolledback due to &quot;</span> <span style="color: #339933;">+</span> cause.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			TextMessage textMessage <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>TextMessage<span style="color: #009900;">&#41;</span> message<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Since the exception is processed gracefully, the transaction manager correctly assumes that the message processing was successful and automatically acknowledges the JMS container.</p>
<h4><strong>Isolation</strong></h4>
<p>Isolation refers to the ability of the application to make operations in a transaction appear isolated from all other operations. According to the EJB Specification version 2.1, transactions not only make completion of a unit of work atomic, but they also isolate the units of work from each other, provided that the system allows concurrent execution of multiple units of work. Hence we are guranteed that an invocation of <code>onMessage()</code> method has no impact on invocation of <code>onMessage()</code> on another MDB instance. Note that an EJB container allows many instances of a message-driven bean class to be executing concurrently, thus allowing for the concurrent processing of a stream of messages. The <i>isolation level</i> describes the degree to which the access to a resource manager by a transaction is isolated from the access to the resource manager by other concurrently executing transactions. For more information on isolation levels please see <a href="http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction7.html" target="_blank">Sun&#8217;s J2EE tutorial</a>.</p>
<h4><strong>Durability</strong></h4>
<p>Durability refers to the guarantee that a successful transaction will persist and can not be undone. JMS supports two modes of message delivery.</p>
<ol>
<li>The NON_PERSISTENT mode is the lowest-overhead delivery mode because it does not require that the message be stored in a stable storage. A JMS provider failure can cause a NON_PERSISTENT message to be lost.</li>
<li>The PERSISTENT mode instructs the JMS container to take extra care to insure the message is not lost in transit due to a JMS provider failure.</li>
</ol>
<p>Delivery mode is set to PERSISTENT by default. JMS providers never produce duplicate messages. This means that a producer that produces a message can rely on the JMS container to insure that consumers of the message (MDB) will receive it only once. No client error can cause a provider to duplicate a message. </p>
<h3><strong>Attachments</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/02/jmsdemo.zip" title="Source code">Source code</a><br />
Unzip and run <code>ant</code> in the project&#8217;s root folder.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/jms-reliable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

