我正在尝试将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调用,就会开始让我超越异常。有什么建议。?
答案 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;
}
[2] http://docs.hazelcast.org/docs/3.9/manual/html-single/index.html#configuring-for-jcache