Ehcache利用RMI方式同步缓存

Ehcache缓存同步有几种方式:(1)RMI (2)Terrocotta (3)JMS (4)JGroups

先介绍下,利用RMI进行缓存同步。

 

测试类1:在sampleDistributedCache2缓存中查找是否存在ehcache键,如果没找到,则打印NotFound;如果找到了,则打印相应值

package my.test.ehcache1; import java.io.InputStream; import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.management.ManagementService; public class EHCacheTest { public static void main(String[] args) { InputStream is = EHCacheTest.class .getResourceAsStream("/my/test/ehcache1/ehcache.xml"); //读入配置 CacheManager cacheManager = new CacheManager(is); //打印初始缓存 String[] cacheNames = cacheManager.getCacheNames(); printNames(cacheNames); //移除缓存 cacheManager.removeCache("sampleDistributedCache1"); cacheNames = cacheManager.getCacheNames(); printNames(cacheNames); //新建缓存 Cache cache = new Cache("Test1", 100, true, false, 10, 10); cacheManager.addCache(cache); cacheNames = cacheManager.getCacheNames(); printNames(cacheNames); cache.put(new Element("test1", "value1")); //得到缓存并插入值(这里监听器被调用) cache = cacheManager.getCache("sampleCache3"); for (int i = 0; i < 20; i++) { cache.put(new Element("key" + i, "value" + i)); } cache.get("key10"); // distributed -- rmi同步 cache = cacheManager.getCache("sampleDistributedCache2"); for (int i = 0; i < 100; i++) { cache.put(new Element("key" + i , "value" + i)); } //注册被管理的Bean // JMX -- jconsole(MBeanServer) MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); for (int i = 0; i < 10; i++) { Element temp = cache.get("ehcache"); if (temp != null) { System.out.println(temp.getValue()); } else { System.out.println("NotFound"); } try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } // distributed cache using RMI // 1.Peer Discovery -- cacheManagerPeerProviderFactory // 2.CacheManager -- cacheManagerPeerListenerFactory // 3.cache replication -- cacheEventListenerFactory // 4.Bootstrap -- 启动后同步 } private static void printNames(String[] names) { System.out.println("======================="); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } } }

 

配置文件1:将官方样例文件中相应位置替换即可

(1)其他JVM提供缓存的rmiUrl地址

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual, rmiUrls=//localhost:40002/sampleDistributedCache2" propertySeparator="," />

(2)监听来自于其他复制节点消息的本JVM的host,port

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

(3)配置复制选项,启动时同步等

<cache name="sampleDistributedCache2" maxElementsInMemory="10" eternal="false" timeToIdleSeconds="100" timeToLiveSeconds="100" overflowToDisk="false"> <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>

 

 

测试类2:向sampleDistributedCache2缓存中添加ehcache键

package my.test.ehcache2; import java.io.InputStream; import java.lang.management.ManagementFactory; import javax.management.MBeanServer; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.Statistics; import net.sf.ehcache.management.ManagementService; import net.sf.ehcache.statistics.LiveCacheStatistics; public class EHCacheTest { public static void main(String[] args) { try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } //读入配置 InputStream is = EHCacheTest.class.getResourceAsStream("/my/test/ehcache2/ehcache.xml"); CacheManager cacheManager = new CacheManager(is); //打印初始缓存 String[] cacheNames = cacheManager.getCacheNames(); printNames(cacheNames); //注册管理Bean MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); //distributed Cache cache = cacheManager.getCache("sampleDistributedCache2"); printCache(cache); //添加值后另一个虚拟机的缓存通过RMI会同步缓存,并读到这个值 cache.put(new Element("ehcache", "newaddvalue")); } private static void printNames(String[] names) { System.out.println("======================="); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } } private static void printCache(Cache cache) { int size = cache.getSize(); long memSize = cache.getMemoryStoreSize(); long diskSize = cache.getDiskStoreSize(); Statistics stat = cache.getStatistics(); LiveCacheStatistics liveStat = cache.getLiveCacheStatistics(); long hits = stat.getCacheHits(); long missed = stat.getCacheMisses(); long hitsOnDisk = stat.getOnDiskHits(); long liveHits = liveStat.getCacheHitCount(); long liveMissed = liveStat.getCacheMissCount(); StringBuilder sb = new StringBuilder(); sb.append("size=" + size + ";memsize=" + memSize); sb.append(";diskSize=" + diskSize + ";hits=" + hits); sb.append(";missed=" + missed + ";liveHits=" + liveHits); sb.append(";liveMissed=" + liveMissed + ";hitsOnDisk=" + hitsOnDisk); System.out.println(sb.toString()); } }

 

配置文件:将官方样例文件中相应位置替换即可

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual, rmiUrls=//localhost:40001/sampleDistributedCache2" propertySeparator="," />

<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=localhost, port=40002, socketTimeoutMillis=2000" />

<cache name="sampleDistributedCache2" maxElementsInMemory="10" eternal="false" timeToIdleSeconds="100" timeToLiveSeconds="100" overflowToDisk="false"> <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>

 

运行结果:

JVM1

2009-12-13 12:24:04 net.sf.ehcache.distribution.RMICacheManagerPeerListener <init>
警告: Explicitly setting the listener hostname to 'localhost' is not recommended. It will only work if all CacheManager peers are on the same machine.
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleDistributedCache1
sampleCache3
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleCache3
=======================
sampleDistributedCache2
sampleCache2
Test1
sampleCache1
sample/DistributedCache3
sampleCache3
key0=value0 has been added
key1=value1 has been added
key2=value2 has been added
key3=value3 has been added
key4=value4 has been added
key5=value5 has been added
key6=value6 has been added
key7=value7 has been added
key8=value8 has been added
key9=value9 has been added
key7=value7 has been evicted
key10=value10 has been added
key0=value0 has been evicted
key11=value11 has been added
key9=value9 has been evicted
key12=value12 has been added
key3=value3 has been evicted
key13=value13 has been added
key1=value1 has been evicted
key14=value14 has been added
key4=value4 has been evicted
key15=value15 has been added
key13=value13 has been evicted
key16=value16 has been added
key8=value8 has been evicted
key17=value17 has been added
key17=value17 has been evicted
key18=value18 has been added
key18=value18 has been evicted
key19=value19 has been added
NotFound
newaddvalue

JVM2:

2009-12-13 12:24:13 net.sf.ehcache.distribution.RMICacheManagerPeerListener <init>
警告: Explicitly setting the listener hostname to 'localhost' is not recommended. It will only work if all CacheManager peers are on the same machine.
=======================
sampleDistributedCache2
sampleCache2
sampleCache1
sample/DistributedCache3
sampleDistributedCache1
sampleCache3
size=10;memsize=10;diskSize=0;hits=0;missed=0;liveHits=0;liveMissed=0;hitsOnDisk=0

你可能感兴趣的:(jvm,properties,cache,String,Class,statistics)