我正在尝试在Infinispan中实现一个分布式系统,我想获取与本地节点关联的密钥。我试图通过利用KeyAffinityService来实现这一点,但是却收到了NullPointerException。我希望有人能帮助我解决我的错误。

程式码片段

    // Create the affinity service to find the Key for the manager
    KeyAffinityService keyAffinityService = KeyAffinityServiceFactory.newLocalKeyAffinityService(
            cache,
            (KeyGenerator)new RndKeyGenerator(),
            Executors.newSingleThreadExecutor(),
            100);


缓存的实现如下:

    EmbeddedCacheManager manager = new DefaultCacheManager();
    try{
        manager = new DefaultCacheManager("democluster.xml");
    }catch(IOException e){}
    Cache<Integer, String> cache = manager.getCache();


xml文件

<infinispan
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="urn:infinispan:config:5.1 http://www.infinispan.org/schemas/infinispan-config-5.1.xsd"
      xmlns="urn:infinispan:config:5.1">

   <global>
      <transport clusterName="demoCluster"/>
      <globalJmxStatistics enabled="true"/>
   </global>

   <namedCache name="clusteredCache">
        <clustering mode="distributed">
        <hash numOwners="1" >
           <groups enabled="true"/>
        </hash>
        </clustering>
    </namedCache>
</infinispan>


错误:

Exception in thread "main" java.lang.NullPointerException
  at org.infinispan.affinity.KeyAffinityServiceFactory.newLocalKeyAffinityService(KeyAffinityServiceFactory.java:95)
  at org.infinispan.affinity.KeyAffinityServiceFactory.newLocalKeyAffinityService(KeyAffinityServiceFactory.java:104)
  at SimpleCache.start(SimpleCache.java:46)
  at SimpleCache.main(SimpleCache.java:96)


我想知道是否有人遇到过类似问题或对这个问题可能有任何想法。

最佳答案

您使用了错误的缓存,应该这样做

Cache<Integer, String> cache = manager.getCache("clusteredCache");


KeyAffinityService仅适用于分布式缓存,并且默认缓存仅是本地缓存(因为您的配置中没有<default>元素)。

08-28 06:16