【问题标题】:Simplest ehcache replicated cache not working最简单的 ehcache 复制缓存不起作用
【发布时间】:2013-09-10 02:21:18
【问题描述】:

您好,很抱歉发布了一个与其他 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

有人知道怎么回事吗?

【问题讨论】:

    标签: caching ehcache


    【解决方案1】:

    好的,我想通了,上面的代码可以正常工作,但在打印出缓存中的内容之前没有等待足够长的时间让缓存同步。我在缓存检查周围添加了一个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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2011-05-31
      • 2011-04-14
      相关资源
      最近更新 更多