Listening to Cache Entry Removed events

Whenever a Cache entry is removed, 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.CacheEntryRemovedListener interface as shown below


import java.util.Iterator;

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

public class CacheEntryRemovedListenerImpl implements CacheEntryRemovedListener {
    @Override
    public void onRemoved(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 “CacheEntryRemovedListenerImpl” that implements the interface “CacheEntryRemovedListener” and provides implementation for the interface method “onRemoved”. This method is called whenever a cache entry is removed. 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 JCacheDemo10 {
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(CacheEntryRemovedListenerImpl.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      
26      cache.remove("key4");
27      
28      cache.deregisterCacheEntryListener(mutableCacheEntryListenerConfiguration);
29      
30      cachingProvider.close();
31  }
32 }

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 CacheEntryRemovedListenerImpl
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 removed 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.
key4–>value4


Check these products on Amazon
Advertisements

Leave a Reply