issue #69 添加Session的支持,添加测试用例

This commit is contained in:
Daniel Qian
2015-01-22 12:36:51 +08:00
parent d18b66c38d
commit 6368ca6816
16 changed files with 514 additions and 132 deletions

View File

@@ -2,6 +2,19 @@ package me.chanjar.weixin.common.session;
public interface InternalSessionManager {
/**
* Return the active Session, associated with this Manager, with the
* specified session id (if any); otherwise return <code>null</code>.
*
* @param id The session id for the session to be returned
*
* @exception IllegalStateException if a new session cannot be
* instantiated for any reason
* @exception java.io.IOException if an input/output error occurs while
* processing this request
*/
InternalSession findSession(String id);
/**
* Construct and return a new session object, based on the default
* settings specified by this Manager's properties. The session
@@ -68,7 +81,33 @@ public interface InternalSessionManager {
*/
void setMaxInactiveInterval(int interval);
/**
* <pre>
* Set the manager checks frequency.
* 设置每尝试多少次清理过期session才真的会执行一次清理动作
* 要和{@link #setBackgroundProcessorDelay(int)}联合起来看
* 如果把这个数字设置为6默认那么就是说manager要等待 6 * backgroundProcessorDay的时间才会清理过期session
* </pre>
* @param processExpiresFrequency the new manager checks frequency
*/
void setProcessExpiresFrequency(int processExpiresFrequency);
/**
* <pre>
* Set the manager background processor delay
* 设置manager sleep几秒尝试执行一次background操作清理过期session
* </pre>
* @param backgroundProcessorDelay
*/
void setBackgroundProcessorDelay(int backgroundProcessorDelay);
/**
* Set the maximum number of active Sessions allowed, or -1 for
* no limit.
* 设置最大活跃session数默认无限
* @param max The new maximum number of sessions
*/
void setMaxActiveSessions(int max);
}

View File

@@ -6,7 +6,7 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class SessionImpl implements WxSession, InternalSession {
public class StandardSession implements WxSession, InternalSession {
/**
* The string manager for this package.
@@ -129,7 +129,7 @@ public class SessionImpl implements WxSession, InternalSession {
* The facade associated with this session. NOTE: This value is not
* included in the serialized version of this object.
*/
protected transient InternalSessionFacade facade = null;
protected transient StandardSessionFacade facade = null;
/**
* The access count for this session.
@@ -137,7 +137,7 @@ public class SessionImpl implements WxSession, InternalSession {
protected transient AtomicInteger accessCount = null;
public SessionImpl(InternalSessionManager manager) {
public StandardSession(InternalSessionManager manager) {
this.manager = manager;
this.accessCount = new AtomicInteger();
}
@@ -147,7 +147,7 @@ public class SessionImpl implements WxSession, InternalSession {
public WxSession getSession() {
if (facade == null){
facade = new InternalSessionFacade(this);
facade = new StandardSessionFacade(this);
}
return (facade);
@@ -281,7 +281,6 @@ public class SessionImpl implements WxSession, InternalSession {
@Override
public void setMaxInactiveInterval(int interval) {
int oldMaxInactiveInterval = this.maxInactiveInterval;
this.maxInactiveInterval = interval;
}
@@ -311,9 +310,9 @@ public class SessionImpl implements WxSession, InternalSession {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SessionImpl)) return false;
if (!(o instanceof StandardSession)) return false;
SessionImpl session = (SessionImpl) o;
StandardSession session = (StandardSession) o;
if (creationTime != session.creationTime) return false;
if (expiring != session.expiring) return false;

View File

@@ -2,17 +2,21 @@ package me.chanjar.weixin.common.session;
import java.util.Enumeration;
public class InternalSessionFacade implements WxSession {
public class StandardSessionFacade implements WxSession {
/**
* Wrapped session object.
*/
private WxSession session = null;
public InternalSessionFacade(WxSession session) {
public StandardSessionFacade(StandardSession session) {
this.session = session;
}
public InternalSession getInternalSession() {
return (InternalSession) session;
}
@Override
public Object getAttribute(String name) {
return session.getAttribute(name);
@@ -37,4 +41,5 @@ public class InternalSessionFacade implements WxSession {
public void invalidate() {
session.invalidate();
}
}

View File

@@ -8,9 +8,12 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
public class InMemorySessionManager implements WxSessionManager, InternalSessionManager {
/**
* 基于内存的session manager
*/
public class StandardSessionManager implements WxSessionManager, InternalSessionManager {
protected final Logger log = LoggerFactory.getLogger(InMemorySessionManager.class);
protected final Logger log = LoggerFactory.getLogger(StandardSessionManager.class);
protected static final StringManager sm =
StringManager.getManager(Constants.Package);
@@ -128,18 +131,9 @@ public class InMemorySessionManager implements WxSessionManager, InternalSession
}
/**
* Return the active Session, associated with this Manager, with the
* specified session id (if any); otherwise return <code>null</code>.
*
* @param id The session id for the session to be returned
*
* @exception IllegalStateException if a new session cannot be
* instantiated for any reason
* @exception java.io.IOException if an input/output error occurs while
* processing this request
*/
protected InternalSession findSession(String id) {
@Override
public InternalSession findSession(String id) {
if (id == null)
return (null);
@@ -189,12 +183,11 @@ public class InMemorySessionManager implements WxSessionManager, InternalSession
return (getNewSession());
}
/**
* Get new session class to be used in the doLoad() method.
*/
protected InternalSession getNewSession() {
return new SessionImpl(this);
return new StandardSession(this);
}
@@ -312,4 +305,17 @@ public class InMemorySessionManager implements WxSessionManager, InternalSession
}
/**
* Set the maximum number of active Sessions allowed, or -1 for
* no limit.
*
* @param max The new maximum number of sessions
*/
@Override
public void setMaxActiveSessions(int max) {
this.maxActiveSessions = max;
}
}

View File

@@ -82,6 +82,9 @@ public class WxMessageInMemoryDuplicateChecker implements WxMessageDuplicateChec
@Override
public boolean isDuplicate(Long wxMsgId) {
if (wxMsgId == null) {
return false;
}
checkBackgroundProcessStarted();
Long timestamp = msgId2Timestamp.putIfAbsent(wxMsgId, System.currentTimeMillis());
if (timestamp == null) {