apkisbossin
apkisbossin

Reputation: 344

Convert net.sf.ehcache.CacheManger to org.springframework.cache.CacheManager?

I have an application that runs locally with a bean in Application.java for Spring Boot called cacheManager

@Bean(name="cacheManager")
 @Primary
 public CacheManager getCacheManager() {
     return new EhCacheCacheManager();
}

Since it worked locally I deployed to a server and apparently there is another application with a CacheManger that's competing for it's space because I get following stacktrace:

Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 2. Shutdown the earlier cacheManager before creating new one with same name. The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626) at net.sf.ehcache.CacheManager.init(CacheManager.java:391) at net.sf.ehcache.CacheManager.(CacheManager.java:269) at org.springframework.cache.ehcache.EhCacheManagerUtils.buildCacheManager(EhCacheManagerUtils.java:54) at org.springframework.cache.ehcache.EhCacheCacheManager.afterPropertiesSet(EhCacheCacheManager.java:74) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ... 32 common frames omitted

I attempted to put

@Bean(name="cacheManager")
@Primary
public CacheManager getCacheManager() {
    return net.sf.ehcache.CacheManager.create();
}

but then net.sf.ehcache.CacheManger.create() doesn't return a spring CacheManger. I tried changing the returning CacheManager to net.sf.ehcache.CacheManager, but I get this locally:

Caused by: java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary (or give it the name 'cacheManager') or declare a specific CacheManager to use, that serves as the default one. at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:212) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151) at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131) at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 42 more

I think converting is the answer, but the answer could also be some sly code move. Suggestions? Extra Information: This is in a webservice

Upvotes: 1

Views: 3788

Answers (2)

apkisbossin
apkisbossin

Reputation: 344

The answer to my problem was to let Spring decide the cache manager, so all I needed to do was add @EnableCaching on my Application.java and then use @Cacheable on the methods I wanted to cache on the server.

Upvotes: 1

Louis Jacomet
Louis Jacomet

Reputation: 14500

Unless you deploy an ehcache.xml configuration file for Ehcache, you get the default embedded configuration. This configuration does not name the CacheManager and as the first exception indicates, you cannot have more than one in a single JVM.

The easiest solution is to have an ehcache.xml, not in a package, and then it will be picked up by your deployment.

Upvotes: 1

Related Questions