简体   繁体   English

用于已部署Web应用程序的Infinispan Unique Cache Manager

[英]Infinispan Unique Cache Manager for deployed Web Applications

I'm working with Infinispan 8.1 and WildFly 10. 我正在使用Infinispan 8.1和WildFly 10。

I initialize my CacheManager programmatically using these code lines: 我使用以下代码行以编程方式初始化CacheManager:

public class SessionManager {
    private static DefaultCacheManager cacheManager;

    public void initializeCache(){

        if (cacheManager ==null){ 

            GlobalConfigurationBuilder gcbLocal = new GlobalConfigurationBuilder();
            ConfigurationBuilder builderLocal = new ConfigurationBuilder();
            builderLocal.clustering().cacheMode(CacheMode.LOCAL);
            cacheManager = new DefaultCacheManager(gcbLocal.build(), builderLocal.build()); 
            cacheManager.getCache(); 

These code lines belong to a jar imported as dependency in multiple web applications deployed on my server. 这些代码行属于作为依赖导入到我的服务器上部署的多个Web应用程序中的jar。

So every time i deploy a new application, the initialize method is invoked and infinispan tries to create a new DefaultCacheManager, giving me this exception: 因此,每次我部署一个新应用程序时,都会调用initialize方法,并且infinispan尝试创建一个新的DefaultCacheManager,给我这个例外:

ISPN000034: There's already a JMX MBean instance type=CacheManager,name="DefaultCacheManager" already registered under 'org.infinispan' JMX domain. If you want to allow multiple instances configured with same JMX domain enable 'allowDuplicateDomains' attribute in 'globalJmxStatistics' config element

This issue can be resolved simply adding this code line: 只需添加以下代码行即可解决此问题:

gcbLocal.globalJmxStatistics().allowDuplicateDomains(true);

But now the effect is that Infinispan will create a new domain separated CacheManager. 但是现在的效果是Infinispan将创建一个新的域,该域分隔CacheManager。 This means that every application will have its own. 这意味着每个应用程序都有其自己的。

My target is to have just 1 DefaultCacheManager serving all the web applications deployed inside the server the way that if WebApplicationA stores some value inside the infinispan cache, the webApplicationB can get it. 我的目标是仅使用1个DefaultCacheManager来服务部署在服务器内的所有Web应用程序,方法是,如果WebApplicationA在infinispan缓存中存储一​​些值,则webApplicationB可以获取它。

Is it possible? 可能吗? How can i obtain a global Cache Manager? 如何获得全局缓存管理器?

Ernest is right - MBean servers are per JVM not per ClassLoader, so you need to ignore duplicated domains. Ernest是正确的-MBean服务器是按JVM而不是按ClassLoader的,因此您需要忽略重复的域。 But what's more interesting - Wildfly uses Infinispan for session clustering, so the default cache manager might be already running. 但是更有趣的是-Wildfly使用Infinispan进行会话群集,因此默认的缓存管理器可能已经在运行。 I strongly recommend using your own cache manager name: 我强烈建议您使用自己的缓存管理器名称:

new GlobalConfigurationBuilder().globalJmxStatistics()
                    .cacheManagerName(CACHE_NAME).build();

Ernest also suggested using a HotRod Server cluster and connecting to it using a HotRod client (which is by far faster than using REST interface). Ernest还建议使用HotRod Server群集,并使用HotRod客户端(远比使用REST接口快得多)连接到该群集。 This sounds reasonable in scenario you described. 在您描述的场景中,这听起来很合理。

It seems obvious that you're running this code in web modules (.war) -- or in jars bundled in war files. 很明显,您正在Web模块(.war)中或在war文件中捆绑的jar中运行此代码。 You cannot share instances across web modules as class-loaders are protected (and that's good for you). 由于类加载器受到保护(因此对您有好处),因此您无法在Web模块之间共享实例。

You have a few options: 您有几种选择:

  1. Instead of deploying war files, make a single ear file with multiple web modules, and one EJB that will then create and use the cache manager. 不用部署war文件,而是制作一个带有多个Web模块的单耳文件,然后制作一个EJB,然后再创建并使用缓存管理器。 Each web module will then get to the cache via the local EJB, with infinispan libs deployed in ear/lib. 然后,每个Web模块将通过本地EJB到达缓存,并且inear / lib中部署了infinispan库。

  2. Run Infinispan server (standalone wildfly installation for Infinispan) and change your code to use the remote clients: 运行Infinispan服务器(Infinispan的独立wildfly安装),然后更改代码以使用远程客户端:

    -- HotRod client to connect to it externally (docs here: http://infinispan.org/docs/8.2.x/getting_started/getting_started.html#_using_hot_rod_to_access_an_infinispan_data_grid ). -HotRod客户端从外部连接到它(此处的文档: http ://infinispan.org/docs/8.2.x/getting_started/getting_started.html#_using_hot_rod_to_access_an_infinispan_data_grid)。

    -- REST client (docs here: http://infinispan.org/docs/8.2.x/user_guide/user_guide.html#_infinispan_rest_server ) -REST客户端(此处的文档: http : //infinispan.org/docs/8.2.x/user_guide/user_guide.html#_infinispan_rest_server

    Each web module can do this separately. 每个Web模块可以单独执行此操作。

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

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