5

Did anyone implemented EhCache 3 with Spring 4.2 (not using Spring boot). If so what are the steps to implement that?

The problem is that spring-context-support (which adds Spring's cache annotations) expects the Ehcache's CacheManager to be on this classpath: net.sf.ehcache.CacheManager

However, in Ehcache 3, the CacheManager class resides on another classpath: org.ehcache.CacheManager.

So, basically spring-context-support does not support Ehcache 3. And you would have to use the JSR-107 annotations directly, not the annotations provided by Spring.

If anyone implemented this combination, please give you're ehcache.xml and spring configuration for reference.

Mohan
  • 699
  • 1
  • 11
  • 27

2 Answers2

5

Ehcache 3 is used through JSR-107. Here is an example.

Your pom.xml:

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.2.9.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.5.0</version>
  </dependency>
  <dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
    <version>1.1.0</version>
  </dependency>

Your ehcache.xml (at the root of the classpath):

<?xml version="1.0" encoding="UTF-8"?>
<config
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
    xmlns='http://www.ehcache.org/v3'
    xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.4.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.4.xsd">

  <service>
    <jsr107:defaults enable-management="false" enable-statistics="true"/>
  </service>

  <cache alias="cache">
    <resources>
      <heap unit="entries">2000</heap>
    </resources>
  </cache>
</config>

An example app using the cache:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URISyntaxException;

import javax.cache.Caching;

@EnableCaching
@Configuration
public class App {

    private static int value = 0;

    @Bean
    public CacheManager cacheManager() throws URISyntaxException {
        return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager(
            getClass().getResource("/ehcache.xml").toURI(),
            getClass().getClassLoader()
        ));
    }

    public static void main( String[] args ) {
        ApplicationContext context = new AnnotationConfigApplicationContext(App.class);

        App app = context.getBean(App.class);
        System.out.println(app.incValue());
        System.out.println(app.incValue()); // still return 0
    }

    @Cacheable("cache")
    public int incValue() {
        return value++;
    }

}
Henri
  • 5,551
  • 1
  • 22
  • 29
  • It works fine in local, but when i deploy my changes to Websphere console, i am getting this exception during startup. Caused by: java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=org/ehcache/jsr107/EhcacheCachingProvider, offset=6. Websphere version is 8.5.5.9 and JDK version is 1.7.1.64 in it. – Mohan Feb 28 '18 at 18:00
  • I get this error. `Invalid content was found starting with element '{"http://www.ehcache.org/v3":service}'. One of '{"http://www.ehcache.org/v3":thread-pools, "http://www.ehcache.org/v3":event-dispatch, "http://www.ehcache.org/v3":write-behind, "http://www.ehcache.org/v3":heap-store, "http://www.ehcache.org/v3":disk-store, "http://www.ehcache.org/v3":cache, "http://www.ehcache.org/v3":cache-template}' is expected.` – wonsuc Feb 21 '19 at 10:00
  • @wonsuc Not really related to this error. You should create your own question. But it means your ehcache.xml is not well formatted. – Henri Feb 21 '19 at 19:17
0

I would recommend you to rely on the JSR-107 (aka JCache, the standard API for caching on the JVM) Spring support, and then add ehcache3 on the classpath.

You could also use Spring own annotations, that integrate very well with JSR 107 ones : Spring has been supporting JSR-107 for almost 4 years now : https://spring.io/blog/2014/04/14/cache-abstraction-jcache-jsr-107-annotations-support.

I invite you to visit the blog post above and the documentation it links to, your use case is very standard and very supported. Feel free to ask further questions.

Anthony Dahanne
  • 4,823
  • 2
  • 40
  • 39