Springboot和Ehcache - 多重缓存异常

3
我正在尝试为一个Springboot应用程序添加缓存,但在启动过程中遇到了一个org.ehcache.jsr107.MultiCacheException异常。
我使用以下依赖(都通过Maven pom文件加载): Springboot 1.5.5, Ehcache 3.3.1, Javax cache 1.0.0。
我的SpringBootApplication代码如下:
@SpringBootApplication
@EnableCaching
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

我有一个CacheConfig类,其中包含以下内容:

@Component
public class CacheConfig implements JCacheManagerCustomizer{
    @Override
    public void customize(javax.cache.CacheManager cacheManager) {
        cacheManager.createCache("item", new MutableConfiguration<>()
                .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 5)))
                .setStoreByValue(false)
                .setStatisticsEnabled(true));
    }
}

我的ehcache.xml文件包含以下内容:

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>

    <service>
        <jsr107:defaults>
            <jsr107:cache name="item" template="heap-cache"/>
        </jsr107:defaults>
    </service>

    <cache-template name="heap-cache">
        <listeners>
            <listener>
                <class>org.terracotta.ehcache.EventLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>UPDATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
                <events-to-fire-on>REMOVED</events-to-fire-on>
                <events-to-fire-on>EVICTED</events-to-fire-on>
            </listener>
        </listeners>
        <resources>
            <heap unit="entries">2000</heap>
            <offheap unit="MB">100</offheap>
        </resources>
    </cache-template>
</config>

测试项服务包括:
@Service
public class ItemService {

    @CacheResult(cacheName = "item")
    public String getItem(int itemNumber) {
        switch (itemNumber) {
            case 1:
                return "Item 1";
            case 2:
                return "Item 2";
            default:
                return "No Item";

        }
    }
}

最后,application.properties 包含:

spring.cache.jcache.config=classpath:ehcache.xml

当我运行应用程序时,出现以下异常并以此结束:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jCacheCacheManager' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] org.terracotta.ehcache.EventLogger
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 70 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jCacheCacheManager' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] org.terracotta.ehcache.EventLogger
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 83 common frames omitted
Caused by: org.ehcache.jsr107.MultiCacheException: [Exception 0] org.terracotta.ehcache.EventLogger
    at org.ehcache.jsr107.ConfigurationMerger.mergeConfigurations(ConfigurationMerger.java:138) ~[ehcache-3.3.1.jar:3.3.1 01f4b2121ef38b7e7d95c952c773881d5b1051d8]
    at org.ehcache.jsr107.Eh107CacheManager.createCache(Eh107CacheManager.java:190) ~[ehcache-3.3.1.jar:3.3.1 01f4b2121ef38b7e7d95c952c773881d5b1051d8]
    at com.example.cacheexample.CacheConfig.customize(CacheConfig.java:18) ~[classes/:na]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.customize(JCacheCacheConfiguration.java:149) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.jCacheCacheManager(JCacheCacheConfiguration.java:104) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e218d7d3.CGLIB$jCacheCacheManager$1(<generated>) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e218d7d3$$FastClassBySpringCGLIB$$b2f1636b.invoke(<generated>) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e218d7d3.jCacheCacheManager(<generated>) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 84 common frames omitted

我不确定该怎么做,因为我基本上复制了这个网站上的内容:http://www.ehcache.org/blog/2016/05/18/ehcache3_jsr107_spring.html

如果有任何见解,将非常感激。

谢谢。


好的,我已经添加了测试服务的外观。 - Brad
看一下源代码——尝试在 https://github.com/ehcache/ehcache3/blob/master/107/src/main/java/org/ehcache/jsr107/Eh107CacheManager.java 中的 createCache 方法中设置断点,以弄清发生了什么。 - Mateusz Chrzaszcz
1
也可以将此问题提交到 https://github.com/ehcache/ehcache3/issues 作为一个问题,因为异常信息真的没有什么帮助。 - Louis Jacomet
@LouisJacomet 不仅没有帮助,而且实际上是误导性的。 - Mateusz Chrzaszcz
它有什么误导性?它列出了有问题的类型,但是遗漏了异常类型,这会防止我们知道哪些类型有特殊之处。 - Louis Jacomet
显示剩余4条评论
1个回答

7
原来在 ehcache.xml 文件中定义的 org.terracotta.ehcache.EventLogger 类只是测试程序的示例记录器。包名让我误以为它是 ehcache 发行版的一部分。
当我逐步执行代码时,我发现会生成 NoClassDefFoundError 异常,并导致 MultiCacheException 被抛出。栈跟踪中没有明确表明 NoClassDefFoundError 是真正的罪魁祸首。我创建了自己的 EventLogger,现在一切都按预期工作。
感谢您的指引!

可以在此处找到缺失类的示例实现:https://github.com/gibsong/ehcache-jsr107-spring/blob/master/src/main/java/org/terracotta/ehcache/EventLogger.java - Luc De pauw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接