In order to natively integrate with JBoss Cache, GridGain is configured to run on top of JGroups. In order to make sure that same cache key access always happens on the same node, we use GridAffinityLoadBalancingSpi ![]()
See GRIDGAIN_HOME/config/jbosscache/spring-jbosscache.xml configuration file.
This example does the following:
- Creates a list of cache keys to access (list of integers from 1 to 10).
- Pass this list as an argument for execution of GridJbossCacheExampleTask.java task.
- The task will create a job for every key passed in. By a virtue of using GridAffinityLoadBalancingSpi
Javadoc underneath, jobs with same keys will always be mapped to the same nodes.
- The task will return a map of cache keys mapped to the nodes they were accessed on.
- The example demonstrates that regardless of how many times this task is executed, same cache keys are always accessed on the same nodes.
Starting Remote Nodes
To try this example you should start remote grid instances. You can start as many as you like by executing the following script (which simply invokes GridJbossCacheExampleNodeLoader.java): GRIDGAIN_HOME/examples/jbosscache/start-node.bat|sh
Once remote instances are started, you can execute this example from Eclipse, Idea, or NetBeans (or any other IDE) by simply hitting run button. You will witness that all nodes discover each other and will participate in task execution. Every node will print out the affinity key of the job it executes.
Full Source Code
package org.gridgain.examples.jbosscache; import org.gridgain.grid.*; import org.gridgain.grid.spi.loadbalancing.affinity.*; import java.util.*; public final class GridJbossCacheExample { /** * Starts JBoss Cache example. * * @param args Command line arguments (non required). * @throws GridException If example failed. */ public static void main(String[] args) throws GridException { try { Grid grid = GridJbossCacheExampleNodeLoader.loadNode(); List<Integer> cacheKeys = new ArrayList<Integer>(); // Generate cache keys. for (int cacheKey = 0; cacheKey < 10; cacheKey++) { cacheKeys.add(cacheKey); } // Shuffle keys just to demonstrate that // ordering of keys does not matter for // mapping keys to nodes. Collections.shuffle(cacheKeys); // Execute example task. This task will use GridGain affinity // load balancer to map jobs to nodes. Map<Integer, UUID> res1 = grid.execute(GridJbossCacheExampleTask.class, cacheKeys).get(); System.out.println(">>>"); System.out.println(">>> Finished first task execution: " + res1); System.out.println(">>>"); // Shuffle keys just to demonstrate that // ordering of keys does not matter for // mapping keys to nodes. Collections.shuffle(cacheKeys); // Execute example task the second time. Since // affinity load balancer is used, jobs will be // mapped to the same nodes as during first execution. Map<Integer, UUID> res2 = grid.execute(GridJbossCacheExampleTask.class, cacheKeys).get(); System.out.println(">>>"); System.out.println(">>> Finished second task execution: " + res2); System.out.println(">>> Node that we accessed percisely the same cache keys as in first execution."); System.out.println(">>>"); // Compare results from both task executions. // Since cache keys should always be mapped to // the same nodes, the results should be equal. assert res1.equals(res2) == true; System.out.println(">>>"); System.out.println(">>> Task results from both executions are equal."); System.out.println(">>>"); } finally { GridJbossCacheExampleNodeLoader.unloadNode(); } } /** * No-op constructor to ensure singleton. */ private GridJbossCacheExample() { // No-op. } }
