Grouping by node age

You can send computations to the oldest or youngest node. The following code snippet sends computations to the oldest and youngest nodes:

package com.packt;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCluster;
import org.apache.ignite.IgniteCompute;
import org.apache.ignite.Ignition;
import org.apache.ignite.cluster.ClusterGroup;
import org.apache.ignite.configuration.IgniteConfiguration;

public class OldestAndNewest {
public static void main(String[] args) {
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
try (Ignite ignite = Ignition.start(cfg)) {
IgniteCluster cluster = ignite.cluster();

//Get the oldest node
ClusterGroup forOldest = cluster.forOldest();

//Get a compute task for the oldest servers
IgniteCompute compute = ignite.compute(forOldest);

//broadcast the computation to the oldest node
compute.broadcast(() -> {
System.out.println("************** Old is Gold!!! *******************");
});


//Get the youngest node
ClusterGroup forYoungest = cluster.forYoungest();

//Get a compute task for the youngest node
compute = ignite.compute(forYoungest);

//broadcast the computation to the youngest node
compute.broadcast(() -> {
System.out.println("************ Young blood!!! *************");
});
}
}
}

The oldest ignite instance will print the text Old is gold. Verify that the other server instance didn't print anything:

As the program itself is the youngest server node, it will print Young blood:

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

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