Enabling JMX Support in ehcache 3.1.2

2.9k views Asked by At

I am using camel 2.18.1 and the camel-ehcache component to build a simple cache. While the cache setup is working okay, I am finding it difficult to register mbeans using ehcache 3.1.2 ( this is pulled in via camel).

Reading the documentations - it is not clear how one would enable support with 3.x as the standard way of registering mbeans using ManagementService is no longer available on the API.

The documentation is a bit confusing with pure ehcache implementations and JSR-107 cache implementations.

Though the JSR-107 JCache implementation have options to turn on JMX support, wiring the xml configuration and starting the cache looks to be throwing an exception on the cache startup :

Caused by: java.lang.IllegalArgumentException: Couldn't resolve Service org.ehcache.jsr107.config.Jsr107Service

My xml configuration for reference is below : Any pointers on how one would enable JMX support for ehcache 3.x and what additional dependencies would be required ?

<ehcache:config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns:ehcache="http://www.ehcache.org/v3"
        xmlns:jcache="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

    <ehcache:service>
        <jcache:defaults jsr-107-compliant-atomics="true" enable-management="true" enable-statistics="true">
            <jcache:cache name="my-cache" template="myDefaultTemplate"/>

        </jcache:defaults>

    </ehcache:service>

    <ehcache:persistence directory="/var/cache"/>

    <ehcache:cache alias="cache-test">


        <!--
      OPTIONAL, defaults to no expiry
        Entries to the Cache can be made to expire after a given time
    -->
        <ehcache:expiry>
            <!--
              time to idle, the maximum time for an entry to remain untouched
                Entries to the Cache can be made to expire after a given time
                other options are:
                   * <ttl>, time to live;
                   * <class>, for a custom Expiry implementation; or
                   * <none>, for no expiry
            -->
            <ehcache:tti unit="minutes">2</ehcache:tti>
        </ehcache:expiry>

        <!--
            The maximal number of entries to be held in the Cache, prior to eviction starting
        -->
        <ehcache:heap unit="entries">200</ehcache:heap>


        <!--
           OPTIONAL
            Any further elements in another namespace
        -->
            <jcache:mbeans enable-statistics="true" enable-management="true" />
    </ehcache:cache>

    <!--
      OPTIONAL
        A <cache-template> defines a named template that can be used be <cache> definitions in this same file
        They have all the same property as the <cache> elements above
    -->
    <ehcache:cache-template name="myDefaultTemplate">
        <ehcache:expiry>
            <ehcache:none/>
        </ehcache:expiry>
        <!--
           OPTIONAL
            Any further elements in another namespace
        -->

    </ehcache:cache-template>


</ehcache:config>

1

There are 1 answers

2
Henri On BEST ANSWER

Most likely it means that your CacheManager isn't registered using JSR-107. If I do so, it works perfectly. You can try by doing

public static void main(String[] args) throws Exception {
    ClassLoader classLoader = CheckJmx.class.getClassLoader();
    URI uri = classLoader.getResource("ehcache.xml").toURI();
    CachingProvider cachingProvider = Caching.getCachingProvider();
    try(CacheManager cm = ((CachingProvider) cachingProvider).getCacheManager(uri, classLoader)) {
        Thread.sleep(60_000);
    }
}

However, when you are not registered through JSR-107, the Jsr107Service isn't available. But adding this service won't help you anyway. The JMX MBeans are only available when registered through JSR-107.

So you best bet is to change the CacheManager creation code to use something similar as above.