30.5. Key Affinity Service The key affinity service allows a value to be placed in a certain node in a distributed Red Hat JBoss Data Grid cluster. The service returns a key that is hashed to a particular node based on a supplied cluster address identifying it. The keys returned by the key affinity service cannot hold any meaning, such as a username. These are only random identifiers that are used throughout the application for this record. The provided key generators do not guarantee that the keys returned by this service are unique. For custom key format, you can pass your own implementation of KeyGenerator. The following is an example of how to obtain and use a reference to this service. Example 30.2. Key Affinity Service The following procedure is an explanation of the provided example. Procedure 30.3. Using the Key Affinity Service Obtain a reference to a cache manager and cache. This starts the service, then uses the supplied Executor to generate and queue keys. Obtain a key from the service which will be mapped to the local node ( cacheManager.getAddress() returns the local address). The entry with a key obtained from the KeyAffinityService is always stored on the node with the provided address. In this case, it is the local node. Report a bug 30.5.1. Lifecycle KeyAffinityService extends Lifecycle , which allows the key affinity service to be stopped, started, and restarted. Example 30.3. Key Affinity Service Lifecycle Parameter The service is instantiated through the KeyAffinityServiceFactory . All factory methods have an Executor , that is used for asynchronous key generation, so that this does not occur in the caller's thread. The user controls the shutting down of this Executor . The KeyAffinityService must be explicitly stopped when it is no longer required. This stops the background key generation, and releases other held resources. The KeyAffinityServce will only stop itself when the cache manager with which it is registered is shut down. Report a bug 30.5.2. Topology Changes KeyAffinityService key ownership may change when a topology change occurs. The key affinity service monitors topology changes and updates so that it doesn't return stale keys, or keys that would map to a different node than the one specified. However, this does not guarantee that a node affinity hasn't changed when a key is used. For example: Thread ( T1 ) reads a key ( K1 ) that maps to a node ( A ). A topology change occurs, resulting in K1 mapping to node B . T1 uses K1 to add something to the cache. At this point, K1 maps to B , a different node to the one requested at the time of read. The above scenario is a not ideal, however it is a supported behavior for the application, as the keys that are already in use may be moved over during cluster change. The KeyAffinityService provides an access proximity optimization for stable clusters, which does not apply during the instability of topology changes. Report a bug

Commands:
EmbeddedCacheManager cacheManager = getCacheManager(); Cache cache = cacheManager.getCache(); KeyAffinityService keyAffinityService = KeyAffinityServiceFactory.newLocalKeyAffinityService( cache, new RndKeyGenerator(), Executors.newSingleThreadExecutor(), 100); Object localKey = keyAffinityService.getKeyForAddress(cacheManager.getAddress()); cache.put(localKey, "yourValue");
public interface Lifecycle { void start(); void stop(); }