我正在嘗試將Infinispan的JCache集成到我現有的EJB項目中。註釋JCacheResult在Infinispan和Glassfish 3.1.1中不工作
我已將Infinispan 5.0.1 CDI和Core軟件包添加到Maven pom。 在beans.xml中添加了Infinispan攔截器,並且可以使用CacheResult註釋。
我在Glassfish 3.1.1中部署應用程序。我已經檢查了Weld jar版本,它是 模塊:org.jboss.weld.osgi-bundle:1.1.1.Final
在運行時,CacheResult方法攔截器沒有緩存方法結果,並且它總是被調用。
我的代碼看起來像這樣,
public void cacheTest() {
Thread.currentThread().setContextClassLoader(
this.getClass().getClassLoader());
EmbeddedCacheManager manager = createCacheConfig();
Set<String> cacheList = manager.getCacheNames(); // new
// DefaultCacheManager().getCacheNames();
for (String cache : cacheList) {
System.out.println("Cache name " + cache);
}
defaultCache = manager.getCache("test-cache");
defaultCache.put("aa", "AA");
String user = "User";
greet(user);
Set<String> keys = defaultCache.keySet();
for (String key : keys) {
System.out.println("Key is -" + key + "Value is -"
+ defaultCache.get(key));
}
}
@CacheResult(cacheName = "test-cache")
public String greet(@CacheKeyParam String user) {
user += "Hello";
return user;
}
public EmbeddedCacheManager createCacheConfig() {
EmbeddedCacheManager manager = new DefaultCacheManager();
Configuration conf = new Configuration();
conf.fluent().eviction().strategy(EvictionStrategy.FIFO).maxEntries(10)
.expiration().maxIdle(1200000L).build();
conf.fluent().clustering().sync();
manager.start();
manager.defineConfiguration("test-cache", conf);
return manager;
}
迎接()方法被調用,但它永遠不會在方法結果添加到測試緩存。我覺得我錯過了一些配置或...我不知道。請幫助我。
當我注入類,他們不會被構造,他們爲空。代碼是這樣的,
@Inject
private static org.infinispan.Cache<String, String> defaultCache;
@Inject
private static EmbeddedCacheManager defaultCacheManager;
這些被執行沒有任何錯誤,但他們不會被初始化。
我沒有線索......但我能夠輕鬆地在這個類中注入其他EJB。順便說一句,我試圖在EJB之一中添加Jcache功能。
我會感激你的幫助......
謝謝... 拉吉小號
感謝您的回覆。 –
感謝您的回覆。是的,我只在EJB中創建Jcache。我已經移除了Injected字段的靜態,並且抱怨出現以下錯誤 - SEVERE:加載應用程序時出現異常:WELD-001408帶有限定符的[DefaultCacheManager]類型的不滿足依賴項[@Default ]在注入點[[字段] @Inject private com.xxxx.xxxxx.xxx.CustomerFacade.defaultCacheManager]緩存攔截器 - 我已經將Infinispan jar打包到EAr中並進行了部署。仍然我的攔截器不叫... –
對於測試,你可以嘗試解開你的EJB項目中的infinispan cdi類嗎? (不要忘記添加META-INF/services文件夾和beans.xml)。看起來擴展不是主動的。 – kevinpollet