Ehcache 下 用jgroup进行复制,Ehcache集群

从官方文档上了解到 用RMI 比较简单,按官方的配置,试了n遍都不成功。配置和网上的一样,还是不行,对rmi没有兴趣了,转用Jgroup.

1. 下载资源

Jgroup, http://sourceforge.net/projects/javagroups/files/JGroups/2.12.1.Final/jgroups-2.12.1.Final.jar/download

我的ehcache用的是2.4.2 Jgroup 要用2.1,我试了用 jgroup3.0 会报错。

ehcache-jgroupsreplication-1.4

http://ehcache.org/downloads/destination?name=ehcache-jgroupsreplication-1.4-distribution.tar.gz&bucket=tcdistributions&file=ehcache-jgroupsreplication-1.4-distribution.tar.gz

2. ehcache.xml ,放到classpath下,配置如下:


  


    
    
    
        
    

	 
     
            
      
       


测试代码:

package com.test.ehcache;

import java.util.List;

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

public class CacheLocalManager {
	private static final CacheManager cacheManager = new CacheManager();
	public CacheLocalManager() {
		// TODO Auto-generated constructor stub
	}
	public static CacheManager getCacheManager() {
		return cacheManager;
	}
	public static Cache getCache(String cacheName) {
		return getCacheManager().getCache(cacheName);
	}
	public static Object readCache(String key, String cacheName){
		try {
			Cache cache = getCache(cacheName);
			Element element = cache.get(key);
			if (element != null) {
				return element.getValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static boolean removeCache(String key, String cacheName){
		try {
			Cache cache = getCache(cacheName);
			cache.remove(key);
			cache.flush();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean saveCache(String key, Object value, String cacheName){
		try {
			Cache cache = getCache(cacheName);
			Element element = new Element(key,value);
			cache.put(element);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static boolean removeAllCache(String cacheName) {
		try {
			Cache cache = getCache(cacheName);
			cache.removeAll();
			cache.flush();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static int cacheSize(String cacheName) {
		try {
			Cache cache = getCache(cacheName);
			return cache.getSize();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public static List getCacheKeys(String cacheName) {
		try {
			Cache cache = getCache(cacheName);
			return cache.getKeys();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}

package com.test.ehcache;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Cache extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Cache() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String action = request.getParameter("action"); if(action.equals("read")){ String value = (String)CacheLocalManager.readCache("key", "Cache_Test"); out.print("cache data==>"+value); } if(action.equals("write")){ CacheLocalManager.saveCache("key", "cache test", "Cache_Test"); out.print("write ok"); } if(action.equals("clear")){ CacheLocalManager.removeCache("key", "Cache_Test"); out.print("clear ok"); } out.flush(); out.close(); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println(""); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
    read
	write
    clear
  


jar包为:

dom4j-1.6.1.jar

ehcache-core-2.4.2.jar

ehcache-terracotta-2.4.2.jar

log4j-1.2.13.jar

slf4j-api-1.6.1.jar

slf4j-simple-1.6.1.jar

ehcache-jgroupsreplication-1.4.jar

jgroups-2.12.1.Final.jar


你可能感兴趣的:(Ehcache 下 用jgroup进行复制,Ehcache集群)