<?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.</title>
	<atom:link href="http://www.kandysoftwareinc.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kandysoftwareinc.com</link>
	<description></description>
	<lastBuildDate>Wed, 17 Dec 2008 06:47:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Implicit &amp; Explicit Locks</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/implicit-explicit-locks</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/implicit-explicit-locks#comments</comments>
		<pubDate>Sun, 23 Nov 2008 18:52:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[await]]></category>
		<category><![CDATA[condition]]></category>
		<category><![CDATA[explicit]]></category>
		<category><![CDATA[finally]]></category>
		<category><![CDATA[implicit]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[notify]]></category>
		<category><![CDATA[signal]]></category>
		<category><![CDATA[synchronized]]></category>
		<category><![CDATA[tr]]></category>
		<category><![CDATA[wait]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=128</guid>
		<description><![CDATA[In Computer Science, <i>synchronization</i> refers to relationships among events. For example:
<ul>
<li>Serialization: Event A must <i>happen before</i> Event B.
<li>Mutual Exclusion: Event A and Event B <i>must not happen at the same time</i>. 
</ul>
Prior to JDK 5, Java's support for synchronization was based on implicit locks by the use of <code>synchronized</code> keyword. This article introduces explicit locks introduced in JDK 5.]]></description>
			<content:encoded><![CDATA[<h3><strong>Introduction</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/locks.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/locks.png" alt="" title="locks" class="alignright size-full wp-image-131" /></a>In Computer Science, <i>synchronization</i> refers to relationships among events. For example:</p>
<ul>
<li>Serialization: Event A must <i>happen before</i> Event B.
<li>Mutual Exclusion: Event A and Event B <i>must not happen at the same time</i>.
</ul>
<p>Java&#8217;s support for synchronization is based on implicit locks by the use of <code>synchronized</code> keyword. Every object has an intrinsic lock associated with it. When a thread invokes a <b>synchronized non-static method</b>, it acquires the intrinsic lock for that method&#8217;s object. The lock is returned when the method returns (even due to an uncaught exception). Similarly, every class has an intrinsic lock associated with it. A thread acquires this class lock to invoke a <b>synchronized static</b> method and releases when the method returns. These locks are acquired and released automatically.</p>
<p>In addition to intrinsic locks, each instance also has a <i>wait set</i> associated with it. When a thread invokes <code>wait()</code> (or any overloaded <code>wait(...)</code> methods) it is placed in this wait set. The <code>wait(...)</code> methods must be called from a synchronized block. This is used in scenarios like when a thread obtains the lock but is not ready to proceed code in the critical section. </p>
<h3><strong>Implicit locks demo</strong></h3>
<p>I will demonstrate implicit locks by implementing a solution to producer-consumer problem. Source code is attached as a link at the end of the article. The producer-consumer problem can be described as follows. There are M threads producing items and N threads consuming items. They share a common buffer for transfering these items. The problem is to make sure that producers won&#8217;t add items when the buffer is full and consumers won&#8217;t remove items from an empty buffer. In <code>ImplicitLocksDemo</code> class I have 3 nested classes &#8211; <code>Producer</code>, <code>Consumer</code> and <code>Drop</code> (shared buffer). <code>Producer</code> and <code>Consumer</code> are implemented as threads. <code>Producer</code> creates a <code>String</code> object and places in the <code>drop</code> object which is shared between producers and consumers as shown below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Drop drop <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Drop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To make the problem a bit more complicated, the shared buffer is made to hold only one item. The <code>Drop</code> class has one member variable (<code>shared</code>) which will be set by a producer. Additionally, the <code>Drop</code> class has a variable called <code>empty</code> which tells if the buffer is full or empty. Producers can only set <code>shared</code> with an item when <code>empty</code> is true. Similarly, consumers can only read <code>shared</code> when <code>empty</code> is false. The <code>Drop</code> class has two methods called <code>get()</code> which the consumers call to read an item and a method called <code>set(String)</code> which is called by producers to set an item. Both <code>get()</code> and <code>set()</code> are made synchronized so only one thread can invoke <code>get()</code> or <code>set()</code> method at any time.</p>
<p>Now, what if a producer acquired a lock and wants to invoke <code>set()</code> method and the buffer is not empty? If a consumer had not read the item produced previously, the producer would be overwriting the item. To use the locks effectively, we use wait/notify mechanism provided by Java. <code>wait()</code> and <code>notify</code> are methods defined in <code>Object</code> class, hence, they are available to any class. Invoking <code>wait()</code> makes the current thread go into a waiting state or into the wait set associated with the object on which <code>wait()</code> was invoked. When a consumer reads the item it invokes <code>notify()</code> on <code>drop</code> object which sends a signal to all threads waiting in the wait set of <code>drop</code> object. That is to say that the shared object is now empty and a producer can safely fill the shared storage. So with the help of wait/notify, threads can effectively communicate with each other and share the critical sections in a safe manner. Complete solution is given below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @(#)ImplicitLocksDemo.java	Feb 26, 2008
 */</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">lock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Random</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Demonstrates Producer and Consumer with a
 * 
 * @author $Author: vijaykandy $
 * @version $Revision: 1.4 $
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImplicitLocksDemo <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Main.
	 * 
	 * @param s
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Drop drop <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Drop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P1&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Consumer c2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Producer p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;P2&quot;</span>, drop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c1<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</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;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>c2<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Shared class
	 * 
	 * @author $Author: vijaykandy $
	 * @version $Revision: 1.4 $
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Drop <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> shared<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> empty <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Takes a message when a signal is received.
		 * 
		 * @return
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #003399;">String</span> get<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>empty<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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">wait</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;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					e.<span style="color: #006633;">printStackTrace</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: #009900;">&#125;</span>
			<span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> shared<span style="color: #339933;">;</span>
			empty <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">notifyAll</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;">return</span> message<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Sets a message when a signal is received.
		 * 
		 * @param s
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> set<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>empty<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: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">wait</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;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					e.<span style="color: #006633;">printStackTrace</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: #009900;">&#125;</span>
			shared <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span>
			empty <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">notifyAll</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: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Produces a message
	 * 
	 * @author $Author: vijaykandy $
	 * @version $Revision: 1.4 $
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Producer <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> Drop drop<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</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>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> messages <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #009900;">&#123;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">toHexString</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">toHexString</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">toHexString</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DONE&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: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> Producer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, Drop drop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">drop</span> <span style="color: #339933;">=</span> drop<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> msg <span style="color: #339933;">:</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				drop.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span>msg<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;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%s produced: %s%n&quot;</span>, name, msg<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>
					<span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Consumes a message
	 * 
	 * @author $Author: vijaykandy $
	 * @version $Revision: 1.4 $
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Consumer <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> Drop drop<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> Consumer<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name, Drop drop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">drop</span> <span style="color: #339933;">=</span> drop<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</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;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> drop.<span style="color: #006633;">get</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;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%s received: %s%n&quot;</span>, name, message<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>message.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DONE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">return</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: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span>random.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <code>synchronized</code> keyword can also be used with a block. In this case, threads are required to acquire the lock associated with the object in the <code>synchronized</code> statement as shown here:.</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> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">Object</span> anObject <span style="color: #339933;">=</span> ...
	<span style="color: #666666; font-style: italic;">// ...</span>
	<span style="color: #000000; font-weight: bold;">synchronized</span><span style="color: #009900;">&#40;</span>anObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// threads must acquire anObject's lock</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/implicit-explicit-locks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Longest Increasing Subsequence</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/lis</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/lis#comments</comments>
		<pubDate>Fri, 15 Feb 2008 00:50:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[increasing]]></category>
		<category><![CDATA[lis]]></category>
		<category><![CDATA[subsequence]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=85</guid>
		<description><![CDATA[A classic computer science problem. The problem is finding the longest increasing subset of numbers within a given set. This article presents a solution in Java. ]]></description>
			<content:encoded><![CDATA[<h3><strong>Introduction</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/dna.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/dna.png" alt="" title="dna" class="alignleft size-medium wp-image-86" /></a>Finding the <a href="http://en.wikipedia.org/wiki/Longest_increasing_subsequence" target="_blank">maximum or longest increasing subsequence</a> (LIS) within a given a set of numbers is a classic Computer Science problem. The algorithm is central to many tools and is used in various disciplines related to mathematics, physics etc. In the field of Bioinformatics, LIS is used to find DNA subsequences matching a certain criteria. By recognizing certain patterns in DNA sequences scientists can determine diseases, genetic disorders etc.</p>
<h3><strong>Problem</strong></h3>
<p>Given a sequence of N integers, find the longest <i>strictly</i> increasing subsequence. For example in the sequence 56, 21, 33, 22, 34, 78, 11, 35, 46, the longest increasing subsequence is 21, 33, 34, 35, 46. In this context we will consider a NXN matrix of integers and find a sequence which is simply a series of adjacent squares. A square may not be used more than once. In the following 10 X 10 grid:</p>
<pre>
  | 0  1  2  3  4  5  6  7  8  9
--+------------------------------
0 | 95 47 30 36 60 31 57 66 12 55
1 | 35 57 41 13 82 80 71 93 31 62
2 | 89 36 98 75 91 24 95 53 37 99
3 | 25 45 26 17 15 84 80 73 96 21
4 | 75 22 43 <font color="red">96</font> 96 36 64 31 45 86
5 | 45 99 <font color="red">68</font> <font color="red">74</font> 54 14 93 17 14 55
6 | 14 42 <font color="red">52</font> 54 34 50 22 84 32 41
7 | 90 <font color="red">44</font> 73 10 71 84 20 12 55 52
8 | 95 <font color="red">33</font> <font color="red">25</font> 31 76 45 44 84 90 52
9 | 94 64 95 <font color="red">24</font> 41 63 87 93 79 12
</pre>
<p>the longest increasing subsequence is of length 8 consisting of entries 24, 25, 33, 44, 52, 68, 74, 96 as highlighted in red.</p>
<h3><strong>Solution</strong></h3>
<p>Each number and its position in the sequence are stored in an array. In the first pass, neighbors for each item in the array are found and are stored along with the item. Items are then sorted by their descending value. In the second and final pass, weights are assigned to the neighbors. The neighbor with the least value is assigned weight 1, the next highest neighbor is assigned 2 and so on. To get the longest increasing subsequence, find the element with the highest weight and print its neighbors in the descending order of weights.</p>
<p>Unzip the attachment and <code>cd</code> into the newly created folder. Compile the classes run as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\MaximumIncreasingSubsequence&gt;javac -d bin src\*.java
&nbsp;
C:\MaximumIncreasingSubsequence&gt;java -classpath bin MaximumIncreasingSubsequence small-grid.txt
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">24</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">129</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">24</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">138</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">167</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">236</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">300</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">26</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">338</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">438</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">28</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">469</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">596</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">709</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">26</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">723</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">778</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">28</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">866</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">27</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">939</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">26</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> with value <span style="color: #cc66cc;">953</span> <span style="color: #66cc66;">&#40;</span>weight = <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
Program took: <span style="color: #cc66cc;">380</span> milliseconds</pre></div></div>

<h3><strong>Attachments</strong></h3>
<p><a href='http://www.kandysoftwareinc.com/wp-content/uploads/2008/02/maximumincreasingsubsequence.zip' title='Source code'>Source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/lis/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache, Tomcat cluster, load balancing using mod_jk</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/apache-tomcat-cluster-load-balancing-using-mod_jk</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/apache-tomcat-cluster-load-balancing-using-mod_jk#comments</comments>
		<pubDate>Thu, 14 Feb 2008 21:46:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[cluster]]></category>
		<category><![CDATA[httpd.conf]]></category>
		<category><![CDATA[load balance]]></category>
		<category><![CDATA[mod_jk]]></category>
		<category><![CDATA[multicast]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[worker]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=64</guid>
		<description><![CDATA[This article discusses how to configure multiple Tomcat instances to balance work load using mod_jk Apache connector.]]></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/tomcat.gif"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/tomcat.gif" alt="" title="tomcat" width="130" height="92" class="alignleft size-thumbnail wp-image-68" /></a><a href="http://www.kandysoftwareinc.com/articles/mod_jk.pdf" target="_blank"><img src="http://www.kandysoftwareinc.com/images/PDF_icon.jpg" align="right" height="50" width="50" class="alignleft" border="0"/></a>This article discusses how to install and configure Apache 2.0.x and Tomcat (or JBoss) to communicate with each other via mod_jk. This article also discusses how to setup a Tomcat cluster using mod_jk to balance work load.<br />
<br/></p>
<h3><strong>Install JDK</strong></h3>
<ol>
<li>
Download JDK rpm and launch the executable by running the following commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> a+x j2sdk-<span style="color: #000000;">1</span>_4_2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>-linux-i586-rpm.bin
.<span style="color: #000000; font-weight: bold;">/</span>j2sdk-<span style="color: #000000;">1</span>_4_2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>-linux-i586-rpm.bin</pre></div></div>

</li>
<li>
Type <strong>yes</strong> and agree to the binary license agreement.
</li>
<li>
Become root by running <strong>su</strong> and entering root password
</li>
<li>
Install the rpm. This will install JDK at <code>/usr/java/j2sdk-1.4.2</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rpm <span style="color: #660033;">-iv</span> jdk-1.4.2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>.i386.rpm</pre></div></div>

</li>
<li>
Edit PATH environment variable to include bin directory of JDK by adding the following lines to <code>/etc/profile</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>java<span style="color: #000000; font-weight: bold;">/</span>j2sdk-1.4.2_<span style="color: #000000; font-weight: bold;">&lt;</span>version number<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> JAVA_HOME
<span style="color: #007800;">PATH</span>=<span style="color: #007800;">$JAVA_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span></pre></div></div>

</li>
</ol>
<h3><strong>Avoid Firewall and Routing issues</strong></h3>
<ol>
<li>
Disable the default firewall (unless you can configure it to allow multicast packets)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>iptables stop
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>ip6tables stop</pre></div></div>

</li>
<li>
Add this multicast route:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>route add <span style="color: #660033;">-net</span> 224.0.0.0 netmask 240.0.0.0 dev eth<span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

</li>
</ol>
<h3><strong>Install Apache</strong></h3>
<ol>
<li>
Download <code>httpd-2.0.50-i686-pc-linux-gnu.tar.gz</code> into a temporary location and run the following commands to install Apache at <code>/usr/local/share</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">unzip</span> <span style="color: #000000; font-weight: bold;">&lt;</span> httpd-2.0.50-i686-pc-linux-gnu.tar.gz <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> xvf -
<span style="color: #7a0874; font-weight: bold;">cd</span> httpd-2.0.50
.<span style="color: #000000; font-weight: bold;">/</span>install-bindist.sh <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apache-2.0.50</pre></div></div>

</li>
<li>
Edit PATH environment variable to include bin directory of Apache by adding the following lines to <code>/etc/profile</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">APACHE_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apache-2.0.50
<span style="color: #7a0874; font-weight: bold;">export</span> APACHE_HOME
<span style="color: #007800;">PATH</span>=<span style="color: #007800;">$APACHE_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span>
<span style="color: #7a0874; font-weight: bold;">export</span> PATH</pre></div></div>

</li>
</ol>
<h3><strong>Install Tomcat</strong></h3>
<ol>
<li>
Move <code>apache-tomcat-x.y.z.zip.zip</code> to <code>/usr/local/share</code> (I will refer to the installation folder by this path) and unzip it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">unzip</span> apache-tomcat-x.y.z.zip</pre></div></div>

</li>
<li>
Edit PATH environment variable to include bin directory of Apache by adding the following lines to <code>/etc/profile</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">TOMCAT_HOME</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>apache-tomcat-x.y.z
<span style="color: #7a0874; font-weight: bold;">export</span> TOMCAT_HOME
<span style="color: #007800;">PATH</span>=<span style="color: #007800;">$TOMCAT_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span>
<span style="color: #7a0874; font-weight: bold;">export</span> PATH</pre></div></div>

</li>
</ol>
<h3><strong>Configure mod_jk</strong></h3>
<ol>
<li>
Copy the mod_jk binary to <code>$APACHE_HOME/modules</code> directory and rename it as <code>mod_jk.so</code>
</li>
<li>
Add the following lines at the very bottom in <code>$APACHE_HOME/conf/httpd.conf</code><br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;">#Include mod_jk configuration file</span>
<span style="color: #00007f;">Include</span> conf/mod-jk.conf</pre></div></div>

</li>
<li>
Under <code>$APACHE_HOME/conf</code> create <code>mod-jk.conf</code> and write the following lines in it:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;"># Load mod_jk module</span>
<span style="color: #adadad; font-style: italic;"># Specify the filename of the mod_jk lib</span>
<span style="color: #00007f;">LoadModule</span> jk_module modules/mod_jk.so
<span style="color: #adadad; font-style: italic;"># Where to find workers.properties</span>
JkWorkersFile conf/workers.properties
<span style="color: #adadad; font-style: italic;"># Where to put jk logs</span>
JkLogFile logs/mod_jk.log
<span style="color: #adadad; font-style: italic;"># Set the jk log level [debug/error/info]</span>
JkLogLevel info
<span style="color: #adadad; font-style: italic;"># Select the log format</span>
JkLogStampFormat <span style="color: #7f007f;">&quot;[%a %b %d %H:%M:%S %Y]&quot;</span>
<span style="color: #adadad; font-style: italic;"># JkOptions indicates to send SSK KEY SIZE</span>
JkOptions +ForwardKeySize +ForwardURICompat -
ForwardDirectories
<span style="color: #adadad; font-style: italic;"># JkRequestLogFormat</span>
JkRequestLogFormat <span style="color: #7f007f;">&quot;%w %V %T&quot;</span>
JkMount /Context/* loadbalancer</pre></div></div>

<p><br/><br />
Where <font color="#ff0000"><strong>Context</strong></font> is the web-app context name.
</li>
<li>
Under <code>$APACHE_HOME/conf</code> create <code>workers.properties</code> and populate it as follows:<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;"># Worker list</span>
worker.list=loadbalancer
<span style="color: #adadad; font-style: italic;"># Define Node1</span>
worker.node1.<span style="color: #00007f;">port</span>=<span style="color: #ff0000;">8009</span>
worker.node1.host=node1.ip.address
worker.node1.type=ajp13
worker.node1.lbfactor=<span style="color: #ff0000;">1</span>
<span style="color: #adadad; font-style: italic;">#worker.node1.local_worker=1 (1)</span>
worker.node1.<span style="color: #00007f;">cachesize</span>=<span style="color: #ff0000;">10</span>
<span style="color: #adadad; font-style: italic;"># Define Node2</span>
worker.node2.<span style="color: #00007f;">port</span>=<span style="color: #ff0000;">8009</span>
worker.node2.host=node2.ip.address
worker.node2.type=ajp13
worker.node2.lbfactor=<span style="color: #ff0000;">1</span>
<span style="color: #adadad; font-style: italic;">#worker.node2.local_worker=1 (1)</span>
worker.node2.<span style="color: #00007f;">cachesize</span>=<span style="color: #ff0000;">10</span>
<span style="color: #adadad; font-style: italic;"># Load-balancer</span>
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=node1, node2
worker.loadbalancer.sticky_session=<span style="color: #ff0000;">1</span>
worker.loadbalancer.local_worker_only=<span style="color: #ff0000;">1</span></pre></div></div>

<p>Add one worker per node and name workers as node1, node2, node3 … nodeN. Don’t forget to define all these as load balancing workers:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">worker.loadbalancer.balanced_workers=node1, node2, node3, ... nodeN</pre></div></div>

<p>Replace <code>node1.ip.address</code> with IPAddress of Tomcat(or JBoss) instance1 and so on. The attribute <code>sticky_session=1</code> tells mod_jk that SESSION ID&#8217;s should be routed back to the same Tomcat worker. The <code>lbfactor</code> determines the number of requests a worker receives. The higher the <code>lbfactor</code> the more requests a worker receives.
</li>
<li>
Each node should be named according to the names specified in <code>workers.properties</code>. On each Tomcat instance, edit <code>$TOMCAT_HOME/conf/server.xml</code>. Locate the &lt;Engine&gt; element and add an attribute called jvmRoute:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Engine</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Catalina&quot;</span> <span style="color: #000066;">defaultHost</span>=<span style="color: #ff0000;">&quot;localhost&quot;</span> <span style="color: #000066;">jvmRoute</span>=<span style="color: #ff0000;">&quot;node1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
…
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Engine<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</li>
</ol>
<p><font color="#ff0000"><strong>NOTE: THE jvmRoute ATTRIBUTE MUST MATCH THE NAME SPECIFIED IN workers.properties.</strong></font></p>
<h3><strong>Start Apache and Tomcat</strong></h3>
<ol>
<li>
Start Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$APACHE_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apachectl <span style="color: #660033;">-k</span> start</pre></div></div>

</li>
<li>
To stop use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$APACHE_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apachectl <span style="color: #660033;">-k</span> stop</pre></div></div>

</li>
<li>
Start Tomcat:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$TOMCAT_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>startup.sh</pre></div></div>

</li>
<li>
Stop Tomcat:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$TOMCAT_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>shutdown.sh</pre></div></div>

</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/apache-tomcat-cluster-load-balancing-using-mod_jk/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>Apache, OpenSSL</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/apache-openssl</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/apache-openssl#comments</comments>
		<pubDate>Wed, 13 Feb 2008 21:40:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=62</guid>
		<description><![CDATA[This article discusses how to configure Apache and OpenSSL to serve https content.]]></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/apache-openssl.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/apache-openssl.png" alt="" title="apache-openssl" class="alignleft size-medium wp-image-73" /></a>This article discusses how to configure Apache and OpenSSL to serve https content. We assume Apache is installed and OpenSSL commands are available in the PATH environment variable. Otherwise install Apache and OpenSSL using <strong><em>yum</em></strong> or <strong><em>apt</em></strong>.</p>
<h3><strong>Create a working directory</strong></h3>
<p>Let&#8217;s call it sslcert:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$mkdir</span> sslcert</pre></div></div>

<h3><strong>Create two subdirectories under it</strong></h3>
<p>Create 2 subdirectories under sslcert. Let&#8217;s call them certs and private.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$cd</span> sslcert
<span style="color: #007800;">$mkdir</span> certs private</pre></div></div>

<h3><strong>Create a database to keep track of each certificate signed</strong></h3>
<p>Run the following commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$echo</span> <span style="color: #ff0000;">'01'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> serial
<span style="color: #007800;">$touch</span> certindex.txt</pre></div></div>

<h3><strong>Make a custom config file for openssl to use</strong></h3>
<p>Copy the config elements into a file called openssl.cnf. (edit as needed):</p>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/ssl11.JPG" title="ssl11.JPG"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/ssl11.JPG" alt="ssl11.JPG" border="0" /></a></td>
</tr>
<tr>
<td><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/ssl21.JPG" title="ssl21.JPG"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/01/ssl21.JPG" alt="ssl21.JPG" border="0" /></a></td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/apache-openssl/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boggle</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/boggle</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/boggle#comments</comments>
		<pubDate>Sat, 09 Feb 2008 00:19:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Boggle]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=77</guid>
		<description><![CDATA[A Java implementation of the popular word game.]]></description>
			<content:encoded><![CDATA[<h3><strong>The Game</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/boggle.png"><img src="http://www.kandysoftwareinc.com/wp-content/uploads/2008/11/boggle.png" alt="" title="boggle" class="alignleft size-medium wp-image-80" /></a><a href="http://en.wikipedia.org/wiki/Boggle" target="_blank">Boggle</a> is a word game designed by Allan Turoff. The objective of the game is to construct as many different words as you can from a grid of letters. You can use letters (in positions) that are next to each other &#8211; above, below, left, right, diagonal but you can not use the same letter more than once. Each word must be defined in a dictionary. Points are assigned according to the length of the word.</p>
<p>The source code attached here is an implementation of this game in Java. It demonstrates <a href="http://en.wikipedia.org/wiki/Recursion" target="_blank">recursion</a> to solve problems like this game. The program attempts to construct words as defined in <code>dic.txt</code> from <code>puzzle.txt</code>. Points are assigned according to the length of the word.</p>
<h3><strong>Instructions</strong></h3>
<p>To run this program you need JDK 5 or better installed. You can get JDK <a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">from here</a>.</p>
<ol>
<li>Unzip the file. This creates a folder called <code>Boggle</code></li>
<li>Compile the code: <code>javac src\*.java  -d bin</code></li>
<li>Run the program: <code>java -classpath bin Boggle</code></li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\Boggle&gt;javac src\*.java  -d bin
&nbsp;
C:\Boggle&gt;java -classpath bin Boggle
Enter boggle file:
puzzle.txt
Opened puzzle.txt
Enter dictionary name:
dic.txt
Opened dic.txt
Reading files...
Solving...
Word            | Points | Path
----------------+--------+-----------------------
            APR |      <span style="color: #cc66cc;">1</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
      APRTOCFED |     <span style="color: #cc66cc;">15</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
            COT |      <span style="color: #cc66cc;">1</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
            FED |      <span style="color: #cc66cc;">1</span> | <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<h3><strong>Attachments</strong></h3>
<p><a href="http://www.kandysoftwareinc.com/wp-content/uploads/2008/02/boggle.zip">Source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/boggle/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>
		<item>
		<title>Technical Articles</title>
		<link>http://www.kandysoftwareinc.com/tech-articles/home</link>
		<comments>http://www.kandysoftwareinc.com/tech-articles/home#comments</comments>
		<pubDate>Fri, 25 Jan 2008 21:51:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Headline]]></category>

		<guid isPermaLink="false">http://www.kandysoftwareinc.com/?p=66</guid>
		<description><![CDATA[Here are some articles we have written over the years on various topics. All source code available on this site is protected by <a href="http://www.gnu.org/licenses/gpl-3.0-standalone.html" title="GPL">GNU GPL</a> and all articles are protected under <a href="http://www.gnu.org/licenses/fdl.html#TOC1">GNU FDL</a>. The source code and documents come with <strong>absolutely no warranty</strong>.
<br/>
<br/>
<br/>]]></description>
			<content:encoded><![CDATA[<p>Here are some articles we have written over the years on various topics. All source code available on this site is protected by <a href="http://www.gnu.org/licenses/gpl-3.0-standalone.html" title="GPL">GNU GPL</a> and all articles are protected under <a href="http://www.gnu.org/licenses/fdl.html#TOC1">GNU FDL</a>. The source code and documents come with <strong>absolutely no warranty</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kandysoftwareinc.com/tech-articles/home/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
