The Apache Ignite clustering API provides a class, ClusterGroup,
for the logical grouping of cluster nodes. Let's start with broadcasting a task to all the nodes of a cluster. Create a new class and add the following lines:
package com.packt;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCompute;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
public class BroadcastAll {
public static void main(String[] args) {
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
compute.broadcast(() -> {
System.out.println("Broadcasting to all nodes");
});
}
}
}
The program starts with a configuration setting peerClassLoadingEnabled=true, then gets a compute object from ignite and broadcasts the computation to all the nodes of your cluster.
Open the default-config.xml file under the config directory of the Ignite installation folder. The ignite servers start up with this default configuration file. We need to enable the peerClassLoadingEnabled flag to send a computational task to a server node. Add the peerClassLoadingEnabled flag value:
<bean id="grid.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="peerClassLoadingEnabled" value="true"/>
</bean>
Start two Ignite server instances:
Run the program, and check the two ignite server consoles and the program console: