Simple Client

In the simplest case, the JatherClient can be used as a thread pool, running Callable instances in separate threads on the local machine. The following example shows how this may be used.

package jatherexample;

import jather.JatherClient;
import jather.SerializableCallable;

import java.util.HashSet;
import java.util.Set;

public class MainClient
{
        public static void main(String[] args) throws Exception
        {
                JatherClient client = new JatherClient();
                String result = client.execute(new MyCallable(1000));
                System.out.println(result);
                client.close();
        }
}

class MyCallable implements SerializableCallable<String>
{
        public int number;

        public MyCallable(int number)
        {
                this.number = number;
        }

        public String call() throws Exception
        {
                String result = "";

                for (int i = 1; i <= number; i++)
                {
                        if (number % i == 0)
                        {
                                System.out.println(number + ") adding factor..." + i);
                                result+=i+",";
                        }
                }

                return result;
        }

}

        

Running this client with:

java -cp jather-0.1-SNAPSHOT.jar:. jatherexample.MainClient

Should produce output like:

...
 ---------------------------------------------------------
 GMS: address is 192.168.10.10:43920 (cluster=Jather)
 ---------------------------------------------------------
...
 ---------------------------------------------------------
 GMS: address is 192.168.10.10:36640 (cluster=Jather)
 ---------------------------------------------------------
1000) adding factor...1
1000) adding factor...2
1000) adding factor...4
1000) adding factor...5
1000) adding factor...8
1000) adding factor...10
1000) adding factor...20
1000) adding factor...25
1000) adding factor...40
1000) adding factor...50
1000) adding factor...100
1000) adding factor...125
1000) adding factor...200
1000) adding factor...250
1000) adding factor...500
1000) adding factor...1000

This client uses the default JGroups JChannel settings. If this example fails to work for you, try following the example demo on the JGroups site.

In the MainClient example there are a few features to note. First the code to be executed is an instance of SerializableCallable . As JatherClient expects a Callable instance that is also Serializable , and the result of the Callable should also be Serializable .

When the MyCallable instance is executed, the call blocks until it has completed. Instead of blocking, Callable instances can be submitted to the JatherClient obtaining a Future instance that can be retrieved at a later state. Here is an example:

package jatherexample;

import jather.JatherClient;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

public class MainSubmitClient
{
        public static void main(String[] args) throws Exception
        {
                JatherClient client = new JatherClient();
                List<Future<String>> calls = new ArrayList<Future<String>>();
                for (int i = 0; i < 5; i++)
                {
                        calls.add(client.submit(new MyCallable(1000)));
                }
                
                for(Future<String> f : calls)
                {
                        System.out.println(f.get());    
                }               
                
                client.close();
        }
}