Search code examples
springehcachejmx

Configure JMX with Spring implementation of ehcache?


I'm trying to follow the articles here & here to implement JMX for ehCache. My app uses the Spring implementation however & the config entries below get this exception:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'managementService' defined in class path resource [trs.application.finance.businessactivites.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [net.sf.ehcache.CacheManager]: Could not convert constructor argument value of type [org.springframework.cache.ehcache.EhCacheCacheManager] to required type [net.sf.ehcache.CacheManager]: Failed to convert value of type 'org.springframework.cache.ehcache.EhCacheCacheManager' to required type 'net.sf.ehcache.CacheManager'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.cache.ehcache.EhCacheCacheManager] to required type [net.sf.ehcache.CacheManager]: no matching editors or conversion strategy found

Does spring have an implementation of net.sf.ehcache.management.ManagementService I can use? If so how do I configure it so I can have JMX support for ehcache in my app.

Note, the "managementService" & "mbeanServer" beans are what I added to try & get ehcache to register with jmx. "myCacheManager" already exists in my spring config & seems to work fine, as far ehcache alone goes.

 <bean id="managementService" class="net.sf.ehcache.management.ManagementService"
      init-method="init"
      destroy-method="dispose">
    <constructor-arg ref="myCacheManager"/>
    <constructor-arg ref="mbeanServer"/>
    <constructor-arg index="2" value="true"/>
    <constructor-arg index="3" value="true"/>
    <constructor-arg index="4" value="true"/>
    <constructor-arg index="5" value="true"/>
</bean>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
      <property name="locateExistingServerIfPossible" value="true"/>
</bean>
<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:ehcache.xml"/>
        </bean>
    </property>
</bean>

Solution

  • The problem is the way you wire your beans.

    You do not want to use myCacheManager in the managementService, you really want to use the bean returned and passed as the cacheManager of myCacheManager.

    So I suggest moving the following:

    <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    

    to a top level bean so that it can be injected in managementService.