Jan Krakora
Jan Krakora

Reputation: 2610

Raw ehcache 3 statistics with Spring Boot

I'm trying to use ehcache 3 as a JSR-107 JCache provider within a Spring Boot application. I know I can enable registration of MBean statistics, but I would like to get the raw ehcache org.ehcache.core.statistics.CacheStatistics where are tier statistics etc.

Here is the CacheManager bean definition

@Configuration
public class CacheConfiguration {

    @Bean
    public JCacheManagerFactoryBean jCacheManager() throws IOException {
        JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
        jCacheManagerFactoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());

        return jCacheManagerFactoryBean;
    }
}

and ehcache.xml file

<?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 https://www.ehcache.org/schema/ehcache-core.xsd
        http://www.ehcache.org/v3/jsr107 https://www.ehcache.org/schema/ehcache-107-ext.xsd">

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

    <cache alias="test">
        <heap unit="MB">20</heap>
    </cache>
</config>

I found an example where is CacheManager configured with a Service https://github.com/ehcache/ehcache3/issues/2951#issuecomment-931364744

StatisticsRetrieval statsRetrievalService = new StatisticsRetrieval();

CacheManager cacheManager = newCacheManagerBuilder()
  .using(statsRetrievalService)
  .build(true)

StatisticsService statisticsService = statsRetrievalService.getStatisticsService();
...

StatisticsRetrieval class

@ServiceDependencies(StatisticsService.class)
public class StatisticsRetrieval implements Service {

    private StatisticsService statisticsService;

    @Override
    public void start(ServiceProvider<Service> serviceProvider) {
      this.statisticsService = serviceProvider.getService(StatisticsService.class);
    }

    @Override
    public void stop() {
      this.statisticsService = null;
    }

    public StatisticsService getStatisticsService() {
      return statisticsService;
    }
}

But I don't know how to use such a service when creating a CacheManager using the JSR-107 API.

Upvotes: 0

Views: 69

Answers (0)

Related Questions