【发布时间】:2019-02-25 16:42:42
【问题描述】:
我们使用JCache 并使用以下缓存配置为JCache 规范提供后端/CacheManager。 Hazelcast 由 spring 自动配置,因此我们不需要显式提供CacheManager,而只需提供我们的 hazlecast 配置。
@Configuration
public class CacheConfig {
public static final int TTL_INFINITE = 0;
@Bean
public Config hazelCastConfig() {
// do not allow hazelcast to send data to hazelcast, see
// http://docs.hazelcast.org/docs/latest-development/manual/html/Preface/Phone_Home.html
GroupProperty.PHONE_HOME_ENABLED.setSystemProperty("false");
return new Config()
.setInstanceName("hazelcast-instance")
// create a cache
.addCacheConfig(new CacheSimpleConfig()
.setName(RateLimiterServiceImpl.CACHE_NAME))
// store it distributed
.addMapConfig(new MapConfig()
.setName(RateLimiterServiceImpl.CACHE_NAME)
.setTimeToLiveSeconds(RateLimiterServiceImpl.CACHE_SECONDS_TO_LIVE)
.setEvictionPolicy(EvictionPolicy.LFU))
// create a cache
.addCacheConfig(new CacheSimpleConfig()
.setName(I18nServiceImpl.CACHE_NAME))
// store it distributed
.addMapConfig(new MapConfig()
.setName(I18nServiceImpl.CACHE_NAME)
.setTimeToLiveSeconds(I18nServiceImpl.CACHE_SECONDS_TO_LIVE)
.setEvictionPolicy(EvictionPolicy.LRU));
}
}
在生产和本地运行测试时,一切都很好。但是使用 gitlab CI 在集成测试过程中会出现以下错误:
java.lang.IllegalStateException: null
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.checkIfManagerNotClosed(AbstractHazelcastCacheManager.java:374) ~[hazelcast-3.9.2.jar:3.9.2]
和 hazelcast 3.10.5
java.lang.IllegalStateException: CacheManager /hz/ is already closed.
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.ensureOpen(AbstractHazelcastCacheManager.java:366) ~[hazelcast-3.10.5.jar:3.10.5]
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:219) ~[hazelcast-3.10.5.jar:3.10.5]
at com.hazelcast.cache.impl.AbstractHazelcastCacheManager.getCache(AbstractHazelcastCacheManager.java:67) ~[hazelcast-3.10.5.jar:3.10.5]
at org.springframework.cache.jcache.JCacheCacheManager.getMissingCache(JCacheCacheManager.java:114) ~[spring-context-support-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.cache.support.AbstractCacheManager.getCache(AbstractCacheManager.java:97) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
这是测试失败:
mockMvc.perform(put("/translations/{locale}", locale)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(dto)
.andExpect(status().isNoContent());
// gives a 500 with the above error message
我们应该如何配置集成测试以使用 hazelcast?
【问题讨论】:
-
最新的 Hazelcast 版本 (3.10.5) 是否有同样的问题?
-
@Rafal Leszko:是的,3.10.5 产生同样的错误
-
对不起,再次@RafałLeszko:现在的错误是:
java.lang.IllegalStateException: CacheManager /hz/ is already closed.我更新了问题。 -
Dzmitry 的回复有帮助吗?如果没有,请告诉我,我会尝试深入研究。
-
部分确实有帮助(见评论)。欢迎进一步的想法! :)
标签: spring-boot hazelcast