Exploring the run and call APIs

The compute grid's run() API works with IgniteRunnable. The IgniteRunnable extends Java's Runnable interface; the run() method is executed in the remote cluster. We are going to create a runnable and examine the run behavior. The following are the steps:

  1. Create a class, AdderRunnable, to add two numbers remotely. Implement the IgniteRunnable interface and create a constructor to pass two integers. In the run method, add the numbers and print it in console:
      class AdderRunnable implements IgniteRunnable {
private static final long serialVersionUID = 1L;
private final int first;
private final int second;

public AdderRunnable(int first, int second) {
super();
this.first = first;
this.second = second;
}

@Override
public void run() {
System.out.println(String.format("In
IgniteRunnable Adder Adding %s and %s the
result is = %s ", first, second,
(first + second)));
}
}
  1. Create a class, IgniteRunnableAndCallable, add the following code snippet to create an instance of AdderRunnable, and pass it to the run method of Ignite compute:
       IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);

try (Ignite ignite = Ignition.start(cfg)) {
IgniteCompute compute = ignite.compute();
compute.run(new AdderRunnable(1, 2));
}
  1. When you execute the program, it will print the result either on your Eclipse console or on a remote server console. If you want to run it on a remote node, then get the compute instance for the remote cluster group using the following snippet:  ingnite.compute(ignite.cluster().forRemotes() );

  1.  Now run it asynchronously, and call the runAsync method with an IgniteRunnable instance. runAsync returns a future to notify of job completion. The run method of IgniteRunnable is a void, so the IgniteFuture can only notify you of whether the job has been done or not, but can't return a result. Add the following snippet:
       IgniteFuture<Void> runAsync = compute.runAsync(new AdderRunnable(1, 2));
while(!runAsync.isDone()) {
System.out.println("Waiting for the job completion");
}
System.out.println("Job done");
  1. Run the program again; it will wait for job completion and finally print 'Job done'. IgniteRunnable is a good option for background processing such as when some calculate wages and update a cache:

The call() API is used when we need the result of a task execution. It takes IgniteCallable<T>; the T is a type to be returned.

  1. Let's create an AdderCallable and examine the call behavior:
      class AdderCallable implements IgniteCallable<Integer> {
private static final long serialVersionUID = 1L;
private final int first;
private final int second;

public AdderCallable(int first, int second) {
super();
this.first = first;
this.second = second;
}
@Override
public Integer call() throws Exception {
int result = first + second;
System.out.println(
String.format("In IgniteCallable Adder Adding %s and %s
the result is =
%s ", first, second, result));
return result;
}
}
  1. The AdderCallable class implements the IgniteCallable<Integer> interface; the call method returns the summation of two numbers passed in the constructor of AdderCallable.  Add the following snippet to our existing code. Check that the call method blocks the thread and waits for the result:
       Integer result = compute.call(new AdderCallable(1, 2));
System.out.println("Callable Job done with result "+result);
  1. It prints the result immediately:

  1. Blocking calls could be frustrating; you can execute a callable asynchronously. Add the following lines:
      IgniteFuture<Integer> callAsync = compute.callAsync(new 
AdderCallable(1, 2));
while(!callAsync.isDone()) {
System.out.println("Waiting for the callable job completion");
}
System.out.println("Callable Job done with result "+callAsync.get());
  1. The callAsync returns a future object; we can wait for the future to get done or cancelled using a while loop. The following is the console output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.75.165