Hi,
I am trying to fire events using JCache with Ehcache as backend cache provider.
I tried this simple example using programmatic cache creation approach.But none of the events are fired.
Below program runs with following exception :
Listenr added : true
Exception in thread "main" java.lang.IllegalArgumentException: null source
at java.util.EventObject.<init>(Unknown Source)
at javax.cache.event.CacheEntryEvent.<init>(CacheEntryEvent.java:26)
at net.sf.ehcache.jcache.JCacheEntryEventAdapter.<init>(JCacheEntryEventAdapter.java:38)
at net.sf.ehcache.jcache.JCacheListenerAdapter.notifyElementPut(JCacheListenerAdapter.java:121)
at net.sf.ehcache.event.RegisteredEventListeners.notifyListener(RegisteredEventListeners.java:294)
at net.sf.ehcache.event.RegisteredEventListeners.invokeListener(RegisteredEventListeners.java:284)
at net.sf.ehcache.event.RegisteredEventListeners.internalNotifyElementPut(RegisteredEventListeners.java:144)
at net.sf.ehcache.event.RegisteredEventListeners.notifyElementPut(RegisteredEventListeners.java:122)
at net.sf.ehcache.Cache.notifyPutInternalListeners(Cache.java:1485)
at net.sf.ehcache.Cache.putInternal(Cache.java:1460)
at net.sf.ehcache.Cache.put(Cache.java:1387)
at net.sf.ehcache.Cache.put(Cache.java:1352)
at net.sf.ehcache.constructs.EhcacheDecoratorAdapter.put(EhcacheDecoratorAdapter.java:119)
at net.sf.ehcache.jcache.JCacheEhcacheDecorator.put(JCacheEhcacheDecorator.java:84)
at net.sf.ehcache.jcache.JCache.put(JCache.java:245)
at com.tibco.events.other.TestCacheEvents.testEvents(TestCacheEvents.java:28)
at com.tibco.events.other.TestCacheEvents.<init>(TestCacheEvents.java:19)
at com.tibco.events.other.TestCacheEvents.main(TestCacheEvents.java:31)
Why events are not getting fired? As per my knowledge ehcache wrapper implementation provides complete implementation of JSR 107(except transaction) or Am I missing something?
I will be thankful if you provide a sample example which can demonstrate JCache Events using ehcache as backend cache?
Code Details are:
JSR 107 JCache Api - 0.5 version
Ehcache-Jcache api - ehcache-jcache-1.5.0-0.5.jar
JDK version 1.6
I included all other dependencies
Here is the code :
Code:
public class TestCacheEvents {
CacheManager cacheManager;
Cache<Object,Object> cache;
private String cacheName = "inventory";
private CacheEntryListener cacheEntryListener;
public TestCacheEvents()
{
cache = Caching.getCacheManager().createCacheBuilder(cacheName).build();
cacheEntryListener = new MyListener();
registerListener();
testEvents();
}
public void registerListener()
{
boolean isRegistered = cache.registerCacheEntryListener(cacheEntryListener);
System.out.println("Listenr added : "+isRegistered);
}
public void testEvents()
{
cache.put("abc", "xyz");
cache.get("abc");
cache.remove("abc");
}
public static void main(String[] args) {
new TestCacheEvents();
}
}
public class MyListener implements CacheEntryCreatedListener<Object,Object>,CacheEntryRemovedListener<Object, Object>, CacheEntryReadListener<Object, Object> {
@Override
public void entryRead(
CacheEntryEvent<? extends Object, ? extends Object> event)
throws CacheEntryListenerException {
System.out.println("Entry Read Event Fired : "+event.getValue());
}
@Override
public void entryCreated(
CacheEntryEvent<? extends Object, ? extends Object> event)
throws CacheEntryListenerException {
System.out.println("Entry Created Event Fired : "+event.getValue());
}
@Override
public void entryRemoved(
CacheEntryEvent<? extends Object, ? extends Object> event)
throws CacheEntryListenerException {
System.out.println("Entry Removed Event Fired :"+event.getValue());
}
}