Ehcache缓存同步

Ehcache缓存同步有几种方式:(1)RMI (2)Terrocotta (3)JMS (4)JGroups
先介绍下,利用RMI进行缓存同步。
测试类1:在sampleDistributedCache2缓存中查找是否存在ehcache键,如果没找到,则打印NotFound;如果找到了,则打印相应值
view plaincopy to clipboardprint?
1. package my.test.ehcache1; 
2.  
3. import java.io.InputStream; 
4. import java.lang.management.ManagementFactory; 
5.  
6. import javax.management.MBeanServer; 
7.  
8. import net.sf.ehcache.Cache; 
9. import net.sf.ehcache.CacheManager; 
10. import net.sf.ehcache.Element; 
11. import net.sf.ehcache.management.ManagementService; 
12.  
13. public class EHCacheTest { 
14.  
15.     public static void main(String[] args) { 
16.         InputStream is = EHCacheTest.class 
17.                 .getResourceAsStream("/my/test/ehcache1/ehcache.xml"); 
18.         //读入配置 
19.         CacheManager cacheManager = new CacheManager(is); 
20.         //打印初始缓存 
21.         String[] cacheNames = cacheManager.getCacheNames(); 
22.         printNames(cacheNames); 
23.         //移除缓存 
24.         cacheManager.removeCache("sampleDistributedCache1"); 
25.         cacheNames = cacheManager.getCacheNames(); 
26.         printNames(cacheNames); 
27.  
28.         //新建缓存 
29.         Cache cache = new Cache("Test1", 100, true, false, 10, 10); 
30.         cacheManager.addCache(cache); 
31.         cacheNames = cacheManager.getCacheNames(); 
32.         printNames(cacheNames); 
33.         cache.put(new Element("test1", "value1")); 
34.          
35.         //得到缓存并插入值(这里监听器被调用) 
36.         cache = cacheManager.getCache("sampleCache3"); 
37.         for (int i = 0; i < 20; i++) { 
38.             cache.put(new Element("key" + i, "value" + i)); 
39.         } 
40.         cache.get("key10"); 
41.          
42.         // distributed -- rmi同步 
43.         cache = cacheManager.getCache("sampleDistributedCache2"); 
44.         for (int i = 0; i < 100; i++) { 
45.             cache.put(new Element("key" + i , "value" + i)); 
46.         } 
47.          
48.         //注册被管理的Bean 
49.         // JMX -- jconsole(MBeanServer) 
50.         MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); 
51.         ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, 
52.                 true, true); 
53.  
54.         for (int i = 0; i < 10; i++) { 
55.             Element temp = cache.get("ehcache"); 
56.             if (temp != null) { 
57.                 System.out.println(temp.getValue()); 
58.             } else { 
59.                 System.out.println("NotFound"); 
60.             } 
61.             try { 
62.                 Thread.sleep(10000); 
63.             } catch (InterruptedException e) { 
64.                 e.printStackTrace(); 
65.             } 
66.         } 
67.  
68.         // distributed cache using RMI 
69.         // 1.Peer Discovery -- cacheManagerPeerProviderFactory 
70.         // 2.CacheManager -- cacheManagerPeerListenerFactory 
71.         // 3.cache replication -- cacheEventListenerFactory 
72.         // 4.Bootstrap -- 启动后同步 
73.     } 
74.  
75.     private static void printNames(String[] names) { 
76.         System.out.println("======================="); 
77.         for (int i = 0; i < names.length; i++) { 
78.             System.out.println(names[i]); 
79.         } 
80.     } 
81.  
82. } 

配置文件1:将官方样例文件中相应位置替换即可
(1)其他JVM提供缓存的rmiUrl地址
view plaincopy to clipboardprint?
1. <cacheManagerPeerProviderFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
3.             properties="peerDiscovery=manual, 
4.                         rmiUrls=//localhost:40002/sampleDistributedCache2" 
5.             propertySeparator="," 
6.     /> 

(2)监听来自于其他复制节点消息的本JVM的host,port
view plaincopy to clipboardprint?
1. <cacheManagerPeerListenerFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" 
3.             properties="hostName=localhost, port=40001, socketTimeoutMillis=2000"  
4.     /> 

(3)配置复制选项,启动时同步等
view plaincopy to clipboardprint?
1. <cache name="sampleDistributedCache2" 
2.            maxElementsInMemory="10" 
3.            eternal="false" 
4.            timeToIdleSeconds="100" 
5.            timeToLiveSeconds="100" 
6.            overflowToDisk="false"> 
7.         <cacheEventListenerFactory 
8.                 class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
9.                 properties="replicateAsynchronously=true, replicatePuts=true, 
10.                             replicatePutsViaCopy=true, replicateUpdates=true, 
11.                             replicateUpdatesViaCopy=true, replicateRemovals=true, 
12.                             asynchronousReplicationIntervalMillis=200"/> 
13.         <bootstrapCacheLoaderFactory 
14.                 class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> 
15.     </cache> 

测试类2:向sampleDistributedCache2缓存中添加ehcache键
view plaincopy to clipboardprint?
1. package my.test.ehcache2; 
2.  
3. import java.io.InputStream; 
4. import java.lang.management.ManagementFactory; 
5.  
6. import javax.management.MBeanServer; 
7.  
8. import net.sf.ehcache.Cache; 
9. import net.sf.ehcache.CacheManager; 
10. import net.sf.ehcache.Element; 
11. import net.sf.ehcache.Statistics; 
12. import net.sf.ehcache.management.ManagementService; 
13. import net.sf.ehcache.statistics.LiveCacheStatistics; 
14.  
15.  
16. public class EHCacheTest { 
17.      
18.     public static void main(String[] args) { 
19.         try { 
20.             Thread.sleep(3 * 1000); 
21.         } catch (InterruptedException e) { 
22.             e.printStackTrace(); 
23.         } 
24.         //读入配置 
25.         InputStream is = EHCacheTest.class.getResourceAsStream("/my/test/ehcache2/ehcache.xml"); 
26.         CacheManager cacheManager = new CacheManager(is); 
27.         //打印初始缓存 
28.         String[] cacheNames = cacheManager.getCacheNames(); 
29.         printNames(cacheNames); 
30.         //注册管理Bean 
31.         MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); 
32.         ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); 
33.          
34.         //distributed 
35.         Cache cache = cacheManager.getCache("sampleDistributedCache2"); 
36.         printCache(cache); 
37.         //添加值后另一个虚拟机的缓存通过RMI会同步缓存,并读到这个值 
38.         cache.put(new Element("ehcache", "newaddvalue")); 
39.     } 
40.      
41.      
42.     private static void printNames(String[] names) { 
43.         System.out.println("======================="); 
44.         for (int i = 0; i < names.length; i++) { 
45.             System.out.println(names[i]); 
46.         } 
47.     } 
48.      
49.     private static void printCache(Cache cache) { 
50.         int size = cache.getSize(); 
51.         long memSize = cache.getMemoryStoreSize(); 
52.         long diskSize = cache.getDiskStoreSize(); 
53.         Statistics stat = cache.getStatistics(); 
54.         LiveCacheStatistics liveStat = cache.getLiveCacheStatistics(); 
55.         long hits = stat.getCacheHits(); 
56.         long missed = stat.getCacheMisses(); 
57.         long hitsOnDisk = stat.getOnDiskHits(); 
58.         long liveHits = liveStat.getCacheHitCount(); 
59.         long liveMissed = liveStat.getCacheMissCount(); 
60.          
61.         StringBuilder sb = new StringBuilder(); 
62.         sb.append("size=" + size + ";memsize=" + memSize); 
63.         sb.append(";diskSize=" + diskSize + ";hits=" + hits); 
64.         sb.append(";missed=" + missed + ";liveHits=" + liveHits); 
65.         sb.append(";liveMissed=" + liveMissed + ";hitsOnDisk=" + hitsOnDisk); 
66.         System.out.println(sb.toString()); 
67.     } 
68. } 

配置文件:将官方样例文件中相应位置替换即可
view plaincopy to clipboardprint?
1. <cacheManagerPeerProviderFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
3.             properties="peerDiscovery=manual, 
4.                         rmiUrls=//localhost:40001/sampleDistributedCache2" 
5.             propertySeparator="," 
6.     /> 

view plaincopy to clipboardprint?
1. <cacheManagerPeerListenerFactory 
2.             class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" 
3.             properties="hostName=localhost, port=40002, socketTimeoutMillis=2000"  
4.     /> 

view plaincopy to clipboardprint?
1. <cache name="sampleDistributedCache2" 
2.            maxElementsInMemory="10" 
3.            eternal="false" 
4.            timeToIdleSeconds="100" 
5.            timeToLiveSeconds="100" 
6.            overflowToDisk="false"> 
7.         <cacheEventListenerFactory 
8.                 class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
9.                 properties="replicateAsynchronously=true, replicatePuts=true, 
10.                             replicatePutsViaCopy=true, replicateUpdates=true, 
11.                             replicateUpdatesViaCopy=true, replicateRemovals=true, 
12.                             asynchronousReplicationIntervalMillis=200"/> 
13.         <bootstrapCacheLoaderFactory 
14.                 class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> 
15.     </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,bean,.net,cache,配置管理)