注释 JCacheResult 在 Infinispan 和 Glassfish 3.1.1 中不起作用
我正在尝试将 Infinispan 的 JCache 集成到我现有的 EJB 项目中。
我已将 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;
}
greet() 方法被调用,但它永远不会将方法结果添加到测试缓存中。我觉得我缺少一些配置或者......我不知道。请帮我解决这个问题。
当我注入类时,它们不会被构造并且它们为空。代码是这样的,
@Inject
private static org.infinispan.Cache<String, String> defaultCache;
@Inject
private static EmbeddedCacheManager defaultCacheManager;
这些执行没有任何错误,但它们不会被初始化。
我不知道...但我可以轻松地在此类中注入其他 EJB。顺便说一句,我正在尝试在其中一个 EJB 中添加 Jcache 功能。
我将不胜感激你的帮助...
谢谢... 拉吉·S
I am trying to integrate JCache from Infinispan into my existing EJB project.
I have added Infinispan 5.0.1 CDI and Core packages to Maven pom.
Added Infinispan Interceptors in beans.xml and able to use the CacheResult annotation.
I am deploying the app in Glassfish 3.1.1. I have checked the Weld jar version, which is
Module : org.jboss.weld.osgi-bundle:1.1.1.Final
In the runtime, the CacheResult Method interceptor is not caching the method result and its always called.
My code looks like this,
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;
}
greet() method gets called but it will never add the method result to the test-cache. I feel am I missing some configuration or...I dont know. Please help me on this.
when I Inject the classes, they wont get constructed and they are null. The code is like this,
@Inject
private static org.infinispan.Cache<String, String> defaultCache;
@Inject
private static EmbeddedCacheManager defaultCacheManager;
These gets executed without any error, but they wont get initialized.
I have no clue...But I am able to inject other EJBs with in this class easily. By the way I am trying to add Jcache functionality in one of EJBs.
I would appreciate your help...
Thanks...
Raj S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 greet 方法位于 CDI bean 或 EJB 中,对吗?
JCache 注释中定义的缓存在 Infinispan CDI 提供的缓存管理器中查找。此缓存管理器包含使用 CDI 配置的缓存(有关详细信息,请参阅 https:// docs.jboss.org/author/display/ISPN/CDI+Support)。在您的示例中,test-cache 配置将不起作用。
另一件事,如果您的 cacheTest 和 greet 方法位于同一个类中,则 greet 方法无法被拦截。如果情况并非如此,您可能会遇到 GLASSFISH-17184。
对于 Cache 和 EmbeddedCacheManager 注入,问题在于您正在进行静态注入,CDI 不支持。来自 CDI (JSR-299) 规范
如果你的方法结果没有被缓存,我认为这是因为CacheResultInterceptor没有被调用。我刚刚使用 Infinispan CDI 快速入门进行了测试。如果拦截器位于库中,则不会启用它们。我认为这是 Glassfish 中的一个错误。
顺便说一句,您可以在此处查看 Infinispan CDI 快速入门中的代码示例。
希望这有帮助!
Your greet method is in a CDI bean or in an EJB, right?
The cache defined in JCache annotations is looked up in the cache manager provided by Infinispan CDI. This cache manager contains the cache configured with CDI (for more information, see https://docs.jboss.org/author/display/ISPN/CDI+Support). In your example the test-cache configuration will have no effect.
Another thing, if your cacheTest and greet methods are in the same class the greet method cannot be intercepted. If that's not the case maybe you're hitting GLASSFISH-17184.
For the Cache and EmbeddedCacheManager injections the problem is that you're doing a static injection, not supported by CDI. From CDI (JSR-299) specification
If your method result is not cached, I think it's because the CacheResultInterceptor is not called. I've just made the test with the Infinispan CDI quickstart. If the interceptors are in a lib they are not enabled. I think it's a bug in Glassfish.
Btw, you can see an example of code in the Infinispan CDI quickstart here.
Hope this help!