Click on Javadoc link to open Javadoc documentation.
Package
org.gridgain.grid.spi.loadbalancing.affinity Javadoc![]()
Available starting with GridGain 
Description
GridCoherenceLoadBalancingSpi ![]()
| Usage Note, that instead of regular GridJob |
Coding Example
To use load balancers for your job routing, in your GridTask.map(List, Object) ![]()
![]()
![]()
![]()
![]()
Here is an example of a grid task that uses affinity load balancing. Note how load balancing jobs is absolutely transparent to the user and is simply a matter of proper grid configuration.
Here is an example of a grid task that uses affinity load balancing. Note how load balancing jobs is absolutely transparent to the user and is simply a matter of proper grid configuration.
public class MyFooBarCoherenceAffinityTask extends GridTaskSplitAdapter<List<Integer>,Object> { // For this example we receive a list of cache keys and for every key // create a job that accesses it. @Override protected Collection<? extends GridJob> split(int gridSize, List<Integer> cacheKeys) throws GridException { List<MyGridAffinityJob> jobs = new ArrayList<MyGridAffinityJob>(gridSize); for (Integer cacheKey : cacheKeys) { jobs.add(new MyFooBarCoherenceAffinityJob(cacheKey)); } // Node assignment via load balancer // happens automatically. return jobs; } ... }
Here is the example of grid jobs created by the task above:
public class MyFooBarCoherenceAffinityJob extends GridCoherenceAffinityJobAdapter<Integer, Serializable> { ... private static final String CACHE_NAME = "myDistributedCache"; public MyFooBarCoherenceAffinityJob(Integer cacheKey) { super(CACHE_NAME, cacheKey); } public Serializable execute() throws GridException { ... // Access data by the same key returned in 'getAffinityKey()' method // and for cache with name returned in 'getCacheName()'. NamedCache mycache = CacheFactory.getCache(getCacheName); mycache.get(getAffinityKey()); ... } }
Configuration
This SPI has no configuration parameters.
Examples
As any GridGain SPI, GridCoherenceLoadBalancingSpi ![]()
GridCoherenceLoadBalancingSpi spi = new GridCoherenceLoadBalancingSpi(); GridConfigurationAdapter cfg = new GridConfigurationAdapter(); // Override default load balancing SPI. cfg.setLoadBalancingSpi(spi); // Start grid. GridFactory.start(cfg);
or from Spring configuration file
<bean id="grid.custom.cfg" class="org.gridgain.grid.GridConfigurationAdapter" singleton="true"> ... <property name="loadBalancingSpi"> <bean class="org.gridgain.grid.spi.loadbalancing.coherence.GridCoherenceLoadBalancingSpi"/> </property> ... </bean>

For more information about using Spring framework for configuration click here.
