使用带有hazelcast的jcache时获取IllegalArgumentException

时间:2017-10-24 07:57:50

标签: java caching spring-boot hazelcast jcache

我正在尝试将jcache与hazelcast服务器提供程序一起使用。但得到这个例外。

    java.lang.IllegalArgumentException: Cannot find cache named 'xyzCache' for Builder throws caches=[xyzCache] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
    at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheR esolver.java:81)
    at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:242)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:675)
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:255)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:581)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)

以下是我用来配置hazelcast的java配置。

HazelcastConfiguration.java

    @EnableCaching
    class HazelcastConfiguration {

    @Bean
    public Config getConfig() throws FileNotFoundException {
    Config config;

    if ((xmlConfigLocation == null) || (xmlConfigLocation.isEmpty())) {
      // use default Hazelcast configuration
      config = new Config();
    } else {
      // overlay custom xml config on default Hazelcast configuration.
      config = new FileSystemXmlConfig(xmlConfigLocation);
    }
    //Trying to create cache config 
    MapConfig cache = new MapConfig();

    cache.setName("xyzCache");
    cache.getMaxSizeConfig().setSize(1);
    cache.setMaxIdleSeconds(0);
    cache.setTimeToLiveSeconds(86400);
    cache.setEvictionPolicy(EvictionPolicy.LRU);
    config.addMapConfig(cache);
    }
    }

使用的依赖关系:

    <dependency>
          <groupId>com.hazelcast</groupId>  
          <artifactId>hazelcast-spring</artifactId>  
          <version>3.6.8</version>  
       </dependency>  

    <dependency>
          <groupId>com.hazelcast</groupId>  
          <artifactId>hazelcast-cloud</artifactId>  
          <version>3.6.8</version>
       </dependency>

Spring Boot版本:1.4.6

使用这些配置,我可以在应用程序中创建和使用hazelcast缓存。将以下依赖项添加到提供程序jcache缓存提供程序后。 Spring尝试从其自动配置及其缓存管理器中使用JCacheCacheConfiguration。

    <dependency>
          <groupId>javax.cache</groupId>
          <artifactId>cache-api</artifactId>
          <version>1.0.0</version>
       </dependency>

Spring Boot启动应用程序时没有任何异常或错误。但是一旦我尝试运行第一个api调用,就会开始让我超越异常。有什么建议。?

3 个答案:

答案 0 :(得分:1)

您似乎通过MapConfig配置缓存。 尝试使用在配置部分中从config.getCacheConfig("xyzCache");方法返回的配置对象,然后让我们看看它是否解决了问题。

答案 1 :(得分:0)

配置上设置缓存实例的方法是 config.setInstanceName(&#34; xyzConfig&#34;) 强>

所以完整的代码应该是:

@EnableCaching
class HazelcastConfiguration {

@Bean
public Config getConfig() throws FileNotFoundException {
    Config config;

    if ((xmlConfigLocation == null) || (xmlConfigLocation.isEmpty())) {
      // use default Hazelcast configuration
      config = new Config();
    } else {
      // overlay custom xml config on default Hazelcast configuration.
      config = new FileSystemXmlConfig(xmlConfigLocation);
    }

    config.setInstanceName("xyzConfig");

    //Trying to create cache config 
    MapConfig cache = new MapConfig();
    cache.getMaxSizeConfig().setSize(1);
    cache.setTimeToLiveSeconds(86400);
    cache.setEvictionPolicy(EvictionPolicy.LFU);

    // This were you put cache key and value
    config.getMapConfigs().put("xyzCache",cache);
}

答案 2 :(得分:0)

javax.cache::cache-api工件不在您的类路径中时,Spring Boot会使用Hazelcast IMap来回复缓存,以便您选择MapConfig并配置IMap保存Cacheable方法的缓存结果。

一旦JCache API位于类路径中,它就会尝试将Hazelcast用作JCache实现(参见[1])。在这种情况下,Spring的JCacheCacheManager尝试获取JCache提供程序已知的Cache,因此您需要为@Cacheable注释中声明的缓存名称配置Hazelcast (有关Hazelcast JCache配置,请参阅[2])。例如,对于您最初发布的编程配置,当javax.cache::cache-api位于类路径中时,您需要按如下方式配置Hazelcast:

@Bean
public Config getConfig() {
    Config config = new Config();
    // do your file stuff here
    CacheSimpleConfig cacheConfig = new CacheSimpleConfig();
    cacheConfig.setName("xyzCache");
    // set other options here
    config.addCacheConfig(cacheConfig);

    // alternatively to creating CacheSimpleConfig and adding it:
    // config.getCacheConfig("xyzCache").setBackupCount(1).set...;
    return config;
}

[1] https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-caching-provider-jcache

[2] http://docs.hazelcast.org/docs/3.9/manual/html-single/index.html#configuring-for-jcache