mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
code clean up for common module
This commit is contained in:
@@ -26,6 +26,6 @@ package me.chanjar.weixin.common.session;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String Package = "me.chanjar.weixin.common.session";
|
||||
public static final String Package = "me.chanjar.weixin.common.session";
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,11 @@ public interface InternalSession {
|
||||
*/
|
||||
WxSession getSession();
|
||||
|
||||
/**
|
||||
* Return the <code>isValid</code> flag for this session.
|
||||
*/
|
||||
boolean isValid();
|
||||
|
||||
/**
|
||||
* Set the <code>isValid</code> flag for this session.
|
||||
*
|
||||
@@ -15,11 +20,6 @@ public interface InternalSession {
|
||||
*/
|
||||
void setValid(boolean isValid);
|
||||
|
||||
/**
|
||||
* Return the <code>isValid</code> flag for this session.
|
||||
*/
|
||||
boolean isValid();
|
||||
|
||||
/**
|
||||
* Return the session identifier for this session.
|
||||
*/
|
||||
|
||||
@@ -7,11 +7,10 @@ public interface InternalSessionManager {
|
||||
* 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
|
||||
* @throws IllegalStateException if a new session cannot be
|
||||
* instantiated for any reason
|
||||
* @throws java.io.IOException if an input/output error occurs while
|
||||
* processing this request
|
||||
*/
|
||||
InternalSession findSession(String id);
|
||||
|
||||
@@ -23,10 +22,10 @@ public interface InternalSessionManager {
|
||||
* <code>null</code>.
|
||||
*
|
||||
* @param sessionId The session id which should be used to create the
|
||||
* new session; if <code>null</code>, a new session id will be
|
||||
* generated
|
||||
* @exception IllegalStateException if a new session cannot be
|
||||
* instantiated for any reason
|
||||
* new session; if <code>null</code>, a new session id will be
|
||||
* generated
|
||||
* @throws IllegalStateException if a new session cannot be
|
||||
* instantiated for any reason
|
||||
*/
|
||||
InternalSession createSession(String sessionId);
|
||||
|
||||
@@ -40,8 +39,8 @@ public interface InternalSessionManager {
|
||||
/**
|
||||
* Remove this Session from the active Sessions for this Manager.
|
||||
*
|
||||
* @param session Session to be removed
|
||||
* @param update Should the expiration statistics be updated
|
||||
* @param session Session to be removed
|
||||
* @param update Should the expiration statistics be updated
|
||||
*/
|
||||
void remove(InternalSession session, boolean update);
|
||||
|
||||
@@ -59,6 +58,7 @@ public interface InternalSessionManager {
|
||||
* @return number of sessions active
|
||||
*/
|
||||
int getActiveSessions();
|
||||
|
||||
/**
|
||||
* Get a session from the recycled ones or create a new empty one.
|
||||
* The PersistentManager manager does not need to create session data
|
||||
@@ -88,6 +88,7 @@ public interface InternalSessionManager {
|
||||
* 要和{@link #setBackgroundProcessorDelay(int)}联合起来看
|
||||
* 如果把这个数字设置为6(默认),那么就是说manager要等待 6 * backgroundProcessorDay的时间才会清理过期session
|
||||
* </pre>
|
||||
*
|
||||
* @param processExpiresFrequency the new manager checks frequency
|
||||
*/
|
||||
void setProcessExpiresFrequency(int processExpiresFrequency);
|
||||
@@ -97,6 +98,7 @@ public interface InternalSessionManager {
|
||||
* Set the manager background processor delay
|
||||
* 设置manager sleep几秒,尝试执行一次background操作(清理过期session)
|
||||
* </pre>
|
||||
*
|
||||
* @param backgroundProcessorDelay
|
||||
*/
|
||||
void setBackgroundProcessorDelay(int backgroundProcessorDelay);
|
||||
@@ -106,6 +108,7 @@ public interface InternalSessionManager {
|
||||
* 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);
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
applicationSession.session.ise=invalid session state
|
||||
applicationSession.value.iae=null value
|
||||
fileStore.saving=Saving Session {0} to file {1}
|
||||
|
||||
@@ -12,17 +12,68 @@ public class StandardSession implements WxSession, InternalSession {
|
||||
* The string manager for this package.
|
||||
*/
|
||||
protected static final StringManager sm =
|
||||
StringManager.getManager(Constants.Package);
|
||||
|
||||
StringManager.getManager(Constants.Package);
|
||||
/**
|
||||
* Type array.
|
||||
*/
|
||||
protected static final String EMPTY_ARRAY[] = new String[0];
|
||||
// ------------------------------ WxSession
|
||||
protected Map<String, Object> attributes = new ConcurrentHashMap<String, Object>();
|
||||
/**
|
||||
* The session identifier of this Session.
|
||||
*/
|
||||
protected String id = null;
|
||||
/**
|
||||
* Flag indicating whether this session is valid or not.
|
||||
*/
|
||||
protected volatile boolean isValid = false;
|
||||
/**
|
||||
* We are currently processing a session expiration, so bypass
|
||||
* certain IllegalStateException tests. NOTE: This value is not
|
||||
* included in the serialized version of this object.
|
||||
*/
|
||||
protected transient volatile boolean expiring = false;
|
||||
/**
|
||||
* The Manager with which this Session is associated.
|
||||
*/
|
||||
protected transient InternalSessionManager manager = null;
|
||||
|
||||
// ------------------------------ InternalSession
|
||||
/**
|
||||
* The time this session was created, in milliseconds since midnight,
|
||||
* January 1, 1970 GMT.
|
||||
*/
|
||||
protected long creationTime = 0L;
|
||||
/**
|
||||
* The current accessed time for this session.
|
||||
*/
|
||||
protected volatile long thisAccessedTime = creationTime;
|
||||
/**
|
||||
* The default maximum inactive interval for Sessions created by
|
||||
* this Manager.
|
||||
*/
|
||||
protected int maxInactiveInterval = 30 * 60;
|
||||
/**
|
||||
* The facade associated with this session. NOTE: This value is not
|
||||
* included in the serialized version of this object.
|
||||
*/
|
||||
protected transient StandardSessionFacade facade = null;
|
||||
/**
|
||||
* The access count for this session.
|
||||
*/
|
||||
protected transient AtomicInteger accessCount = null;
|
||||
|
||||
public StandardSession(InternalSessionManager manager) {
|
||||
this.manager = manager;
|
||||
this.accessCount = new AtomicInteger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String name) {
|
||||
|
||||
if (!isValidInternal())
|
||||
throw new IllegalStateException
|
||||
(sm.getString("sessionImpl.getAttribute.ise"));
|
||||
(sm.getString("sessionImpl.getAttribute.ise"));
|
||||
|
||||
if (name == null) return null;
|
||||
|
||||
@@ -33,7 +84,7 @@ public class StandardSession implements WxSession, InternalSession {
|
||||
public Enumeration<String> getAttributeNames() {
|
||||
if (!isValidInternal())
|
||||
throw new IllegalStateException
|
||||
(sm.getString("sessionImpl.getAttributeNames.ise"));
|
||||
(sm.getString("sessionImpl.getAttributeNames.ise"));
|
||||
|
||||
Set<String> names = new HashSet<String>();
|
||||
names.addAll(attributes.keySet());
|
||||
@@ -45,7 +96,7 @@ public class StandardSession implements WxSession, InternalSession {
|
||||
// Name cannot be null
|
||||
if (name == null)
|
||||
throw new IllegalArgumentException
|
||||
(sm.getString("sessionImpl.setAttribute.namenull"));
|
||||
(sm.getString("sessionImpl.setAttribute.namenull"));
|
||||
|
||||
// Null value is the same as removeAttribute()
|
||||
if (value == null) {
|
||||
@@ -56,97 +107,32 @@ public class StandardSession implements WxSession, InternalSession {
|
||||
// Validate our current state
|
||||
if (!isValidInternal())
|
||||
throw new IllegalStateException(sm.getString(
|
||||
"sessionImpl.setAttribute.ise", getIdInternal()));
|
||||
"sessionImpl.setAttribute.ise", getIdInternal()));
|
||||
|
||||
attributes.put(name, value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removeAttribute(String name) {
|
||||
removeAttributeInternal(name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
if (!isValidInternal())
|
||||
throw new IllegalStateException
|
||||
(sm.getString("sessionImpl.invalidate.ise"));
|
||||
(sm.getString("sessionImpl.invalidate.ise"));
|
||||
|
||||
// Cause this session to expire
|
||||
expire();
|
||||
|
||||
}
|
||||
|
||||
// ------------------------------ InternalSession
|
||||
/**
|
||||
* The session identifier of this Session.
|
||||
*/
|
||||
protected String id = null;
|
||||
|
||||
/**
|
||||
* Flag indicating whether this session is valid or not.
|
||||
*/
|
||||
protected volatile boolean isValid = false;
|
||||
|
||||
/**
|
||||
* We are currently processing a session expiration, so bypass
|
||||
* certain IllegalStateException tests. NOTE: This value is not
|
||||
* included in the serialized version of this object.
|
||||
*/
|
||||
protected transient volatile boolean expiring = false;
|
||||
|
||||
/**
|
||||
* The Manager with which this Session is associated.
|
||||
*/
|
||||
protected transient InternalSessionManager manager = null;
|
||||
|
||||
/**
|
||||
* Type array.
|
||||
*/
|
||||
protected static final String EMPTY_ARRAY[] = new String[0];
|
||||
|
||||
/**
|
||||
* The time this session was created, in milliseconds since midnight,
|
||||
* January 1, 1970 GMT.
|
||||
*/
|
||||
protected long creationTime = 0L;
|
||||
|
||||
/**
|
||||
* The current accessed time for this session.
|
||||
*/
|
||||
protected volatile long thisAccessedTime = creationTime;
|
||||
|
||||
/**
|
||||
* The default maximum inactive interval for Sessions created by
|
||||
* this Manager.
|
||||
*/
|
||||
protected int maxInactiveInterval = 30 * 60;
|
||||
|
||||
/**
|
||||
* The facade associated with this session. NOTE: This value is not
|
||||
* included in the serialized version of this object.
|
||||
*/
|
||||
protected transient StandardSessionFacade facade = null;
|
||||
|
||||
/**
|
||||
* The access count for this session.
|
||||
*/
|
||||
protected transient AtomicInteger accessCount = null;
|
||||
|
||||
|
||||
public StandardSession(InternalSessionManager manager) {
|
||||
this.manager = manager;
|
||||
this.accessCount = new AtomicInteger();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public WxSession getSession() {
|
||||
|
||||
if (facade == null){
|
||||
if (facade == null) {
|
||||
facade = new StandardSessionFacade(this);
|
||||
}
|
||||
return (facade);
|
||||
@@ -161,16 +147,6 @@ public class StandardSession implements WxSession, InternalSession {
|
||||
return this.isValid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the <code>isValid</code> flag for this session.
|
||||
*
|
||||
* @param isValid The new value for the <code>isValid</code> flag
|
||||
*/
|
||||
@Override
|
||||
public void setValid(boolean isValid) {
|
||||
this.isValid = isValid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
if (!this.isValid) {
|
||||
@@ -197,6 +173,16 @@ public class StandardSession implements WxSession, InternalSession {
|
||||
return this.isValid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the <code>isValid</code> flag for this session.
|
||||
*
|
||||
* @param isValid The new value for the <code>isValid</code> flag
|
||||
*/
|
||||
@Override
|
||||
public void setValid(boolean isValid) {
|
||||
this.isValid = isValid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdInternal() {
|
||||
return (this.id);
|
||||
|
||||
@@ -13,16 +13,65 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
*/
|
||||
public class StandardSessionManager implements WxSessionManager, InternalSessionManager {
|
||||
|
||||
protected final Logger log = LoggerFactory.getLogger(StandardSessionManager.class);
|
||||
|
||||
protected static final StringManager sm =
|
||||
StringManager.getManager(Constants.Package);
|
||||
StringManager.getManager(Constants.Package);
|
||||
/**
|
||||
* The descriptive name of this Manager implementation (for logging).
|
||||
*/
|
||||
private static final String name = "SessionManagerImpl";
|
||||
protected final Logger log = LoggerFactory.getLogger(StandardSessionManager.class);
|
||||
private final Object maxActiveUpdateLock = new Object();
|
||||
/**
|
||||
* 后台清理线程是否已经开启
|
||||
*/
|
||||
private final AtomicBoolean backgroundProcessStarted = new AtomicBoolean(false);
|
||||
|
||||
|
||||
// -------------------------------------- InternalSessionManager
|
||||
/**
|
||||
* The set of currently active Sessions for this Manager, keyed by
|
||||
* session identifier.
|
||||
*/
|
||||
protected Map<String, InternalSession> sessions = new ConcurrentHashMap<String, InternalSession>();
|
||||
/**
|
||||
* The maximum number of active Sessions allowed, or -1 for no limit.
|
||||
*/
|
||||
protected int maxActiveSessions = -1;
|
||||
|
||||
/**
|
||||
* Number of session creations that failed due to maxActiveSessions.
|
||||
*/
|
||||
protected int rejectedSessions = 0;
|
||||
|
||||
/**
|
||||
* The default maximum inactive interval for Sessions created by
|
||||
* this Manager.
|
||||
*/
|
||||
protected int maxInactiveInterval = 30 * 60;
|
||||
|
||||
// Number of sessions created by this manager
|
||||
protected long sessionCounter = 0;
|
||||
|
||||
protected volatile int maxActive = 0;
|
||||
/**
|
||||
* Processing time during session expiration.
|
||||
*/
|
||||
protected long processingTime = 0;
|
||||
/**
|
||||
* Frequency of the session expiration, and related manager operations.
|
||||
* Manager operations will be done once for the specified amount of
|
||||
* backgrondProcess calls (ie, the lower the amount, the most often the
|
||||
* checks will occur).
|
||||
*/
|
||||
protected int processExpiresFrequency = 6;
|
||||
/**
|
||||
* background processor delay in seconds
|
||||
*/
|
||||
protected int backgroundProcessorDelay = 10;
|
||||
/**
|
||||
* Iteration count for background processing.
|
||||
*/
|
||||
private int count = 0;
|
||||
|
||||
@Override
|
||||
public WxSession getSession(String sessionId) {
|
||||
@@ -33,7 +82,7 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
public WxSession getSession(String sessionId, boolean create) {
|
||||
if (sessionId == null) {
|
||||
throw new IllegalStateException
|
||||
(sm.getString("sessionManagerImpl.getSession.ise"));
|
||||
(sm.getString("sessionManagerImpl.getSession.ise"));
|
||||
}
|
||||
|
||||
InternalSession session = findSession(sessionId);
|
||||
@@ -60,64 +109,6 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
return session.getSession();
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------- InternalSessionManager
|
||||
/**
|
||||
* The descriptive name of this Manager implementation (for logging).
|
||||
*/
|
||||
private static final String name = "SessionManagerImpl";
|
||||
|
||||
/**
|
||||
* The maximum number of active Sessions allowed, or -1 for no limit.
|
||||
*/
|
||||
protected int maxActiveSessions = -1;
|
||||
|
||||
/**
|
||||
* Number of session creations that failed due to maxActiveSessions.
|
||||
*/
|
||||
protected int rejectedSessions = 0;
|
||||
|
||||
/**
|
||||
* The default maximum inactive interval for Sessions created by
|
||||
* this Manager.
|
||||
*/
|
||||
protected int maxInactiveInterval = 30 * 60;
|
||||
|
||||
// Number of sessions created by this manager
|
||||
protected long sessionCounter=0;
|
||||
|
||||
protected volatile int maxActive=0;
|
||||
|
||||
private final Object maxActiveUpdateLock = new Object();
|
||||
|
||||
/**
|
||||
* Processing time during session expiration.
|
||||
*/
|
||||
protected long processingTime = 0;
|
||||
|
||||
/**
|
||||
* Iteration count for background processing.
|
||||
*/
|
||||
private int count = 0;
|
||||
|
||||
/**
|
||||
* Frequency of the session expiration, and related manager operations.
|
||||
* Manager operations will be done once for the specified amount of
|
||||
* backgrondProcess calls (ie, the lower the amount, the most often the
|
||||
* checks will occur).
|
||||
*/
|
||||
protected int processExpiresFrequency = 6;
|
||||
|
||||
/**
|
||||
* background processor delay in seconds
|
||||
*/
|
||||
protected int backgroundProcessorDelay = 10;
|
||||
|
||||
/**
|
||||
* 后台清理线程是否已经开启
|
||||
*/
|
||||
private final AtomicBoolean backgroundProcessStarted = new AtomicBoolean(false);
|
||||
|
||||
@Override
|
||||
public void remove(InternalSession session) {
|
||||
remove(session, false);
|
||||
@@ -131,7 +122,6 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public InternalSession findSession(String id) {
|
||||
|
||||
@@ -145,15 +135,15 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
public InternalSession createSession(String sessionId) {
|
||||
if (sessionId == null) {
|
||||
throw new IllegalStateException
|
||||
(sm.getString("sessionManagerImpl.createSession.ise"));
|
||||
(sm.getString("sessionManagerImpl.createSession.ise"));
|
||||
}
|
||||
|
||||
if ((maxActiveSessions >= 0) &&
|
||||
(getActiveSessions() >= maxActiveSessions)) {
|
||||
(getActiveSessions() >= maxActiveSessions)) {
|
||||
rejectedSessions++;
|
||||
throw new TooManyActiveSessionsException(
|
||||
sm.getString("sessionManagerImpl.createSession.tmase"),
|
||||
maxActiveSessions);
|
||||
sm.getString("sessionManagerImpl.createSession.tmase"),
|
||||
maxActiveSessions);
|
||||
}
|
||||
|
||||
// Recycle or create a Session instance
|
||||
@@ -216,14 +206,14 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
|
||||
sessions.put(session.getIdInternal(), session);
|
||||
int size = getActiveSessions();
|
||||
if( size > maxActive ) {
|
||||
synchronized(maxActiveUpdateLock) {
|
||||
if( size > maxActive ) {
|
||||
if (size > maxActive) {
|
||||
synchronized (maxActiveUpdateLock) {
|
||||
if (size > maxActive) {
|
||||
maxActive = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -251,19 +241,19 @@ public class StandardSessionManager implements WxSessionManager, InternalSession
|
||||
|
||||
long timeNow = System.currentTimeMillis();
|
||||
InternalSession sessions[] = findSessions();
|
||||
int expireHere = 0 ;
|
||||
int expireHere = 0;
|
||||
|
||||
if(log.isDebugEnabled())
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("Start expire sessions {} at {} sessioncount {}", getName(), timeNow, sessions.length);
|
||||
for (int i = 0; i < sessions.length; i++) {
|
||||
if (sessions[i]!=null && !sessions[i].isValid()) {
|
||||
if (sessions[i] != null && !sessions[i].isValid()) {
|
||||
expireHere++;
|
||||
}
|
||||
}
|
||||
long timeEnd = System.currentTimeMillis();
|
||||
if(log.isDebugEnabled())
|
||||
if (log.isDebugEnabled())
|
||||
log.debug("End expire sessions {} processingTime {} expired sessions: {}", getName(), timeEnd - timeNow, expireHere);
|
||||
processingTime += ( timeEnd - timeNow );
|
||||
processingTime += (timeEnd - timeNow);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -21,37 +21,34 @@ package me.chanjar.weixin.common.session;
|
||||
* reached and the server is refusing to create any new sessions.
|
||||
*/
|
||||
public class TooManyActiveSessionsException
|
||||
extends IllegalStateException
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
extends IllegalStateException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* The maximum number of active sessions the server will tolerate.
|
||||
*/
|
||||
private final int maxActiveSessions;
|
||||
/**
|
||||
* The maximum number of active sessions the server will tolerate.
|
||||
*/
|
||||
private final int maxActiveSessions;
|
||||
|
||||
/**
|
||||
* Creates a new TooManyActiveSessionsException.
|
||||
*
|
||||
* @param message A description for the exception.
|
||||
* @param maxActive The maximum number of active sessions allowed by the
|
||||
* session manager.
|
||||
*/
|
||||
public TooManyActiveSessionsException(String message,
|
||||
int maxActive)
|
||||
{
|
||||
super(message);
|
||||
|
||||
maxActiveSessions = maxActive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the maximum number of sessions allowed by the session manager.
|
||||
*
|
||||
* @return The maximum number of sessions allowed by the session manager.
|
||||
*/
|
||||
public int getMaxActiveSessions()
|
||||
{
|
||||
return maxActiveSessions;
|
||||
}
|
||||
/**
|
||||
* Creates a new TooManyActiveSessionsException.
|
||||
*
|
||||
* @param message A description for the exception.
|
||||
* @param maxActive The maximum number of active sessions allowed by the
|
||||
* session manager.
|
||||
*/
|
||||
public TooManyActiveSessionsException(String message,
|
||||
int maxActive) {
|
||||
super(message);
|
||||
|
||||
maxActiveSessions = maxActive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the maximum number of sessions allowed by the session manager.
|
||||
*
|
||||
* @return The maximum number of sessions allowed by the session manager.
|
||||
*/
|
||||
public int getMaxActiveSessions() {
|
||||
return maxActiveSessions;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user