首页

基于Redis分布式存储实现分布式系统的用户会话管理Session

标签:同步session,分布式,云平台,Redis,分布式存储,HttpSession,UserSession,高可用,自定义     发布时间:2016-07-22   

对于分布式或集群应用系统的会话管理,这边基于分布式Redis来存储Session会话对象信息,保证会话对象可跨应用分享使用,具体实现代码如下

用户会话对象UserSession接口 - 面向业务信息

public abstract interface UserSession@b@{@b@  public abstract Object getAttribute(String paramString);@b@@b@  public abstract void setAttribute(String paramString, Object paramObject);@b@@b@  public abstract void removeAttribute(String paramString);@b@@b@  public abstract boolean containsAttribute(String paramString);@b@@b@  public abstract boolean isInvalidate();@b@@b@  public abstract String getId();@b@@b@  public abstract boolean destroy();@b@}

会话Session实现类UserSessionImpl - 实现业务(UserSession)和协议信息(HttpSession)接口实现

import com.xwd.cache.redis.map.RedisMap;@b@import java.util.Enumeration;@b@import java.util.Iterator;@b@import java.util.Set;@b@import javax.servlet.ServletContext;@b@import javax.servlet.http.HttpSession;@b@import javax.servlet.http.HttpSessionContext;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public class UserSessionImpl@b@  implements UserSession, HttpSession@b@{@b@  protected static Log logger = LogFactory.getLog(UserSessionImpl.class);@b@  private RedisMap<String, Object> attributes;@b@  private String sessionId;@b@  private boolean isNew;@b@  private boolean invalidate;@b@@b@  public UserSessionImpl(RedisMap<String, Object> attributes, String sessionId, boolean isNew, WeSessionConfig configure)@b@  {@b@    this.attributes = attributes;@b@    this.sessionId = sessionId;@b@    this.isNew = isNew;@b@    int expireTime = configure.getCacheExpiresTime();@b@@b@    if (isNew) {@b@      if (expireTime > 0)@b@        attributes.setExpiredTime(expireTime);@b@@b@    }@b@    else if (configure.isRefreshExpiresTime())@b@      attributes.setExpiredTime(expireTime);@b@  }@b@@b@  public Object getAttribute(String attrKey)@b@  {@b@    if (attrKey != null) if ((attrKey = attrKey.trim()).length() != 0) break label26;@b@    throw new IllegalArgumentException("attrKey requried.");@b@@b@    if (logger.isDebugEnabled())@b@      label26: logger.debug(new StringBuilder().append("GetAttribute<").append(attrKey).append("> by ").append(this.attributes.getId()).toString());@b@@b@    return this.attributes.get(attrKey);@b@  }@b@@b@  public void setAttribute(String attrKey, Object attrValue)@b@  {@b@    if (attrKey != null) if ((attrKey = attrKey.trim()).length() != 0) break label26;@b@    throw new IllegalArgumentException("attrKey requried.");@b@@b@    label26: this.attributes.put(attrKey, attrValue);@b@    if (logger.isInfoEnabled())@b@      logger.info(new StringBuilder().append("SetAttribute<").append(attrKey).append(">...").toString());@b@@b@    this.attributes.put(attrKey, attrValue);@b@  }@b@@b@  public void removeAttribute(String attrKey)@b@  {@b@    Object result = this.attributes.remove(attrKey);@b@    if (logger.isDebugEnabled())@b@      logger.debug(new StringBuilder().append("removeAttribute<").append(attrKey).append(">=").append(result != null).append(" by ").append(this.attributes.getId()).toString());@b@  }@b@@b@  public boolean containsAttribute(String attrKey)@b@  {@b@    boolean result = this.attributes.containsKey(attrKey);@b@    if (logger.isDebugEnabled())@b@      logger.debug(new StringBuilder().append("containsAttribute<").append(attrKey).append(">=").append(result).append(" by ").append(this.attributes.getId()).toString());@b@@b@    return result;@b@  }@b@@b@  public boolean destroy()@b@  {@b@    return this.attributes.destroy();@b@  }@b@@b@  public long getCreationTime()@b@  {@b@    throw new UnsupportedOperationException("Not support method:getCreationTime");@b@  }@b@@b@  public String getId()@b@  {@b@    return this.sessionId;@b@  }@b@@b@  public long getLastAccessedTime()@b@  {@b@    throw new UnsupportedOperationException("Not support method:getLastAccessedTime");@b@  }@b@@b@  public ServletContext getServletContext()@b@  {@b@    throw new UnsupportedOperationException("Not support method:getServletContext");@b@  }@b@@b@  public void setMaxInactiveInterval(int paramInt)@b@  {@b@    throw new UnsupportedOperationException("Not support method:setMaxInactiveInterval");@b@  }@b@@b@  public int getMaxInactiveInterval()@b@  {@b@    throw new UnsupportedOperationException("Not support method:getMaxInactiveInterval");@b@  }@b@@b@  public HttpSessionContext getSessionContext()@b@  {@b@    throw new UnsupportedOperationException("Not support method:getSessionContext");@b@  }@b@@b@  public Object getValue(String name)@b@  {@b@    throw new UnsupportedOperationException("Not support method:getValue");@b@  }@b@@b@  public Enumeration<String> getAttributeNames()@b@  {@b@    Set names = this.attributes.keySet();@b@    if (names == null)@b@      return null;@b@@b@    Iterator i = names.iterator();@b@    Enumeration e = new Enumeration(this, i)@b@    {@b@      public boolean hasMoreElements() {@b@        return this.val$i.hasNext();@b@      }@b@@b@      public String nextElement()@b@      {@b@        return ((String)this.val$i.next());@b@      }@b@@b@    };@b@    return e;@b@  }@b@@b@  public String[] getValueNames()@b@  {@b@    throw new UnsupportedOperationException("Not support method:getValueNames");@b@  }@b@@b@  public void putValue(String name, Object value)@b@  {@b@    throw new UnsupportedOperationException("Not support method:putValue");@b@  }@b@@b@  public void removeValue(String name)@b@  {@b@    throw new UnsupportedOperationException("Not support method:removeValue");@b@  }@b@@b@  public void invalidate()@b@  {@b@    this.invalidate = true;@b@  }@b@@b@  public boolean isInvalidate() {@b@    return this.invalidate;@b@  }@b@@b@  public boolean isNew()@b@  {@b@    return this.isNew;@b@  }@b@}