0

Hi Sorry for posting a question so similar to other ehcache replication problems but I've been banging my head on this most of the day and I've read a lot of stackoverflow posts without a solution.

I've attempted to set up the simplest ehcache replication test possible and it's not working. EhcacheTest writes one element into a cache named "tprc" then reads the cache and prints what it finds. EhcacheTest2 is nearly identical but writes a different element. I'm expecting EhcacheTest2 to show both values, the one written by EhcacheTest and EhcacheTest2.

Here's 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());
            }
        }
    }
}

Here's 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());
      }
    }
  }
}

And here's the ehcache.xml file:

<?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>

I run EhcacheTest, leave it running, then run EhcacheTest2. The output for EhcacheTest is:

1 name1=jim

and the output for EhcacheTest2 is:

2 name2=erik

I want the output for EhcacheTest2 to show

2 name1=jim
2 name2=erik

Does anybody know what's wrong?

4

1 回答 1

2

Ok I figured it out, the code above sort of worked but didn't wait long enough for the caches to sync before printing out what was in the cache. I added a while loop around the cache check and after a few iterations through the loop it each test program started showing cache elements from the other program. Here's the test program with the loop:

  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);
    }
  }

Another way to fix this is to add a synchronous bootstrapCacheLoaderFactor to the cache config in 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>
于 2013-09-12T20:44:22.717 回答