Caching 最简单的ehcache复制缓存不工作

Caching 最简单的ehcache复制缓存不工作,caching,ehcache,Caching,Ehcache,您好,很抱歉发布了一个与其他ehcache复制问题如此相似的问题,但我一天中的大部分时间都在思考这个问题,我读了很多没有解决方案的stackoverflow帖子 我试图设置最简单的ehcache复制测试,但它不起作用。EhcacheTest将一个元素写入名为“tprc”的缓存,然后读取缓存并打印找到的内容。EhcacheTest2几乎相同,但写入了不同的元素。我希望EhcacheTest2同时显示两个值,一个是由EhcacheTest和EhcacheTest2编写的 以下是测试: public

您好,很抱歉发布了一个与其他ehcache复制问题如此相似的问题,但我一天中的大部分时间都在思考这个问题,我读了很多没有解决方案的stackoverflow帖子

我试图设置最简单的ehcache复制测试,但它不起作用。EhcacheTest将一个元素写入名为“tprc”的缓存,然后读取缓存并打印找到的内容。EhcacheTest2几乎相同,但写入了不同的元素。我希望EhcacheTest2同时显示两个值,一个是由EhcacheTest和EhcacheTest2编写的

以下是测试:

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的输出为:

2 name2=erik
我希望显示EhcacheTest2的输出

2 name1=jim
2 name2=erik

有人知道怎么了吗

好吧,我想出来了,上面的代码可以正常工作,但是在打印出缓存中的内容之前,没有等待缓存同步足够长的时间。我在缓存检查周围添加了一个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);
    }
  }
publicstaticvoidmain(String[]args)抛出InterruptedException{
CacheManager=CacheManager.newInstance(“bin/ehcache.xml”);
Cache Cache=manager.getCache(“tprc”);
元素=新元素(“名称2”、“埃里克”);
cache.put(元素);
while(true){
对于(int i=1;i
2 name2=erik
2 name1=jim
2 name2=erik
  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);
    }
  }
<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>