ehcache集群数据不同步

benny周 发布于 2015/03/26 17:21
阅读 322
收藏 0

问题如题

运营环境:

server1 : win 7,tomcat。ip:192.168.1.213

server2:win 7中安装的centos6.6虚拟机,tomcat。ip:192.168.1.211.

需要缓存的App对象已经实现Serializable接口。两个server中的防火墙都已经关闭。

具体文件如下面所示。小弟刚接触,望各位不吝赐教,谢谢!


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=automatic, multicastGroupAddress=230.0.0.1,
        multicastGroupPort=40001, timeToLive=1"/>

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


    <cache name="sampleCache2"
        maxEntriesLocalHeap="10"
        eternal="false"
        timeToIdleSeconds="100"
        timeToLiveSeconds="3600"
        overflowToDisk="false">

        <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,

            replicateUpdatesViaCopy=false, replicateRemovals=true "/>

        <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>

    </cache>
</ehcache>  

EhcacheUtil.java文件:

public class EhcacheUtil implements Serializable {  
 
    /**

*/
    private static final long serialVersionUID = 1L;


    private static final String path = "/cache/ehcache.xml";  
  
    private URL url;  
  
    private CacheManager manager;  
  
    private static EhcacheUtil ehCache;  
  
    private EhcacheUtil(String path) {  
   
        url = getClass().getResource(path);  
        manager = CacheManager.create(url);  
    }  
  
    public static EhcacheUtil getInstance() {  
        if (ehCache== null) {  
            ehCache= new EhcacheUtil(path);  
        }  
        return ehCache;  
    }  
  
    public void put(String cacheName, String key, Object value) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = new Element(key, value);  
        cache.put(element);  
    }  
  
    public Object get(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        Element element = cache.get(key);  
        return element == null ? null : element.getObjectValue();  
    }  
  
    public Cache get(String cacheName) {  
        return manager.getCache(cacheName);  
    }  
  
    public void remove(String cacheName, String key) {  
        Cache cache = manager.getCache(cacheName);  
        cache.remove(key);  
    }  
}  

IndexController.java文件:

@WebServlet(description = "index", urlPatterns = { "/index" })
public class IndexController extends HttpServlet {
    private static final long serialVersionUID = 1L;
// private static App app = new App();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexController() {
        super();
        // TODO Auto-generated constructor stub
        App app = new App();
        EhcacheUtil.getInstance().put("sampleCache2", "APP", app);
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws         ServletException, IOException {
        // TODO Auto-generated method stub
        App obj = (App)EhcacheUtil.getInstance().get("sampleCache2","APP");
        obj.num++;
        EhcacheUtil.getInstance().put("sampleCache2","APP",obj);
        response.getOutputStream().write(("num="+obj.num).getBytes());
    }


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws         ServletException, IOException {
        // TODO Auto-generated method stub
    }
}



App.java文件:

public class App implements Serializable{
    
    private static final long serialVersionUID = 1L;
    public static int num=0;
    
}


加载中
返回顶部
顶部