我目前正在尝试将HazelCast缓存与jcache对象集成,以我的开发为基础。
我需要集成大量不同的配置,为此我创建了一个hazelcast.xml。在我尝试使用hazelcast地图对象(com.hazelcast.core.IMap)时,我得到了它,所以我可以使用适当的配置获取缓存:
private static void initHazelcast() {
log.info("initHazelcast()");
Config cfg = null;
try {
cfg = new XmlConfigBuilder("./src/main/resources/hazelcast.xml").build();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
hazelcastInstance = Hazelcast.newHazelcastInstance(cfg);
IMap map = hazelcastInstance.getMap("EXPIR00001");
log.info("initHazelcast() End");
}
HazelCast.xml:
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config-3.9.xsd"
xmlns="http://www.hazelcast.com/schema/config">
<map name="EXPIR00001">
<time-to-live-seconds>1</time-to-live-seconds>
<max-idle-seconds>1</max-idle-seconds>
<eviction-policy>LRU</eviction-policy>
<max-size policy="PER_NODE">5000</max-size>
</map>
</hazelcast>
现在我尝试使用JCache的类javax.cache.Cache。
我正在使用此示例,但我没有像这样检索xml配置:
http://docs.hazelcast.org/docs/3.9.3/manual/html-single/index.html#jcache-declarative-configuration 和 http://docs.hazelcast.org/docs/3.9.3/manual/html-single/index.html#scoping-to-join-clusters
Hazelcast.xml:
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config-3.9.xsd"
xmlns="http://www.hazelcast.com/schema/config">
<cache name="EXPIR00001">
<backup-count>1</backup-count>
<async-backup-count>1</async-backup-count>
<in-memory-format>BINARY</in-memory-format>
<eviction size="10000" max-size-policy="ENTRY_COUNT" eviction-policy="LRU" />
<expiry-policy-factory>
<timed-expiry-policy-factory expiry-policy-type="CREATED"
duration-amount="1"
time-unit="DAYS"/>
</expiry-policy-factory>
</cache>
</hazelcast>
方法java:
private static void initHazelcast() {
log.info("initHazelcast()");
Config cfg = null;
try {
cfg = new XmlConfigBuilder("./src/main/resources/hazelcast.xml").build();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
hazelcastInstance = Hazelcast.newHazelcastInstance(cfg);
CacheManager manager = Caching.getCachingProvider().getCacheManager();
//In JCache
Cache<byte[], byte[]> cache = manager.getCache( "EXPIR00001" );
log.info("initHazelcast() End");
}
CacheManager管理器如何与hazelcast intance相关??管理器对象不检索缓存Id =“EXPIR00001”
我需要从xml文件配置,decalratively(Hazelcast.xml)。我们可能需要许多配置。
我无法使用: http://docs.hazelcast.org/docs/3.9.3/manual/html-single/index.html#hazelcast-jcache
答案 0 :(得分:0)
以下是http://docs.hazelcast.org/docs/3.9.3/manual/html-single/index.html#hazelcast-jcache
的第一个示例代码// Retrieve the CachingProvider which is automatically backed by
// the chosen Hazelcast member or client provider
CachingProvider cachingProvider = Caching.getCachingProvider();
// Create a CacheManager
CacheManager cacheManager = cachingProvider.getCacheManager();
// Create a simple but typesafe configuration for the cache
CompleteConfiguration<String, String> config =
new MutableConfiguration<String, String>()
.setTypes( String.class, String.class );
// Create and get the cache
Cache<String, String> cache = cacheManager.createCache( "example", config );
// Alternatively to request an already existing cache
// Cache<String, String> cache = cacheManager
// .getCache( name, String.class, String.class );
// Put a value into the cache
cache.put( "world", "Hello World" );
// Retrieve the value again from the cache
String value = cache.get( "world" );
// Print the value 'Hello World'
System.out.println( value );
关于JCache的Hazelcast集成如何工作的解释:
首先,我们使用检索javax.cache.spi.CachingProvider 来自javax.cache.Caching :: getCachingManager的静态方法 自动选择Hazelcast作为底层JCache 实现,如果在类路径中可用。这样,Hazelcast CachingProvider的实现会自动启动一个新的 Hazelcast会员或客户(取决于所选的提供商类型)和 从命令行参数或中获取配置 从类路径。我们将展示如何使用现有的 本章后面的HazelcastInstance;现在,我们保持简单。
我建议你阅读文档,你会找到很多问题的答案。
答案 1 :(得分:0)
我发现了错误,我的目的是将经理作为一个静态变量留在课堂上。
CacheManager manager = Caching.getCachingProvider().getCacheManager();
将此行替换为:
CachingProvider caching = Caching.getCachingProvider();
CacheManager cacheManager = caching.getCacheManager();
完整示例: http://docs.hazelcast.org/docs/3.9.3/manual/html-single/index.html#scoping-to-join-clusters