Search code examples
spring-bootehcachespring-boot-actuatorspring-boot-adminehcache-3

Ehcache 3 and Spring Boot Admin statistics


I'm trying to migrate a Spring Boot project currently using Ehcache 2 to latest Ehcache 3.7.

Everything seems fine except the missing Spring Boot Admin cache statistics.

Here is the previous Ehcache 2 configuration:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="ehcache.xsd"
     updateCheck="false"
     monitoring="on"
     dynamicConfig="true"
     statistics="true">

<cache name="asset"
       maxEntriesLocalHeap="5"
       timeToIdleSeconds="600"
       timeToLiveSeconds="3600"
       memoryStoreEvictionPolicy="LRU"/>

</ehcache>

And the new Ehcache 3 configuration:

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

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

<cache alias="asset">
    <resources>
        <heap unit="entries">5</heap>
    </resources>
    <expiry>
        <ttl unit="hours">1</ttl>
    </expiry>
    <jsr107:mbeans enable-management="true" enable-statistics="true"/>
</cache>

</config>

POM dependencies (only those related to cache management):

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jcache</artifactId>
        <version>5.4.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.7.0</version>
    </dependency>

Spring configuration:

spring:
  cache:
    ehcache:
      config: classpath:ehcache.xml
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect
        hbm2ddl:
          auto: none
        cache:
          use_second_level_cache: true
          region:
            factory_class: jcache
        javax:
          cache:
            provider: org.ehcache.jsr107.EhcacheCachingProvider
            missing_cache_strategy: fail

I used to obtain this kind of statistics with Ehcache 2:

enter image description here

But with Ehcache 3, no stats are displayed on SBA Insights/Details page nor on Data/Caches one.

With Ehcache 2 it was pretty plug'n play but it does not seem to be the case with Ehcache 3.

Anybody has a hint?

Thanks!


Solution

  • Solved! Silly mistake...

    In spring configuration, instead of:

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

    I had to use this with Ehcache 3:

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

    And define a bean:

    @Bean
    public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer(
            JCacheCacheManager cacheManager) {
        return (properties) -> properties.put(ConfigSettings.CACHE_MANAGER,
                cacheManager.getCacheManager());
    
    }
    

    As described here: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-hibernate-second-level-caching

    And now caches are well reported via actuators and JMX in SBA :)