Listening to Cache Entry Created events

Whenever a new Cache entry is created, 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.CacheEntryCreatedListener interface as shown below


import java.util.Iterator;

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

public class CacheEntryCreatedListenerImpl implements CacheEntryCreatedListener<String, String> {
    @Override
    public void onCreated(Iterable<CacheEntryEvent<? extends String, ? extends String>> iterable) throws CacheEntryListenerException {
        Iterator<CacheEntryEvent<? extends String, ? extends String>> iterator = iterable.iterator();
        for(;iterator.hasNext();) {
            CacheEntryEvent<? extends String, ? extends String> cacheEntryEvent = iterator.next();
            System.out.println(cacheEntryEvent.getKey() + "-->" + cacheEntryEvent.getValue());
        }
    }
}

In the above code, we have created a class named “CacheEntryCreatedListenerImpl” that implements the interface “CacheEntryCreatedListener” and provides implementation for the interface method “onCreated”. This method is called whenever a new cache entry is created. 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 JCacheDemo9 {
10  public static void main(String[] args) {
11      CachingProvider cachingProvider = Caching.getCachingProvider();
12      CacheManager cacheManager = cachingProvider.getCacheManager();
13      
14      MutableConfiguration<String, String> mutableConfiguration = new MutableConfiguration<String, String>();
15      mutableConfiguration.setTypes(String.class, String.class);
16      
17      MutableCacheEntryListenerConfiguration<String, String> mutableCacheEntryListenerConfiguration = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(CacheEntryCreatedListenerImpl.class), null, true, false);
18      
19      Cache<String, String> 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.deregisterCacheEntryListener(mutableCacheEntryListenerConfiguration);
27      
28      cachingProvider.close();
29  }
30 }

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 CacheEntryCreatedListenerImpl
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 new entry is created in 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–>value1
key2–>value2
key3–>value3
key4–>value4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s