<?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>Ashish's Tech Blog &#187; Add new tag</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/tag/add-new-tag/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting Started with Terracotta</title>
		<link>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:51:40 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Terracotta]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=340</guid>
		<description><![CDATA[Abstract In this small post we shall explore Terracotta, a leading pure Java Scalability platform. The discussion is based on the AtomicInteger example from Terracotta site, which shows how to implement a Cluster wide id generator (Actually it’s the Sequencer example, but to keep my steps simple had used AtomicInteger). The reason why I choose [...]]]></description>
			<content:encoded><![CDATA[<h2><strong>Abstract</strong></h2>
<p>In this small post we shall explore Terracotta, a leading pure Java Scalability platform. The discussion is based on the AtomicInteger example from Terracotta site, which shows how to implement a Cluster wide id generator (Actually it’s the Sequencer example, but to keep my steps simple had used AtomicInteger). The reason why I choose this example was coz of a very similar requirement that I had to implement in Clustered J2EE application.<br />
Terracotta is well known and needs no introduction <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2><strong>About problem Statement</strong></h2>
<p>Well, I needed a simple solution to have<br />
•	Cluster wide unique id’s<br />
•	Less frequent access to these id’s<br />
•	Optional Persistence</p>
<p><strong>NOTE:</strong> Please note that the current example is slightly modified version of example from terracotta.org.</p>
<h2>Pre-requisite</h2>
<p>To run this example, you need to have following installed<br />
•	Terracotta<br />
•	And JDK offcourse <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Let’s take a look at the sample code first</p>
<pre class="brush: java">
import java.util.concurrent.atomic.*;

public class IdGenerator
{

private AtomicInteger masterCounter = new AtomicInteger(0);

private AtomicInteger slaveCounter = new AtomicInteger(1024);

public int getNewMasterId()
{
int newId = masterCounter.getAndIncrement();
System.out.println(&quot;Master Id is: &quot; + newId);
return newId;
}

public int getNewSlaveId()
{
int slaveId = slaveCounter.getAndIncrement();
System.out.println(&quot;Slave Id is: &quot; + slaveId);
return slaveId;
}

public static void main(String[] args)
{
new IdGenerator().getNewMasterId();
}
}
</pre>
<p>The code has two fields ids master and slave, of which master id need to be unique cluster wide. The code is pretty straight forward, and there is nothing to explain. The id’s are generated by simple getAndIncrement() API.</p>
<p>Lets see the tc-config.xml</p>
<pre class="brush: xml">

&lt;tc:tc-config xmlns:tc=&quot;http://www.terracotta.org/config&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://www.terracotta.org/schema/terracotta-4.xsd&quot;&gt;

&lt;application&gt;
&lt;dso&gt;
&lt;roots&gt;
&lt;root&gt;
&lt;field-name&gt;IdGenerator.masterCounter&lt;/field-name&gt;
&lt;/root&gt;
&lt;/roots&gt;
&lt;/dso&gt;
&lt;/application&gt;
&lt;/tc:tc-config&gt;
</pre>
<p>Since only master Id needed to be unique in this case, hence the configuration</p>
<p>Let’s look at the deployment architecture</p>
<div id="attachment_343" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-343" title="nodes" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2009/11/nodees-300x134.png" alt="Deployment Architecture" width="300" height="134" /><p class="wp-caption-text">Deployment Architecture</p></div>
<p>The application was to run on 3 nodes (3 JVM’s on same machine) and one Terracotta server running.<br />
Before we get into running this application, lets take a look at some useful scripts provided by Terracotta<br />
•	<strong>dso-java (bat|sh)</strong> – startup script that bootstraps Terracotta libraries to your application<br />
•	<strong>start-tc-server (bat|sh)</strong> – Script to start Terracotta Server. It is mandatory to start Terracotta Server before running Clients</p>
<p>Let’s run the application<br />
1.	Compile the application. There is no dependency on TC libraries.<br />
2.	First Start the terracotta Server using script start-tc-server<br />
3.	Run 3 separate JVM with command “dso-java IdGenerator”<br />
That’s it. Our first application is running. If we observe the console output, you can see that we have unique id’s for each invocation.</p>
<h2>References:</h2>
<p>Source code for Terracotta examples - http://svn.terracotta.org/svn/forge/cookbook</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
