ehcache分布式例子

阅读更多
配置文件:
recluster_ehcache_0.xml

    xsi:noNamespaceSchemaLocation="ehcache.xsd">      
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"      
        properties="peerDiscovery=manual,rmiUrls=//10.1.36.100:40000/UserCache"/>      
      
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"      
        properties="hostName=10.1.36.100,port=40001,socketTimeoutMillis=120000" />      
      
            timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"      
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"      
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"      
        memoryStoreEvictionPolicy="LRU">      
                  properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />    
   
      
      
            timeToIdleSeconds="100000" timeToLiveSeconds="100000"      
        overflowToDisk="false">      
                properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />    
   
  
   
    

recluster_ehcache.xml
    xsi:noNamespaceSchemaLocation="ehcache.xsd">      
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"      
        properties="peerDiscovery=manual,rmiUrls=//127.0.0.1:40001/UserCache"/>      
      
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"      
        properties="hostName=10.1.36.100,port=40000,socketTimeoutMillis=120000" />      
      
            timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"      
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"      
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"      
        memoryStoreEvictionPolicy="LRU">      
               properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />    
   
      
      
            timeToIdleSeconds="100000" timeToLiveSeconds="100000"      
        overflowToDisk="false">      
                  properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= false, replicateRemovals= true " />   
   
    
   
   
    
    


代码:

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

}

你可能感兴趣的:(cache)