我正在尝试在我的应用中使用Jboss AS
托管Infinispan
,以便我可以使用Jboss Admin console
进行管理。
我已经尝试了基于Infinispan文档的以下步骤,
1)创建了一个名为Config的类
import javax.annotation.Resource;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import org.infinispan.manager.EmbeddedCacheManager;
public class Config {
@Produces
@ApplicationScoped
@Resource(lookup = "java:jboss/infinispan/test")
private EmbeddedCacheManager defaultCacheManager;
public void printObject() {
System.out.println("defaultCacheManager:" + defaultCacheManager);
}
}
2)创建了一个servlet,用于创建Config对象并调用printObject()方法
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 3200037917839533696L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doIt(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doIt(req, resp);
}
protected void doIt(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Config config = new Config();
config.printObject();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3)通过standalone.xml全局配置添加了应用程序依赖性
<subsystem xmlns="urn:jboss:domain:ee:1.0">
<global-modules>
<module name="org.infinispan" slot="main"/>
<module name="javax.enterprise.api" slot="main"/>
<module name="javax.faces.api" slot="main"/>
<module name="javax.inject.api" slot="main"/>
<module name="javax.annotation.api" slot="main"/>
</global-modules>
</subsystem>
4)在名为test
的子系统中添加了新的缓存容器<subsystem xmlns="urn:jboss:domain:infinispan:1.2" default-cache-container="test">
<cache-container name="test" default-cache="entity" start="EAGER">
<local-cache name="entity"/>
</cache-container>
</subsystem>
5)从我的应用程序中删除了所有与Infinispan相关的罐子
但是,当我尝试从servel调用printObject()方法时,它正在打印 null
13:37:24,206 INFO [stdout](http - 127.0.0.1-9090-1) defaultCacheManager:null
为什么会发生这种情况,如果我身边遗漏任何事情,请纠正我。
答案 0 :(得分:6)
试试这个
@Resource(lookup = "java:jboss/infinispan/container/test")
private EmbeddedCacheManager defaultCacheManager;
或
以下是示例代码:
<强> Config.java:强>
它被定义为在服务器启动时初始化的托管bean,并调用其默认构造函数,其引用存储在静态字段中,以便从应用程序的任何位置访问它。
Config类必须是单例。不要在代码中的任何位置调用new Config()
。它由服务器本身创建。
在应用程序中需要时调用Config.getInstance()
。
它还处理缺少配置的情况(在开发机器上)。
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.infinispan.Cache;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
@ManagedBean(name = "Config", eager = true)
@ApplicationScoped
public class Config {
@Resource(name = "java:jboss/infinispan/container/test")
private CacheContainer container;
private Cache<String, Object> entityCache = null;
private static Config configInstance = null;
public Config() {
configInstance = this;
}
@PostConstruct
public void start() {
entityCache = container.getCache("entity");
}
public static Config getInstance() {
if (configInstance == null) {
createInstance();
}
return configInstance;
}
public Cache<String, Object> getEntityCache() {
return entityCache;
}
private static void createInstance() {
configInstance = new Config();
if (configInstance.container == null) {
ConfigurationBuilder confBuilder = new ConfigurationBuilder();
confBuilder.clustering().cacheMode(CacheMode.LOCAL);
EmbeddedCacheManager cacheManager = new DefaultCacheManager(confBuilder.build());
cacheManager.start();
configInstance.container = cacheManager;
}
configInstance.start();
}
}