Hanumant’s Java Workshop

Turbo Charged Java Development!

Working with Terracotta

Terracotta allows us to specify, through an XML based configuration file, what objects do we want to share accross JVMs. In our application, we want a single Jobs instance to be shared accross JVMs. Our Producers will add jobs to the same Jobs instances and our consumers will take out jobs from the same Jobs instance.

We will create a new starter class called Main that can spawn either a Producer or a Consumer thread depending on the command line argument:

The Main class:

public class Main {
    Jobs jobs = new Jobs();
    public Main(boolean isProducer){
        if(isProducer) new Producer(jobs).start();
        else new Consumer(jobs).start();

    public static void main(String[] args){
        new Main(args.length>0 && 

All we want to do now is to tell Terracotta to share jobs field of this Main class and make sure that all the Producers and Consumers use the methods of Jobs class in a mutually exclusive manner. The config file to do this is really simple –

<?xml version="1.0" encoding="UTF-8"?>
<tc:tc-config xmlns:tc="http://www.terracotta.org/config">
         <method-expression>* Jobs*.*(..)</method-expression>

That’s all we need to do in terms of coding!!!

Running the application:

To run the application,
1. First we need to run the Terracotta Server using start-tc-server.bat available in the bin directory of Terracotta installation.

2. Launch our Main class using dso-java.bat script (available in bin) instead of directly using java. We pass the config file name as a system property and our class name :

c:\works\test\>dso-java -Dtc.config tc-config-pc.xml Main consumer

I started up the consumer first and I can see that it gets stuck on the wait() because there is no Job in Jobs.

3. Start the producer in another cmd window:
c:\works\test\>dso-java -Dtc.config tc-config-pc.xml Main producer

That’s it!!! As soon as producer starts putting Job instances in jobs, the consumer starts getting them. No RMI, No EJB, No CORBA and we have shared an object with multiple JVMs. No code changes required in the main fiunctionality to make it work on multiple JVMs.

To understand how it works under the hood, please do read the documentation at Terracotta website.

September 26, 2007 - Posted by | Java

1 Comment »

  1. Hi,

    Looking forward to your GridGain analysis 🙂

    I think GridGain benefits will shine not only when you have to execute something on remote node, but also when you need to split a bigger task into smaller subtasks.

    Also GridGain’s peer-class-loading will come very convenient during development – there is no deployment or extra configuration of remote nodes.

    Dmitriy Setrakyan
    GridGain – Grid Computing Made Simple

    Comment by Dmitriy Setrakyan | September 26, 2007 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: