[quote]本文是ehcache RMI集群的例子,导入附件中的java 项目到eclipse中,依次启动附件中的A.java,B.java,C.java, 就可以在eclipse的console中就可以看到3个cache的内容同步啦[/quote]
1. ehcache 配置文件
2. A.java
1. ehcache 配置文件
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40002/UserCache|//localhost:40003/UserCache" />
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost,port=40001, socketTimeoutMillis=2000" />
<cache name="UserCache" maxElementsInMemory="10000" >
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true,
replicatePutsViaCopy=true, replicateUpdates=true,
replicateUpdatesViaCopy=true, replicateRemovals=true,
asynchronousReplicationIntervalMillis=200" />
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
</cache>
</ehcache>
2. A.java
CacheManager manager = new CacheManager("config/ehcache_cluster1.xml");
// 取得Cache
Cache cache = manager.getCache("UserCache");
Element element = new Element("Client A", "A");
cache.put(element);
while (true)
{
Thread.sleep(5000);
System.out.println("\n");
for (Object key : cache.getKeys())
{
System.out.println(key + ":" + cache.get(key));
}
cache.put(new Element("client A" + System.currentTimeMillis(), "client1"));
}