在ehcache 3.1.2中启用JMX支持

时间:2017-01-07 11:09:43

标签: caching ehcache jmx jsr107

我正在使用camel 2.18.1和camel-ehcache组件来构建一个简单的缓存。虽然缓存设置工作正常,但我发现很难使用ehcache 3.1.2注册mbeans(这是通过camel引入的)。

阅读文档 - 目前尚不清楚如何使用3.x启用支持,因为API上不再提供使用ManagementService注册mbeans的标准方法。

对于纯ehcache实现和JSR-107缓存实现,文档有点令人困惑。

虽然JSR-107 JCache实现具有打开JMX支持的选项,但连接xml配置并启动缓存看起来会在缓存启动时抛出异常:

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

我的xml配置供参考,如下: 有关如何为ehcache 3.x启用JMX支持以及需要哪些其他依赖项的任何指针?

<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 个答案:

答案 0 :(得分:1)

很可能这意味着您的CacheManager未使用JSR-107注册。如果我这样做,它就完美无缺。您可以尝试

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);
    }
}

但是,当您未通过JSR-107注册时,Jsr107Service不可用。但无论如何,添加此服务对您无济于事。 JMX MBean仅在通过JSR-107注册时可用。

因此,您最好将更改CacheManager创建代码更改为使用与上述类似的内容。