最简单的ehcache复制缓存无法正常工作

时间:2013-09-10 02:21:19

标签: caching ehcache

您好抱歉发布了一个与其他ehcache复制问题类似的问题,但我在这一天的大部分时间里一直在喋喋不休,而且我已经阅读了很多没有解决方案的stackoverflow帖子。

我试图设置最简单的ehcache复制测试,但它无法正常工作。 EhcacheTest将一个元素写入名为" tprc"的缓存中。然后读取缓存并打印它找到的内容。 EhcacheTest2几乎相同但写了不同的元素。我期待EhcacheTest2显示两个值,即EhcacheTest和EhcacheTest2写的值。

这里的EhcacheTest:

public class EhcacheTest {

    public static void main(String[] args) {
        CacheManager manager = CacheManager.newInstance("bin/ehcache.xml");

        Cache cache = manager.getCache("tprc");
        Element element = new Element("name1", "jim");
        cache.put(element);

        for (int i = 1; i <= 2; i++) {
            String key = "name" + Integer.toString(i);
            Element got = cache.get(key);
            if (got != null) {
                System.out.println("1 " + got.getObjectKey() + "=" + got.getObjectValue());
            }
        }
    }
}

这里是EhcacheTest2:

public class EhcacheTest2 {
  public static void main(String[] args) {
    CacheManager manager = CacheManager.newInstance("bin/ehcache.xml");

    Cache cache = manager.getCache("tprc");
    Element element = new Element("name2", "erik");
    cache.put(element);

    for (int i = 1; i <= 2; i++) {
      String key = "name" + Integer.toString(i);
      Element got = cache.get(key);
      if (got != null) {
        System.out.println("2 " + got.getObjectKey() + "=" + got.getObjectValue());
      }
    }
  }
}

这里是ehcache.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true">

    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
            properties="peerDiscovery=automatic,
                        multicastGroupAddress=230.0.0.1,
                        multicastGroupPort=4446"/>

    <cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
            properties="hostName=192.168.1.115, port=40001, socketTimeoutMillis=5000"/>

    <cache name="tprc"
           maxEntriesLocalHeap="10"
           eternal="false"
           timeToIdleSeconds="100"
           timeToLiveSeconds="100">
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
                properties="replicateAsynchronously=false, replicatePuts=true,
                            replicatePutsViaCopy=true, replicateUpdates=true,
                            replicateUpdatesViaCopy=true, replicateRemovals=true"/>
    </cache>
</ehcache>

我运行EhcacheTest,让它继续运行,然后运行EhcacheTest2。 EhcacheTest的输出是:

1 name1=jim

,EhcacheTest2的输出为:

2 name2=erik

我希望EhcacheTest2的输出显示

2 name1=jim
2 name2=erik

有人知道什么是错的吗?

1 个答案:

答案 0 :(得分:2)

好的,我想通了,上面的代码有点工作,但没有等待足够的时间让缓存同步,然后打印出缓存中的内容。我在缓存检查周围添加了一个while循环,在循环几次迭代之后,每个测试程序开始显示来自另一个程序的缓存元素。这是循环的测试程序:

  public static void main(String[] args) throws InterruptedException {   
    CacheManager manager = CacheManager.newInstance("bin/ehcache.xml");

    Cache cache = manager.getCache("tprc");
    Element element = new Element("name2", "erik");
    cache.put(element);

    while (true) {
      for (int i = 1; i <= 2; i++) {
        String key = "name" + Integer.toString(i);
        Element got = cache.get(key);
        if (got != null) {
          Date now = new Date();
          long nowLong = now.getTime();
          System.out.println("2 " + got.getObjectKey() + "=" + got.getObjectValue() 
                  + " timeLeft: " + (got.getExpirationTime() - nowLong)/1000);
        }
      }
      Thread.sleep(2000);
    }
  }

解决此问题的另一种方法是将同步bootstrapCacheLoaderFactor添加到ehcache.xml中的缓存配置中:

<cache name="tprc"
       maxEntriesLocalHeap="10"
       eternal="false"
       timeToIdleSeconds="100"
       timeToLiveSeconds="100">
    <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicateAsynchronously=false, replicatePuts=true,
                        replicatePutsViaCopy=true, replicateUpdates=true,
                        replicateUpdatesViaCopy=true, replicateRemovals=true"/>
    <bootstrapCacheLoaderFactory
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
            properties="bootstrapAsynchronously=false, maximumChunkSizeBytes=5000000"/>
</cache>