To have 2 ehcache.xml in web and service project

236 Views Asked by At

I have a services project and web project. I need to have eh-cache in both projects.

Here some time service project work via batch as well. So we need a separate cache in web and service project.

At this point, my web project also uses eh-cache for its own purposes. I am not much experienced with eh-cache and I fear that the two projects might clash when deployed together. I also did not find relevant information on eh-cache site.

Can you provide me some information how to best configure the two projects, so that I can achieve the above requirements?

Web Project:

@Bean
public EhCacheCacheManager ehCacheCacheManager() {
    net.sf.ehcache.CacheManager cacheManager = ehCacheManagerFactoryBean().getObject();
    return new EhCacheCacheManager(cacheManager);
}

@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
    EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
    cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
    cacheManagerFactoryBean.setShared(true);
    return cacheManagerFactoryBean;
}

Service Project:

@Bean
public IServiceCacheManager cacheManager() {
    return new ServiceCacheManager();
}

public ServiceCacheManager() {
    EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
    factory.setConfigLocation(new ClassPathResource("service-ehcache.xml"));
    factory.setShared(true);
    factory.afterPropertiesSet();
    setCacheManager(factory.getObject());
}

public ServiceCacheManager(net.sf.ehcache.CacheManager cacheManager) {
    super(cacheManager);
}

Also, I have tried to given bean name as well. Like below.

@Bean(name="WebCacheManager")
    public EhCacheCacheManager ehCacheCacheManager() {
        net.sf.ehcache.CacheManager cacheManager = ehCacheManagerFactoryBean().getObject();
        return new EhCacheCacheManager(cacheManager);
    }


@Bean(name="ServiceCacheManager")
    public ServiceCacheManager cacheManager() {
        return new CBAEngineCacheManager();
    }

Above configuration also not worked.

Thanks! Bharathi

1

There are 1 best solutions below

0
On

I had to do this as we have two ehcache configuration files, one in the main service and another in an included library. I created two cache managers in different ways as Spring doesn't allow two EhCacheManagerFactoryBean in the context and the config location is set soon after bean creation, so it cannot be updated.

One cache manager was created with the EhCacheManagerFactoryBean approach as shown in the above post, with a bean qualifier. And the other using Cachemanager.create

Here is a code snippet showing the above two methods of creation:

@Bean("ehCacheManager1")
fun EhCacheCacheManager ehCacheCacheManager1(ehCacheManagerFactoryBean: CacheManager): EhCacheCacheManager =
    EhCacheCacheManager(ehCacheManagerFactoryBean)

@Bean("ehCacheManager2")
@Primary
fun ehCacheManager2(): EhCacheCacheManager {
  val configLocation = cacheProperties().resolveConfigLocation(ClassPathResource("ehcache.xml"))
  return EhCacheCacheManager(CacheManager.create(configLocation.url))
}

@Bean
fun cacheProperties() = CacheProperties()