Listening to Cache Entry Update events

Whenever a Cache entry is updated, an event (i.e., CacheEntryEvent) is generated.

In this post under JCache, I will show with an example how to create a listener to listen for those events and respond.

We have to create a listener class which implements the javax.cache.event.CacheEntryUpdatedListener interface as shown below


import java.util.Iterator;

import javax.cache.event.CacheEntryEvent;
import javax.cache.event.CacheEntryListenerException;
import javax.cache.event.CacheEntryUpdatedListener;

public class CacheEntryUpdatedListenerImpl implements CacheEntryUpdatedListener {
    @Override
    public void onUpdated(Iterable iterable)
            throws CacheEntryListenerException {
        Iterator iterator = iterable.iterator();
        for(;iterator.hasNext();) {
            CacheEntryEvent cacheEntryEvent = iterator.next();
            System.out.println(cacheEntryEvent.getKey() + "-->" + cacheEntryEvent.getValue());
        }
    }
}

In the above code, we have created a class named “CacheEntryUpdatedListenerImpl” that implements the interface “CacheEntryUpdatedListener” and provides implementation for the interface method “onUpdated”. This method is called whenever a cache entry is updated. It receives a list of CacheEntryEvent as an parameter, whose details we are printing in the for loop.

Next I will show how to register the listener to the cache.

Main Code


1  import javax.cache.Cache;
2  import javax.cache.CacheManager;
3  import javax.cache.Caching;
4  import javax.cache.configuration.FactoryBuilder;
5  import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
6  import javax.cache.configuration.MutableConfiguration;
7  import javax.cache.spi.CachingProvider;
8  
9  public class JCacheDemo11 {
10  public static void main(String[] args) {
11      CachingProvider cachingProvider = Caching.getCachingProvider();
12      CacheManager cacheManager = cachingProvider.getCacheManager();
13      
14      MutableConfiguration mutableConfiguration = new MutableConfiguration();
15      mutableConfiguration.setTypes(String.class, String.class);
16      
17      MutableCacheEntryListenerConfiguration mutableCacheEntryListenerConfiguration = new MutableCacheEntryListenerConfiguration(FactoryBuilder.factoryOf(CacheEntryUpdatedListenerImpl.class), null, true, false);
18      
19      Cache cache = cacheManager.createCache("cache1", mutableConfiguration);
20      cache.registerCacheEntryListener(mutableCacheEntryListenerConfiguration);
21      cache.put("key1", "value1");
22      cache.put("key2", "value2");
23      cache.put("key3", "value3");
24      cache.put("key4", "value4");
25      cache.put("key1", "value");
26      
27      cache.deregisterCacheEntryListener(mutableCacheEntryListenerConfiguration);
28      
29      cachingProvider.close();
30  }
31 }

In the above code, at line 17, we create an instance of MutableCacheEntryListenerConfiguration. It takes the following as arguments
1) Factory instance to create instances of CacheEntryUpdatedListenerImpl
2) Factory instance to create instance of CacheEntryEventFilter implementation
3) a boolean value indicating whether old value is required or not
4) a boolean value indicating whether the listenerFactory should block the thread causing the event or not

At line 20, we register the instance of MutableCacheEntryListenerConfiguration with the cache by calling registerCacheEntryListener method.

From now on whenever a entry is updated from the cache, our custom listener will be called.

At line 26, we deregister the instance of MutableCacheEntryListenerConfiguration from the cache by calling deregisterCacheEntryListener method.

Output

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
key1–>value

Leave a Reply