Java 将jcache与hazelcast一起使用时获取IllegalArgumentException

Java 将jcache与hazelcast一起使用时获取IllegalArgumentException,java,caching,spring-boot,hazelcast,jcache,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='' | sy

我正在尝试将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)
使用的依赖项:

    <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>

com.hazelcast
榛铸弹簧
3.6.8  
com.hazelcast
黑云
3.6.8
Spring启动版本:1.4.6

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

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

javax.cache
缓存api
1.0.0

Spring Boot在没有任何异常或错误的情况下启动应用程序。但当我尝试运行第一个api调用时,它就开始抛出异常。有什么建议吗?

设置缓存实例的方法是config.setInstanceName(“xyzConfig”)onconfig

因此,完整的代码应该如下所示:

@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);
}

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

javax.cache::cache api
工件不在类路径中时,Spring Boot会使用Hazelcast
IMap
来备份缓存,因此您的
MapConfig
会被拾取并配置一个
IMap
来保存
Cacheable
方法的缓存结果

一旦JCacheAPI位于类路径中,它就会尝试使用Hazelcast作为JCache实现(参见[1])。在本例中,Spring的
JCacheCacheCacheManager
尝试获取JCache提供程序已知的
缓存
,因此需要为您在
@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]


[2]

这是否意味着当设置任何cacheconfig时,jcache与hazelcast一起工作?如果我们必须使用MapConfig,那么它将无法工作。Map和Cache在Hazelcast中是不同的数据结构。如果您配置MapConfig并使用相同的名称调用缓存,您将得到null。是的,我在配置MapConfig时得到null。在用hazelcast配置jcache时,有没有办法坚持使用MapConfig?没有,它是像IQueue一样的独立数据结构。但为什么在使用ICache时需要进行IMap配置?MapConfig是否提供了任何要求,但没有其他要求?
@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;
}