Hazelcast 中的 Jcache 集成不起作用

Jcache integration in hazelcast does not work

我目前正在尝试将 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的classjavax.cache.Cache

我正在使用这个示例,但我没有像这样检索 xml 配置:

http://docs.hazelcast.org/docs/3.9.3/manual/html-single/index.html#jcache-declarative-configurationhttp://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 实例相关联??。管理器对象不检索缓存 Id = "EXPIR00001"

我需要从 xml 文件以声明方式 (Hazelcast.xml) 进行配置。有很多配置,我们可能需要。

我无法使用: http://docs.hazelcast.org/docs/3.9.3/manual/html-single/index.html#hazelcast-jcache

这是第一个示例代码 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 集成工作原理的解释:

First of all, we retrieve the javax.cache.spi.CachingProvider using the static method from javax.cache.Caching:: getCachingManager, which automatically picks up Hazelcast as the underlying JCache implementation, if available in the classpath. This way, the Hazelcast implementation of a CachingProvider will automatically start a new Hazelcast member or client (depending on the chosen provider type) and pick up the configuration from either the command line parameter or from the classpath. We will show how to use an existing HazelcastInstance later in this chapter; for now, we keep it simple.

我建议您阅读文档,您会找到很多问题的答案。

我发现了错误,我的意图是将管理器作为静态变量留在 class。

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