作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在我的应用程序中使用 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 相关的 jar
但是,当我尝试从服务器调用 printObject() 方法时,它正在打印 null
13:37:24,206 INFO [stdout] (http--127.0.0.1-9090-1) defaultCacheManager:null
为什么会这样,如果我这边遗漏了什么,请指正。
最佳答案
试试这个
@Resource(lookup = "java:jboss/infinispan/container/test")
private EmbeddedCacheManager defaultCacheManager;
或者
这是一个示例代码:
配置.java:
它被定义为在服务器启动时初始化的托管 bean,它的默认构造函数被调用,它的引用存储在静态字段中,以便从应用程序的任何地方访问它。
配置类必须是单例的。不要在代码中的任何地方调用 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();
}
}
关于java - Jboss ,Infinispan, 如何将 Jboss 配置为托管缓存管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22246731/
我是一名优秀的程序员,十分优秀!