怎样使用Memcached做Session同步
最近一直在看Xmemcached,希望能够通过它实现TOMCAT集群的Session复制,因为TOMCAT自己带的那个SESSION复制功能,当节点多了之后容易出错,且不稳定,还有就是不能同步Application里的东西,这几天捣鼓了很久,倒是把Application里的东西整合好了,但是让我头痛的是Session同步,不知道 这里有没有大侠做过类似的研究开发,希望能一起讨论下。
关于Application同步的代码我贴下:求指教
public class MemcachedServletContext{private static final Logger logger = Logger.getLogger(MemcachedServletContext.class); private MemcachedClient xmc = ApplicationHelper.XMEMCACHEDCLIENT;//全局XMC链接 private boolean SysMemCached = ApplicationHelper.SysMemCached.equals("1")?true:false; //系统是否启用Memcached功能 private String APPKEY = ApplicationHelper.APPKEY; private ServletContext application;public MemcachedServletContext(ServletContext application){this.application = application;}public Object getAttribute(String arg0) {Object obj = null;if(SysMemCached && !ClusterHelper.isNewObj(xmc, arg0)){ try { GetsResponse GRObj = xmc.gets(APPKEY+arg0); if(GRObj == null) return null; obj = GRObj.getValue(); application.setAttribute(arg0,obj); Hashtable Apptable = ApplicationHelper.SysMenCasHash;if(Apptable.containsKey(APPKEY+arg0)){Apptable.remove(APPKEY+arg0);}Apptable.put(APPKEY+arg0,GRObj.getCas());} catch (TimeoutException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (MemcachedException e) {e.printStackTrace();}}else{logger.debug("++++++++++++++++++++++++++");return application.getAttribute(arg0);} return obj;}public void removeAttribute(String arg0) {if(SysMemCached){ try {xmc.deleteWithNoReply(APPKEY+arg0);ClusterHelper.ReMoveObj(xmc,arg0);} catch (InterruptedException e) {e.printStackTrace();} catch (MemcachedException e) {e.printStackTrace();}}application.removeAttribute(arg0);}public void setAttribute(String arg0, Object arg1) {removeAttribute(arg0);application.setAttribute(arg0, arg1);if(SysMemCached && arg1 instanceof java.io.Serializable){try {if(ClusterHelper.isExist(xmc, arg0)){ //xmc.replaceWithNoReply(APPKEY+arg0, 0, arg1);new CASThread(xmc,0,APPKEY+arg0,arg1,new CountDownLatch(1)).start(); logger.debug("replace::"+APPKEY+arg0);}else{ xmc.addWithNoReply(APPKEY+arg0, 0, arg1); logger.debug("add::"+APPKEY+arg0);}ClusterHelper.setCasToHash(xmc,arg0,(xmc.gets(APPKEY+arg0)).getCas());} catch (TimeoutException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (MemcachedException e) {e.printStackTrace();}}}} 1 楼 wqy_blue3316 2011-07-08 自己顶下,难道没有人会么~