Alex
Alex

Reputation: 1535

Infinispan Unique Cache Manager for deployed Web Applications

I'm working with Infinispan 8.1 and WildFly 10.

I initialize my CacheManager programmatically using these code lines:

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.

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:

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. 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.

Is it possible? How can i obtain a global Cache Manager?

Upvotes: 2

Views: 1548

Answers (2)

Sebastian Łaskawiec
Sebastian Łaskawiec

Reputation: 2737

Ernest is right - MBean servers are per JVM not per ClassLoader, so you need to ignore duplicated domains. But what's more interesting - Wildfly uses Infinispan for session clustering, so the default cache manager might be already running. 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). This sounds reasonable in scenario you described.

Upvotes: 2

ernest_k
ernest_k

Reputation: 45319

It seems obvious that you're running this code in web modules (.war) -- or in jars bundled in war files. You cannot share instances across web modules as class-loaders are protected (and that's good for you).

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. Each web module will then get to the cache via the local EJB, with infinispan libs deployed in ear/lib.

  2. Run Infinispan server (standalone wildfly installation for Infinispan) and change your code to use the remote clients:

    -- 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).

    -- REST client (docs here: http://infinispan.org/docs/8.2.x/user_guide/user_guide.html#_infinispan_rest_server)

    Each web module can do this separately.

Upvotes: 1

Related Questions