-1

我目前正在尝试将 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-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 文件进行配置,decalratively (Hazelcast.xml)。有很多配置,我们可能需要。

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

4

2 回答 2

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.Caching::getCachingManager 的静态方法检索 javax.cache.spi.CachingProvider,如果在类路径中可用,它会自动选择 Hazelcast 作为底层 JCache 实现。这样,CachingProvider 的 Hazelcast 实现将自动启动一个新的 Hazelcast 成员或客户端(取决于所选的提供程序类型)并从命令行参数或类路径中获取配置。我们将在本章后面展示如何使用现有的 HazelcastInstance;现在,我们保持简单。

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

于 2018-03-12T03:52:13.150 回答
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

于 2018-03-12T13:21:07.017 回答