Broadcasting asynchronously

The following are the steps to our first compute grid task:

  1. Create a Gradle project called 'compute-grid' and add the following dependencies. You have to define the ${igniteVersion} at the top of the file under the ext section of the buildScript closure: 
       compile group: 'org.apache.ignite', name: 'ignite-core', version: 
"${igniteVersion}"
compile group: 'org.apache.ignite', name: 'ignite-spring', version:
"${igniteVersion}"
compile group: 'org.apache.ignite', name: 'ignite-indexing',
version: "${igniteVersion}"
compile group: 'com.h2database', name: 'h2', version: '1.4.195'
  1. We already explored message broadcasting in Chapter 2, Understanding Topologies and Caching Strategies. This example is going to examine the broadcastAsync API. We will create an IgniteCallable instance and broadcast it asynchronously to all nodes. The IgniteCallable call method returns an IgniteFuture object as result. Our IgniteCallable call will return the execution time. Add the following lines:
      IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
try (Ignite ignite = Ignition.start(cfg)) {
// Get a compute task
IgniteCompute compute = ignite.compute();

// broadcast the computation to all nodes
IgniteCallable<String> callableJob = new
IgniteCallable<String>() {
private static final long serialVersionUID = 1L;

@Override
public String call() throws Exception {
System.out.println("Executing in a cluster");
return String.valueOf(System.currentTimeMillis());
}
};

//broadcast async and get a future.
IgniteFuture<Collection<String>> asyncFuture =
compute.broadcastAsync(callableJob);


//Async process may take time. wait till the execution
is completed
while (!asyncFuture.isDone()) {
System.out.println("Waiting for response");
}

//Get the response from all nodes
asyncFuture.get().forEach(result -> {
System.out.println(result);
});
}
  1. The future object returns a collection of strings, the individual responses from the cluster nodes. Now, launch an Ignite instance and run the program. The result will contain two responses: the remote ignite node and the program node. The following broadcast message got printed in the Eclipse console:

The remote Ignite server prints the broadcast message:

Our program waits for the async broadcast results, and finally prints the results collected from the nodes:

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

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