简体   繁体   English

Jboss,Infinispan,如何将Jboss配置为托管缓存管理器

[英]Jboss ,Infinispan, How to configure Jboss As managed cache manager

I am trying to use Jboss AS managed Infinispan in my application, so that I can use Jboss Admin console to manage it. 我试图在我的应用程序中使用Jboss AS管理的Infinispan ,以便可以使用Jboss Admin console进行管理。 I have tried the following steps based on Infinispan documentation, 我根据Infinispan文档尝试了以下步骤,

1) Created a class named Config 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) created a servlet just for making Config object and calling printObject() method 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) Added the application dependencies via standalone.xml global configuration 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)Added new cache container in subsystem named test 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)Removed all Infinispan related jars from my application 5)从我的应用程序中删除了所有与Infinispan相关的罐子

But, When I try to call the printObject() method from servel it is printing null 但是,当我尝试从servel调用printObject()方法时,它正在打印null

13:37:24,206 INFO [stdout] (http--127.0.0.1-9090-1) defaultCacheManager:null 13:37:24,206 INFO [stdout](http--127.0.0.1-9090-1) defaultCacheManager:null

Why it is happening , please correct me if any thing is missed from my side. 为什么会发生这种情况,如果我身边错过任何事情,请纠正我。

Try this one 试试这个

@Resource(lookup = "java:jboss/infinispan/container/test")
private EmbeddedCacheManager defaultCacheManager;

Or 要么

Here is a sample code: 这是一个示例代码:

Config.java: Config.java:

Its defined as managed bean that is initialized at the time of server start-up and its default constructor is called where its reference is stored in a static field to access it from anywhere in the application. 它的定义是在服务器启动时初始化的托管Bean,其默认构造函数称为在其引用存储在静态字段中以从应用程序中的任何位置访问它的默认构造函数。

Config class must be as singleton. Config类必须为单例。 Do not call new Config() any where in code. 不要在代码中的任何位置调用new Config() It is created by server itself. 它是由服务器本身创建的。

Call Config.getInstance() whenever it is needed in the application. 在应用程序中需要时调用Config.getInstance()

It also handle the condition where configuration is missing (on dev machine). 它还可以处理缺少配置的情况(在开发机上)。

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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM