Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot2.x and ehcache3.5 are error based on annotations

//jsr107
  @Configuration
  @EnableCaching
  public class EhCacheConfig extends CachingConfigurerSupport {
    @Bean
    @Override
    public CacheManager cacheManager() {
        CacheConfiguration<String, String> cacheConfiguration = 
                CacheConfigurationBuilder
                .newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10000L))
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofDays(2L)))
                    .withDispatcherConcurrency(4)
                .build();
        Map<String, org.ehcache.config.CacheConfiguration<?, ?>> cacheMap = new HashMap<>();
        cacheMap.put("vsk", cacheConfiguration);
        EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider();
         DefaultConfiguration defaultConfiguration = 
                 new DefaultConfiguration(cacheMap, ehcacheCachingProvider.getDefaultClassLoader());
         javax.cache.CacheManager cacheManager = 
                 ehcacheCachingProvider.getCacheManager(ehcacheCachingProvider.getDefaultURI(), defaultConfiguration);
//This is a mistake here.
        return new JCacheCacheManager(cacheManager);
    }
  //SimpleCacheResolver
    @Bean
    @Override
    public CacheResolver cacheResolver() {
        return new SimpleCacheResolver();
    }
  //SimpleKeyGenerator
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }
  //SimpleCacheErrorHandler
    @Bean
    @Override
    public CacheErrorHandler errorHandler() {
        return new SimpleCacheErrorHandler();
    }
  }

Below is an error message.:

       **Caused by: javax.cache.CacheException: Multiple CachingProviders have been configured when only a single CachingProvider is expected
        at javax.cache.Caching$CachingProviderRegistry.getCachingProvider(Caching.java:386)**
How do I deal with it?I have tried many changes or not.
like image 492
chunfytseng Avatar asked Apr 16 '26 02:04

chunfytseng


2 Answers

The answer of @clevertension is correct. You have more than one caching provider in your classpath.

However, in general this is not normal. So you should clean your classpath to remove everything you don't want.

Then, Caching is registering all providers. It is normal so it's not really pretty to remove the others. You can retrieve the one you need with EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider(EhcacheCachingProvider.class.getName());

Finally, you probably do not need all this code. The following code should do the exact same thing since Spring will look for a registered CacheManager to use when enabling caching.

@Configuration
@EnableCaching
public class EhCacheConfig {

  @Bean
  public CacheManager cacheManager() {
    CacheConfiguration<String, String> cacheConfiguration =
        CacheConfigurationBuilder
            .newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.heap(10000L))
            .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofDays(2L)))
            .withDispatcherConcurrency(4)
            .build();
    Map<String, CacheConfiguration<?, ?>> cacheMap = new HashMap<>();
    cacheMap.put("vsk", cacheConfiguration);
    EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) Caching.getCachingProvider(EhcacheCachingProvider.class.getName());
    DefaultConfiguration defaultConfiguration =
        new DefaultConfiguration(cacheMap, ehcacheCachingProvider.getDefaultClassLoader());
    javax.cache.CacheManager cacheManager =
        ehcacheCachingProvider.getCacheManager(ehcacheCachingProvider.getDefaultURI(), defaultConfiguration);
    return new JCacheCacheManager(cacheManager);
  }

}
like image 111
Henri Avatar answered Apr 17 '26 17:04

Henri


the exception message is clear that you have multiple cache providers, so if you want to know all the cache provider, you can add the following code snippet in your spring-boot main function

public static void main(String[] args) {
        //list all the caching provider
        Iterator<CachingProvider> iterator = Caching.getCachingProviders(Caching.getDefaultClassLoader()).iterator();
        while(iterator.hasNext()) {
            CachingProvider provider = iterator.next();
            if (!(provider instanceof EhcacheCachingProvider)) {
                iterator.remove();
            }
        }

        SpringApplication.run(Run.class, args);
    }
like image 37
clevertension Avatar answered Apr 17 '26 16:04

clevertension



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!