From edaf36c61e3e2c3715a87a7f381290cbfb306a57 Mon Sep 17 00:00:00 2001 From: devefx Date: Mon, 10 Oct 2016 15:20:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DredisSession=E8=BF=87?= =?UTF-8?q?=E6=9C=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tomcat/redissessions/RedisSession.java | 17 +++++++ .../redissessions/RedisSessionManager.java | 47 ++++++++++++++++--- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/orangefunction/tomcat/redissessions/RedisSession.java b/src/main/java/com/orangefunction/tomcat/redissessions/RedisSession.java index 0fa742d8..8706f14a 100644 --- a/src/main/java/com/orangefunction/tomcat/redissessions/RedisSession.java +++ b/src/main/java/com/orangefunction/tomcat/redissessions/RedisSession.java @@ -117,4 +117,21 @@ public void readObjectData(java.io.ObjectInputStream in) throws IOException, Cla this.setCreationTime(in.readLong()); } + @Override + public void access() { + if (getMaxInactiveInterval() > 0) { + Jedis jedis = null; + try { + jedis = ((RedisSessionManager) manager).acquireConnection(); + jedis.expire(getId().getBytes(), getMaxInactiveInterval()); + } catch (IOException e) { + log.error(e.getMessage()); + } finally { + if (jedis != null) { + returnConnection(jedis, error); + } + } + } + } + } diff --git a/src/main/java/com/orangefunction/tomcat/redissessions/RedisSessionManager.java b/src/main/java/com/orangefunction/tomcat/redissessions/RedisSessionManager.java index 2b58a261..79b4f958 100644 --- a/src/main/java/com/orangefunction/tomcat/redissessions/RedisSessionManager.java +++ b/src/main/java/com/orangefunction/tomcat/redissessions/RedisSessionManager.java @@ -54,9 +54,9 @@ static SessionPersistPolicy fromName(String name) { private final Log log = LogFactory.getLog(RedisSessionManager.class); - protected String host = "localhost"; - protected int port = 6379; - protected int database = 0; + protected String host = Protocol.DEFAULT_HOST; + protected int port = Protocol.DEFAULT_PORT; + protected int database = Protocol.DEFAULT_DATABASE; protected String password = null; protected int timeout = Protocol.DEFAULT_TIMEOUT; protected String sentinelMaster = null; @@ -78,6 +78,8 @@ static SessionPersistPolicy fromName(String name) { protected EnumSet sessionPersistPoliciesSet = EnumSet.of(SessionPersistPolicy.DEFAULT); + protected int maxActiveSessions = -1; + protected int rejectedSessions = 0; /** * The lifecycle event support for this component. */ @@ -189,14 +191,23 @@ public void setSentinelMaster(String master) { this.sentinelMaster = master; } + public int getMaxActiveSessions() { + return this.maxActiveSessions; + } + + public void setMaxActiveSessions(int max) { + int oldMaxActiveSessions = this.maxActiveSessions; + this.maxActiveSessions = max; + this.support.firePropertyChange("maxActiveSessions", new Integer(oldMaxActiveSessions), new Integer(this.maxActiveSessions)); + } + @Override public int getRejectedSessions() { - // Essentially do nothing. - return 0; + return rejectedSessions; } - public void setRejectedSessions(int i) { - // Do nothing. + public void setRejectedSessions(int rejectedSessions) { + this.rejectedSessions = rejectedSessions; } protected Jedis acquireConnection() { @@ -333,6 +344,11 @@ protected synchronized void stopInternal() throws LifecycleException { @Override public Session createSession(String requestedSessionId) { + if (maxActiveSessions >= 0 && getSize() >= maxActiveSessions) { + ++rejectedSessions; + throw new IllegalStateException(sm.getString("standardManager.createSession.ise")); + } + RedisSession session = null; String sessionId = null; String jvmRoute = getJvmRoute(); @@ -450,6 +466,23 @@ public Session findSession(String id) throws IOException { return session; } + @Override + public Session[] findSessions() { + try { + String[] keys = keys(); + Session[] sessions = new Session[keys.length]; + int i = 0; + for(String key : keys) { + byte[] data = loadSessionDataFromRedis(key); + DeserializedSessionContainer container = sessionFromSerializedData(key, data); + sessions[i++] = container.session; + } + return sessions; + } catch (IOException e) { + log.error("Error find sessions", e); + } + } + public void clear() { Jedis jedis = null; Boolean error = true;