ehcache分布式例子

参考了网上的一个代码写的,开始不能运行成功,最后调试可以了
recluster_ehcache_0.xml:
<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=//10.1.36.100:40000/UserCache"/>      
      
    <cacheManagerPeerListenerFactory      
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"      
        properties="hostName=10.1.36.100,port=40001,socketTimeoutMillis=120000" />      
      
    <defaultCache maxElementsInMemory="10000" eternal="false"      
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"      
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"      
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"      
        memoryStoreEvictionPolicy="LRU">      
          <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
        properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />    
    </defaultCache>      
      
    <cache name="UserCache" maxElementsInMemory="1000" eternal="false"      
        timeToIdleSeconds="100000" timeToLiveSeconds="100000"      
        overflowToDisk="false">      
        <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
        properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />    
    </cache>  
    </ehcache> 
----------------------------------
recluster_ehcache.xml:
<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=//127.0.0.1:40001/UserCache"/>      
      
    <cacheManagerPeerListenerFactory      
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"      
        properties="hostName=10.1.36.100,port=40000,socketTimeoutMillis=120000" />      
      
    <defaultCache maxElementsInMemory="10000" eternal="false"      
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"      
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"      
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"      
        memoryStoreEvictionPolicy="LRU">      
       <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
        properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />    
    </defaultCache>      
      
    <cache name="UserCache" maxElementsInMemory="1000" eternal="false"      
        timeToIdleSeconds="100000" timeToLiveSeconds="100000"      
        overflowToDisk="false">      
          <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
        properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />   
    </cache>    
</ehcache>       
-------------------------------------------------------
代码:
package cache.echache.Init;

import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class clustertest {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException{
// TODO Auto-generated method stub
//URL url = clustertest.class.getClassLoader().getResource("recluster_ehcache.xml");
CacheManager manager = new CacheManager("recluster_ehcache.xml");  

//get Cache       
Cache cache = manager.getCache("UserCache");    
Thread.sleep(10000);
Element element = new Element("key1", "value1");  
Element element1 = new Element("key12", "value1");
Element element2 = new Element("key13", "value1");
Element element3 = new Element("key14", "value1");
cache.put(element); 
cache.put(element1);
cache.put(element2);
cache.put(element3);
System.out.println("Initial:\n"//+url.toString()
+"\n"+manager.getName()
+"\n"+cache.getName()
+" 's size = "+cache.getSize()
+"\n"+element.toString());    


Element element01 = cache.get("key1");       
System.out.println(element01.getValue()); 
Element element02 = cache.get("key12");       
System.out.println(element02.getValue());
System.out.println("主机测试等待中............."); 

while(true){
Thread.sleep(1000);
}
}

}
-------------------------
package cache.echache.Init;

import java.net.URL;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class clustertest0 {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException{
// TODO Auto-generated method stub
//URL url = clustertest.class.getClassLoader().getResource("recluster_ehcache.xml");

CacheManager manager = new CacheManager("recluster_ehcache_0.xml");  

//get Cache       
Cache cache = manager.getCache("UserCache");   
Thread.sleep(10000);
/*Element element = new Element("key1", "value1");  
Element element1 = new Element("key12", "value1");
Element element2 = new Element("key13", "value1");
Element element3 = new Element("key14", "value1");
cache.put(element); 
cache.put(element1);
cache.put(element2);
cache.put(element3);
System.out.println("Initial:\n"//+url.toString()
+"\n"+manager.getName()
+"\n"+cache.getName()
+" 's size = "+cache.getSize()
+"\n"+element.toString());     */

       
/*Element element01 = cache.get("key1");       
System.out.println(element01.getValue()); 
System.out.println("主机测试等待中.............");  */

while(true){
Element element01 = cache.get("key1"); 
if(null!=element01){
System.out.println(element01.getValue()); 

}
Thread.sleep(1000);
}
}

}

猜你喜欢

转载自lixuan74.iteye.com/blog/1717732