public static void main(String[] args) throws IgniteException {
// Preparing IgniteConfiguration using Java APIs
IgniteConfiguration cfg = new IgniteConfiguration(); // The node will be started as a client node.
cfg.setClientMode(true); // Classes of custom Java logic will be transferred over the wire from this app.
cfg.setPeerClassLoadingEnabled(true); // Setting up an IP Finder to ensure the client can locate the servers.
TcpDiscoveryMulticastIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder();
ipFinder.setAddresses(Collections.singletonList("127.0.0.1:47500..47509"));
cfg.setDiscoverySpi(new TcpDiscoverySpi().setIpFinder(ipFinder)); // Starting the node
Ignite ignite = Ignition.start(cfg); // Create an IgniteCache and put some values in it.
IgniteCache cache = ignite.getOrCreateCache("myCache");
cache.put(1, "Hello");
cache.put(2, "World!"); System.out.println(">> Created the cache and add the values."); // Executing custom Java compute task on server nodes.
ignite.compute(ignite.cluster().forServers()).broadcast(new RemoteTask()); System.out.println(">> Compute task is executed, check for output on the server nodes."); // Disconnect from the cluster.
ignite.close();
} /**
* A compute tasks that prints out a node ID and some details about its OS and JRE.
* Plus, the code shows how to access data stored in a cache from the compute task.
*/
private static class RemoteTask implements IgniteRunnable {
@IgniteInstanceResource
Ignite ignite; @Override
public void run() {
System.out.println(">> Executing the compute task"); System.out.println(
" Node ID: " + ignite.cluster().localNode().id() + "\n" +
" OS: " + System.getProperty("os.name") +
" JRE: " + System.getProperty("java.runtime.name")); IgniteCache cache = ignite.cache("myCache"); System.out.println(">> " + cache.get(1) + " " + cache.get(2));
}
}