Child pages
  • Hello Clustered World

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Include Page
Wiki Markup

Wiki Markup


		      <div class="product-main-column">
			<h1>Hello Clustered World</h1>
			<p>A simple "Hello, World" application that shows the
			   basics of using the Terracotta platform. We refer to
			   this application as "Hello Clustered World."</p>
			<table class="get-started-table" cellpadding="0"
			       cellspacing="0" border="0">

			    <td class="get-started-step-text" valign="top">
			      <h4>1. The Java File</h4>
			      <p>The first thing we need for Hello Clustered
			         World is a simple program. The following
			         program instantiates a Main class with an int
			         counter. It increments the counter, and prints
			         the value.</p>

public class HelloClusteredWorld
    private static int counter;

    public static void main(String[] args) 
        System.out.println("Counter is: " + counter);


			      <p>Unlike a typical Hello World which has no
			         state - we need a program with
			         state. Otherwise, there isn't anything
			         interesting to cluster. The key to clustering
			         is maintaining shared state. So our
			         HelloClusteredWorld uses an int counter for
			      <!-- end step one -->

			    <td class="get-started-step-text" valign="top">
			      <h4>2. Compile</h4>

			      <p>Use javac to compile the program:</p>
%> javac</pre></p>
			      <!-- end step 2 -->


			    <td class="get-started-step-text" valign="top">
			      <h4>3. Run Standalone</h4>
			      <p>Before adding Terracotta, get a feel for how
			         the program runs as a standalone Java
			         process: </p>
%> java HelloClusteredWorld
Counter is: 1</pre></p>
			      <p>If you run the program again, the counter
			         output will remain 1, since each execution of
			         a standalone Java process starts with fresh,
			         reset memory values:</p>

%> java HelloClusteredWorld
Counter is: 1</pre></p>
			      <!-- end step 3 -->

			    <td class="get-started-step-text" valign="top">

			      <h4>4. Configure</h4>
			      <p>To cluster a simple application such as
			         HelloClusteredWorld we need only mark the
			         clustered state. Terracotta introduces the
			         notion of a root, which is the "container" of
			         the clustered state. A root can be any member
			         field of any class.</p>

			      <p>In HelloClusteredWorld, the clustered state is
			         the counter, so we mark the counter field as a

<tc:tc-config xmlns:tc="">




			      <!-- end step 4 -->

			    <td class="get-started-step-text" valign="top">

			      <h4>5. Start a Terracotta Server Array</h4>
			      <p>One requirement for all Terracotta
			         applications is that there must be a running
			         Terracotta Server Array. The Terracotta Server
			         Array manages cluster coherence and persistent

			      <p>The Terracotta Server Array itself is
			         clusterable to deliver the scale and
			         availability as your application demands,
			         however, for demonstration purposes, we will
			         start just one server instance. We do this by
			         running the start-tc-server script included in
			         the bin directory of Terracotta:</p>

			      <p><pre class="code">

2008-10-28 22:59:20,355 INFO - Terracotta Enterprise Edition 2.7.0, as of 20080925-140911 (Revision 2417-10251 by cruise@rh4mo0 from 2.7)
2008-10-28 22:59:21,085 INFO - Configuration loaded from the Java resource at '/com/tc/config/schema/setup/default-config.xml', 
                               relative to class
2008-10-28 22:59:26,853 INFO - JMX Server started. Available at URL[service:jmx:jmxmp://]
2008-10-28 22:59:27,993 INFO - Terracotta Server has started up as ACTIVE node
on successfully, and is now ready for work.

			      <p>When we see the line "Terracotta Server has
			         started up...", our clustered application is
			         ready to run. </p>

			      <!-- end step 5 -->

			    <td class="get-started-step-text" valign="top">
			      <h4>6. Start the HelloClusteredWorld Program as a Terracotta Client JVM</h4>
			      <p>To start a clustered application, start Java
			         with Terracotta wired in. A simple script,
			         dso-java, is included in the Terracotta bin

			      <p>Dso-java is a plain wrapper for Java that
			         configures your JVM to run with Terracotta. It
			         accepts the same parameters as the java
			         command line:</p>

%> dso-java HelloClusteredWorld
Counter: 1</pre></p>
			      <p>At first glance, the output of
			         HelloClusteredWorld clustered is no different
			         from the standalone pass. But wait! Terracotta
			         has provided us with a persistent clustered
			         heap. So what happens if we start another
			         clustered instance? Let's find out...</p>

%> dso-java HelloClusteredWorld
Counter: 2</pre></p>

			      <p>As we can see, on this second run, our state
			         has been preserved! Let's see what happened
			         when Terracotta was added to our
			         HelloClusteredWorld application.</p>

			      <p>During the first pass, the clustered state was
			         initialized to 0, and stored persistently in
			         the Terracotta Server Array (and to
			         disk). When the state was modified (counter++)
			         the Terracotta Server Array automatically
			         persisted the updated state. Hence, upon exit
			         of the first pass, the Terracotta Server Array
			         held the persistent state—the counter
			         field—with a value of 1.</p>

			      <p>Then, in the second pass, the persistent
			         state—the counter field—was automatically
			         loaded into the second JVM. The application
			         modified this state (counter++) and the new
			         value—2—was printed to the console</p>
			      <!-- end step 6 -->

			  <!-- end get-started-table -->

			<!-- end left-image-main-column -->