Hanumant’s Java Workshop

Turbo Charged Java Development!

Working with GridGain

From what I understand about GridGain, basically, you have to identify a task (called GridTask) that can be split up and the splits (called GridJobs) can then be thrown on to multiple machines (called GridNodes). The original task (the one that we split up), can wait for the results of all the GridJobs and once they are ready it can combine them and return the final output. This requires a fair amount of code change (as compared to Terracotta) if you already have something running that you want to run on multiple machines. Of course, in Terracotta you would probably spend that time in configuration instead of java code changes. So, I believe, in terms of amount of efforts, there isn’t much difference.

Based on this understanding, I am still trying to figure out how to make use of GridGain in our Producer-consumer scenario. It was fairly intuitive to do on Terracotta but I am not yet sure how to proceed with GridGain. May be this scenario is more suitable for Terracotta.

Let’s see…

September 27, 2007 - Posted by | Java

1 Comment »

  1. Hi,

    Nothing could be further from the truth. For simply taking a code and running it with GridGain (just like Terracotta), you don’t have to write any code (GridTasks are useful when you want to split your logic into smaller pieces to achieve even higher level of parallelism).

    Here are the steps you need to do in order to grid-enable your producer-consumer application described here.

    1. Make Job implement java.io.Serializable

    2. Attach @Gridify annotation to Job.run() method:
    {code}
    /**
    * The body of the class is directly copied from your example.
    */
    public class Job implements Serializable {
    int jobduration = (int) (Math.random()*5000);

    /**
    * This method is grid-enabled by a virtue of attaching @Gridify annotation. The body of the method is directly copied from your example.
    */
    @Gridify
    public void run(){
    try {
    Thread.sleep(jobduration);

    System.out.println(“Job finished in ” + jobduration + ” millis.”);
    }
    catch (InterruptedException ex) {
    ex.printStackTrace();
    }
    }
    }
    {code}
    3. That’s it. Here is what your Main would look like:
    {code}
    public class Main {
    public static void main(String[] args) throws Exception {
    // Start grid.
    GridFactory.start();

    try {
    Jobs jobs = new Jobs();

    new Producer(jobs).start();
    new Consumer(jobs).start();
    new Consumer(jobs).start();

    Thread.sleep(Long.MAX_VALUE);
    }
    finally {
    // Stop grid.
    GridFactory.stop(true);
    }
    }
    }
    {code}

    4. Now you can run your Main directly from IDE of your choice. Note that there is no extra configuration required. If you start remote GridGain node (just double-click on gridgain.bat script) then your jobs will be executed on that node without any (re)deployment steps – the classes will be peer-loaded automatically.

    Best,
    Dmitriy Setrakyan
    GridGain – Grid Computing Made Simple

    Comment by Dmitriy Setrakyan | September 27, 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: