<?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; JMS</title>
	<atom:link href="http://www.kandysoftwareinc.com/tech-articles/tag/jms/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>JMS Internals</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/jms-internals</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/jms-internals#comments</comments>
		<pubDate>Thu, 14 Feb 2008 02:35:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JEE]]></category>
		<category><![CDATA[JMS]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[delivery]]></category>
		<category><![CDATA[JBoss]]></category>
		<category><![CDATA[MDB]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[onMessage()]]></category>
		<category><![CDATA[queue]]></category>
		<category><![CDATA[topic]]></category>
		<category><![CDATA[Websphere]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=101</guid>
		<description><![CDATA[This article explains JMS architecture, message models, messaging domains and various components in a JMS system. It begins with the basics and explains some powerful yet not well known techniques and concepts. Techniques like pausing message delivery, selecting messages etc explained.]]></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-internals.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/jms-internals.png" alt="" title="jms-internals" width="190" height="131" class="alignleft size-medium wp-image-111" /></a>This article discusses JMS architecture and various components within a JMS system. Message models and messaging styles are explained. The article also discuses synchronous and asynchronous message consumption. </p>
<h3><strong>JMS Architecture</strong></h3>
<p>JMS (as defined in JMS specification version 1.1) is a set of interfaces and associated semantics that provides a common way for Java programs to create, send, receive and read an enterprise messaging system’s <i>messages</i>. In this context <i>messages</i> are asynchronous requests, reports or events that are consumed by enterprise applications, not humans. They contain precisely formatted data that describe specific business actions.<br />
A JMS application is composed of the following parts:</p>
<ul>
<li>JMS Clients: These are Java programs that send and receive messages. For example, Message Driven Beans.</li>
<li>Messages: The information transferred between the clients.</li>
<li>JMS Provider: The messaging system that implements JMS specification. JBossMQ is an example.</li>
<li>Administered Objects: These are pre-configured JMS objects used by the clients. There are two types of administered objects:
<ol>
<li>Connection Factory: Client uses this object to create a connection with a provider.</li>
<li>Destination: Client uses this object to specify the destination of messages it is sending or the source of messages it receives.</li>
</ol>
</li>
</ul>
<h3><strong>Message Model</strong></h3>
<p>Messages are lightweight entities that consist of a header and a body. The header contains fields used for message routing and identification. The body is the payload which clients exchange.</p>
<table>
<tr>
<td>
Header used in JBossMQ
</td>
</tr>
<tr>
<td valign="top">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Header <span style="color: #009900;">&#123;</span> 
   jmsDestination  <span style="color: #339933;">:</span> QUEUE.<span style="color: #006633;">KSIDemoQueue</span>
   jmsDeliveryMode <span style="color: #339933;">:</span> <span style="color: #cc66cc;">2</span>
   jmsExpiration   <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span>
   jmsPriority     <span style="color: #339933;">:</span> <span style="color: #cc66cc;">4</span>
   jmsMessageID    <span style="color: #339933;">:</span> ID<span style="color: #339933;">:</span><span style="color: #cc66cc;">29</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">12015475384804</span>
   jmsTimeStamp    <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1201547538480</span>
   jmsCorrelationID<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">null</span>
   jmsReplyTo      <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">null</span>
   jmsType         <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">null</span>
   jmsRedelivered  <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">true</span>
   jmsProperties   <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>JMS_JBOSS_REDELIVERY_COUNT<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span>
   jmsPropReadWrite<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">false</span>
   msgReadOnly     <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">true</span>
   producerClientId<span style="color: #339933;">:</span> ID<span style="color: #339933;">:</span><span style="color: #cc66cc;">29</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</td>
</tr>
<tr>
<td>
Header used in WebSphere 5.1
</td>
</tr>
<tr>
<td valign="top">

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  JMSType<span style="color: #339933;">:</span>         <span style="color: #000066; font-weight: bold;">null</span>
  JMSDeliveryMode<span style="color: #339933;">:</span> <span style="color: #cc66cc;">2</span>
  JMSExpiration<span style="color: #339933;">:</span>   <span style="color: #cc66cc;">0</span>
  JMSPriority<span style="color: #339933;">:</span>     <span style="color: #cc66cc;">4</span>
  JMSMessageID<span style="color: #339933;">:</span>    ID<span style="color: #339933;">:</span>414d5120445149434d5320202020202046f2d24b2001c001
  JMSTimestamp<span style="color: #339933;">:</span>    <span style="color: #cc66cc;">1201106363820</span>
  JMSCorrelationID<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">null</span>
  JMSDestination<span style="color: #339933;">:</span>  queue<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">///QUEUE.KSIDemoQueue</span>
  JMSReplyTo<span style="color: #339933;">:</span>      <span style="color: #000066; font-weight: bold;">null</span>
  JMSRedelivered<span style="color: #339933;">:</span>  <span style="color: #000066; font-weight: bold;">false</span>
  JMS_IBM_PutDate<span style="color: #339933;">:</span> <span style="color: #cc66cc;">20080123</span>
  JMSXAppID<span style="color: #339933;">:</span>       Websphere MQ Client <span style="color: #000000; font-weight: bold;">for</span> Java
  JMS_IBM_Format<span style="color: #339933;">:</span>  MQSTR   
  JMS_IBM_PutApplType<span style="color: #339933;">:</span><span style="color: #cc66cc;">28</span>
  JMS_IBM_MsgType<span style="color: #339933;">:</span> <span style="color: #cc66cc;">8</span>
  JMSXUserID<span style="color: #339933;">:</span>      mqm         
  JMS_IBM_PutTime<span style="color: #339933;">:</span> <span style="color: #cc66cc;">16392943</span>
  JMSXDeliveryCount<span style="color: #339933;">:</span><span style="color: #cc66cc;">1</span></pre></div></div>

</td>
</tr>
</table>
<p>Header fields are discussed below.</p>
<table class="default">
<tr>
<td>
<pre>Header field</pre>
</td>
<td>
Description
</td>
<td>Set by</td>
</tr>
<tr>
<td>
<pre>JMSDestination</pre>
</td>
<td>
This field contains the destination to where the message is being sent.
</td>
<td>Send Method</td>
</tr>
<tr>
<td>
<pre>JMSDeliveryMode</pre>
</td>
<td>
JMS supports two message delivery modes.</p>
<ol>
<li>NON_PERSISTENT mode</li>
<p> In this mode, the JMS container does not persist the message. A JMS provider failure can cause a NON_PERSISTENT message to be lost. A JMS provider will deliver these types of messages <i>at most once</i>. The provider may lose the message but it must not deliver it twice.</p>
<li>PERSISTENT mode</li>
<p> In this mode, the JMS container insures that a message will be delivered to the client. A failure within JMS container will not cause the message to be lost. In this mode the messages will be delivered <i>once and only once</i>.
</ol>
</td>
<td>Send Method</td>
</tr>
<tr>
<td>
<pre>JMSMessageID</pre>
</td>
<td>
Each message in a JMS container is assigned a unique ID. This field is used to identify a specific message. All IDs begin with <b>ID:</b>.
</td>
<td>Send Method</td>
</tr>
<tr>
<td>
<pre>JMSTimestamp</pre>
</td>
<td>
This field contains the time a message was handed off to a provider to be sent. It is in the format of a normal Java millis time value.
</td>
<td>Send Method</td>
</tr>
<tr>
<td>
<pre>JMSCorrelationID</pre>
</td>
<td>
This field can use to link one message with another. As an example, a response message can be linked with a request message by populating this field with request message&#8217;s <i>JMSMessageID</i>. ID can be a provider specific message ID or spplication specific string or an array of bytes (<code>byte[]</code>).
</td>
<td>Client</td>
</tr>
<tr>
<td>
<pre>JMSReplyTo</pre>
</td>
<td>
The client populates this field to denote where a reply message has to be sent.Messages sent with a <i>JMSReplyTo</i> value are typically expecting a response although a response is not madatory.
</td>
<td>Client</td>
</tr>
<tr>
<td>
<pre>JMSRedelivered</pre>
</td>
<td>
If the field is set to true, it is an indication to the consuming application that the message may have been delivered in the past and that the application should take extra precautions to prevent duplicate processing.
</td>
<td>Provider</td>
</tr>
<tr>
<td>
<pre>JMSType</pre>
</td>
<td>
Clients can classify messages and set this field to denote that this message belongs to a specific category. For e.g., a call center system can label messages as &#8216;Tech Support&#8217; or &#8216;Sales&#8217; etc. This field can be used with selectors described later.
</td>
<td>Client</td>
</tr>
<tr>
<td>
<pre>JMSExpiration</pre>
</td>
<td>
This denotes the expiration time of a message. The expiration time is calculated as sum of this value and the current GMT value. Expired messages are destroyed. If this is set to 0, the message never expires.
</td>
<td>Send Method</td>
</tr>
<tr>
<td>
<pre>JMSPriority</pre>
</td>
<td>
JMS defines ten priority levels. Levels 0-4 are considered <i>normal</i> and levels 5-9 are <i>expedited</i>. Generally high priority messages are delivered ahead of lower priority messages but this behavior is vendor specific.
</td>
<td>Send Method</td>
</tr>
</table>
<p>In addition to these properties, a client can set and get additional properties via <code>setXXXProperty()</code> and <code>getXXXProperty()</code> methods. JMS reserves <b>JMSX</b> property name prefix for JMS defined properties. Most of these JMSX properties are dependant on a JMS provider. </p>
<p>JMS provides five forms of message body. All messages extend <a href="http://java.sun.com/products/jms/javadoc-102a/javax/jms/Message.html" target="_blank">Message</a> interface.</p>
<ol>
<li><b>StreamMessage</b> This message is composed of Java primitive values. It is written and read sequentially</li>
<li><b>MapMessage</b> This message contains a set of key-value pairs where keys are Strings and values are Java primitive types</li>
<li><b>TextMessage</b> This is the commonly used message form. This message body contains a <i>java.lang.String</i>. For e.g., clients and exchange XML data using this type.</li>
<li><b>ObjectMessage</b> This message allows for transfer of a Serializable Java object.</li>
<li><b>BytesMessage</b> This message is composed of bytes.</li>
</ol>
<p>The difference between StreamMessage and BytesMessage is that StreamMessage preserves the type information of Java primitive types whereas BytesMessage does not. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/jms-internals/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

