Could not register JMX administration bean in JCS

1.5k Views Asked by At

Several webapps in a tomcat server instantiate a JCS 2.1 CompositeCacheManager instance through CompositeCacheManager#getUnconfiguredInstance(). This method registers a JMX bean (JCSAdminBean) and it raises a warning because of an InstanceAlreadyExistsException exception when the bean has already been registered :

2017-09-06 11:34:08,296 WARN  CompositeCacheManager    : Could not register JMX bean.
javax.management.InstanceAlreadyExistsException: org.apache.commons.jcs:type=JCSAdminBean
        at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
        at org.apache.commons.jcs.engine.control.CompositeCacheManager.initialize(CompositeCacheManager.java:271)
        at org.apache.commons.jcs.engine.control.CompositeCacheManager.getUnconfiguredInstance(CompositeCacheManager.java:218)

How can I get rid of these warnings ? I need caching in several webapps but I do not necessarily need this administration bean.

1

There are 1 best solutions below

3
On

I ran into this as well, and I could find nothing on google. So, after some hacking through it, I found that JCS.getInstance() must be called only once per cache. i.e. it needs to be setup in a static variable.

Following Joshua Bloch's best practises for singletons, and to ensure that the cache is accessible from multiple classes, we use an enum for that purpose. JCS.getInstance will only be called once throughout the life of the class.

public enum Cache
{
    INSTANCE;

    CacheAccess<String, Map<String, STVLang>> stvLangCache;

    Cache()
    {
        stvLangCache = JCS.getInstance("stvlang");
    }

    public CacheAccess<String, Map<String, STVLang>> getLangCache()
    {
        return stvLangCache;
    }
}