Is there a way to set up cache listneners such that the listners run on the same node where the cache entry has been inserted.
I want to run some processing on the grid for every cache entry that is put
into the data cache. For performance, I want to run this processing on the same node where the entry exists. Whats the best way to achieve this?
If you subscribe a cache event listener in GridGain, then it will be executed exactly on the node where the event happened. Take a look at CacheEventsExample on Github or in GridGain distro.
Here is code taken from CacheEventsExaple which subscribes optional local and remote listener for specific cache events:
GridFuture<UUID> fut = g.forCache(CACHE_NAME).events().remoteListen(locLsnr, rmtLsnr,
EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
// Wait until event listeners are subscribed on all nodes.
fut.get();
Note that for event notifications to work, they must be enabled in configuration, either from code or in XML configuration file. Here is XML snippet:
<property name="includeEventTypes">
<util:constant static-field="org.gridgain.grid.events.GridEventType.EVTS_CACHE"/>
</property>
To ignore events on backup nodes, you can have this logic in your remote listener
GridNode node = cache.affinity.mapPartitionToNode(event.partition());
// If we are on primary node.
if (node.isLocal())
// Process event.
I also recommend watching Distributed Events screencast on GridGain website.