我正在使用具有此配置的maven项目:
Maven infinispan-jcache:
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-jcache</artifactId>
<version>7.1.0.Final</version>
</dependency>
然后我用这种方式用@CacheResult注释定义一个方法:
@CacheResult(cacheName = "customCacheName")
public Double getCustomAttribute(String id){
[...]
}
我想在将对象添加到缓存时执行某些操作,因此我开始使用CacheEntryEventListener。我用这种方式开发了自定义监听器:
public class MyCustomCacheEntryEventListener implements CacheEntryCreatedListener<Object, Object>, Serializable
{
public MyCustomCacheEntryEventListener()
{
}
@Override
public void onCreated(Iterable<CacheEntryEvent< ? extends Object, ? extends Object>> events)
throws CacheEntryListenerException
{
LOG.info("onCreated invoked");
for (CacheEntryEvent< ? extends Object, ? extends Object> event : events) {
LOG.info("Received a " + event);
}
}
}
要完成配置,我在@Startup bean中调用了这段代码:
CachingProvider provider = Caching.getCachingProvider();
CacheManager manager = provider.getCacheManager();
CacheEntryListenerConfiguration<Object, Object> listenerConfiguration =
new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(new MyCustomCacheEntryEventListener()),
null, false, true);
CompleteConfiguration<Object, Object> config = new MutableConfiguration<>().setTypes(Object.class, Object.class).addCacheEntryListenerConfiguration(listenerConfiguration);
manager.createCache("customCacheName", config).registerCacheEntryListener(listenerConfiguration);
JCache注释工作正常。我还将 TRACE 日志记录添加到org.infinispan以获取更多信息。
不会触发Entry事件侦听器。
感谢任何帮助。 问候, 涓