Search code examples
ehcacheehcache-3

ListenerObject not found in imports for Ehcache 3?


I am trying to implement a listener for an Ehcache 3.3.1 project using the code below. Can anyone suggest a solution for the ListenerObject? I can't seem to find it anywhere,except on the docs page I got the code from

import java.util.logging.Level;
import java.util.logging.Logger;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.event.EventType;

public class CacheHandler{
     private Logger LOG = Logger.getLogger(this.getClass().getName());
     private String cacheName="basicCache";
     public Cache cache;

     public CacheHandler(){
         if(cache==null)
             cache=initCache();
     }
     private Cache initCache(){
       CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
           .newEventListenerConfiguration(new ListenerObject(), EventType.CREATED, EventType.UPDATED) 
           .unordered().asynchronous(); 

       final CacheManager manager = CacheManagerBuilder.newCacheManagerBuilder()
           .withCache(cacheName,
               CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10))
                   .add(cacheEventListenerConfiguration) 
           ).build(true);

       final Cache<String, String> cache = manager.getCache("foo", String.class, String.class);
       return cache;
     }

     public Cache getCache(){
        if(cache==null)
            cache=initCache();

        return cache;           
     }
}

Solution

  • It is indeed not mentioned but since it is only one method it is normally easy to figure out.

    Here is an example:

    public class ListenerObject implements CacheEventListener<Object, Object> {
      @Override
      public void onEvent(CacheEvent<? extends Object, ? extends Object> event) {
        System.out.println(event);
      }
    }
    

    The real one used in the documentation is here.

    Then, I've played a bit with your code to real production usable code.

    public class CacheHandler implements AutoCloseable {
      private static final String CACHE_NAME = "basicCache";
      private final Cache<String, String> cache;
      private final CacheManager cacheManager;
    
      public CacheHandler() {
        cacheManager = initCacheManager();
        cache = cacheManager.getCache(CACHE_NAME, String.class, String.class);
      }
    
      private CacheManager initCacheManager(){
        CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
          .newEventListenerConfiguration(new ListenerObject(), EventType.CREATED, EventType.UPDATED)
          .ordered().synchronous();
    
        return CacheManagerBuilder.newCacheManagerBuilder()
          .withCache(CACHE_NAME,
            CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10))
              .add(cacheEventListenerConfiguration)
          ).build(true);
      }
    
      public Cache getCache(){
        return cache;
      }
    
      @Override
      public void close() {
        cacheManager.close();
      }
    
      public static void main(String[] args) {
        try(CacheHandler handler = new CacheHandler()) {
          Cache<String, String> cache = handler.getCache();
          cache.put("a", "b");
          cache.putIfAbsent("a", "c");
        }
    
      }
    }
    

    Some comments:

    • I assumed you want singleton cache kept in a variable. So that's what I did. The lazy initCache wasn't useful because the withCache tells Ehcache to create the cache when creating the cache manager.
    • We will want to keep a reference to the CacheManager in order to close it at the end.
    • The getCache was retrieving "foo", not "basicCache"