From ee04495e203e9f58aab7a895877a0b30935b8016 Mon Sep 17 00:00:00 2001 From: wangdongxun Date: Thu, 22 Sep 2016 10:55:53 +0800 Subject: [PATCH 1/7] add redis database config --- .../r/tomcat/session/data/cache/constants/RedisConstants.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java b/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java index 7f01483..b7dd2f1 100644 --- a/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java +++ b/src/com/r/tomcat/session/data/cache/constants/RedisConstants.java @@ -23,6 +23,7 @@ public class RedisConstants public static final String TIME_BETWEENEVICTION = "redis.time.betweenEviction"; public static final String HOSTS = "redis.hosts"; public static final String PASSWORD = "redis.password"; + public static final String DATABASE = "redis.db"; public static final String IS_CLUSTER_ENABLED = "redis.cluster.enabled"; // Redis property default values From 3740cf404cd829f37912ca764ed494a35a883bbb Mon Sep 17 00:00:00 2001 From: wangdongxun Date: Thu, 22 Sep 2016 14:33:37 +0800 Subject: [PATCH 2/7] add redis database config --- src/com/r/tomcat/session/data/cache/RedisManager.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/com/r/tomcat/session/data/cache/RedisManager.java b/src/com/r/tomcat/session/data/cache/RedisManager.java index 807f4c7..0738251 100644 --- a/src/com/r/tomcat/session/data/cache/RedisManager.java +++ b/src/com/r/tomcat/session/data/cache/RedisManager.java @@ -57,6 +57,7 @@ public class RedisManager poolConfig.setNumTestsPerEvictionRun(testNumPerEviction); long timeBetweenEviction = Long.parseLong(properties.getProperty(RedisConstants.TIME_BETWEENEVICTION, RedisConstants.DEFAULT_TIME_BETWEENEVICTION_VALUE)); poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEviction); + int database = Integer.parseInt(properties.getProperty(RedisConstants.DATABASE, "0")); String hosts = properties.getProperty(RedisConstants.HOSTS, Protocol.DEFAULT_HOST.concat(":").concat(String.valueOf(Protocol.DEFAULT_PORT))); String host = null; int port = 0; @@ -74,8 +75,10 @@ public class RedisManager if (password == null || password == "" || password.isEmpty()) { pool = new JedisPool(poolConfig, host, port); } else { - pool = new JedisPool(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, password); +// pool = new JedisPool(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, password); + pool =new JedisPool(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, password,database); } + } public Jedis getJedis() { From 750478baa32972e77229c340d8eb6b0e8b0cd310 Mon Sep 17 00:00:00 2001 From: wangdongxun Date: Thu, 22 Sep 2016 19:57:20 +0800 Subject: [PATCH 3/7] repair redis session expire time unit bug --- .../cache/RequestSessionCacheFactory.java | 1 + .../management/RequestSessionManager.java | 34 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/com/r/tomcat/session/data/cache/RequestSessionCacheFactory.java b/src/com/r/tomcat/session/data/cache/RequestSessionCacheFactory.java index d8554df..fdd1988 100644 --- a/src/com/r/tomcat/session/data/cache/RequestSessionCacheFactory.java +++ b/src/com/r/tomcat/session/data/cache/RequestSessionCacheFactory.java @@ -50,6 +50,7 @@ public class RequestSessionCacheFactory resourceStream = null; properties = new Properties(); File file = new File(System.getProperty("catalina.base").concat(File.separator).concat("conf").concat(File.separator).concat(RedisConstants.REDIS_DATA_CACHE_PROPERTIES_FILE)); + log.info("redis properties:"+file.getPath().toString()); if (file.exists()) { resourceStream = new FileInputStream(file); } diff --git a/src/com/r/tomcat/session/management/RequestSessionManager.java b/src/com/r/tomcat/session/management/RequestSessionManager.java index 843c7f5..c4be4f8 100644 --- a/src/com/r/tomcat/session/management/RequestSessionManager.java +++ b/src/com/r/tomcat/session/management/RequestSessionManager.java @@ -133,8 +133,8 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle } catch (Exception e) { log.error("Error while initializing serializer/rediscache", e); } - log.info("The sessions will expire after " + (getContext().getSessionTimeout() * 60) + " seconds"); - getContext().setDistributable(true); +// log.info("The sessions will expire after " + (getContext().getSessionTimeout() * 60) + " seconds"); +// getContext().setDistributable(true); } @Override @@ -162,7 +162,7 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle customSession.setNew(true); customSession.setValid(true); customSession.setCreationTime(System.currentTimeMillis()); - customSession.setMaxInactiveInterval((getContext().getSessionTimeout() * 60)); +// customSession.setMaxInactiveInterval((getContext().getSessionTimeout() * 60)); customSession.setId(sessionId); customSession.tellNew(); } @@ -229,7 +229,9 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle @Override public void remove(Session session, boolean update) { - requestSessionCacheUtils.deleteKey(session.getId()); + // expire after 10 seconds + requestSessionCacheUtils.expire(session.getId(),10); +// requestSessionCacheUtils.deleteKey(session.getId()); } @Override @@ -262,7 +264,7 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle serializer.deserializeSessionData(data, customSession, metadata); customSession.setId(id); customSession.setNew(false); - customSession.setMaxInactiveInterval((getContext().getSessionTimeout() * 60)); +// customSession.setMaxInactiveInterval((getContext().getSessionTimeout() * 60)); customSession.access(); customSession.setValid(true); customSession.resetDirtyTracking(); @@ -296,8 +298,16 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle currentSessionSerializationMetadata.set(updatedSerializationMetadata); currentSessionIsPersisted.set(true); } - log.trace("Setting expire timeout on session [" + customSession.getId() + "] to " + (getContext().getSessionTimeout() * 60)); - requestSessionCacheUtils.expire(customSession.getId(), (getContext().getSessionTimeout() * 60)); + //time uite seconds + int timeOut=session.getMaxInactiveInterval(); + if(timeOut<1){ + timeOut=-1; + } +// log.trace("Setting expire timeout on session [" + customSession.getId() + "] to " + (getContext().getSessionTimeout() * 60)); +// requestSessionCacheUtils.expire(customSession.getId(), (getContext().getSessionTimeout() * 60)); + + log.trace("Setting expire timeout on session [" + customSession.getId() + "] to " + timeOut+"seconds"); + requestSessionCacheUtils.expire(customSession.getId(), timeOut); } catch (IOException e) { log.error("Error occured while storing the session object into redis", e); } @@ -332,10 +342,12 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle */ private void initializeSessionSerializer() throws Exception { serializer = new SessionDataSerializer(); - Loader loader = null; - if (getContext() != null) { - loader = getContext().getLoader(); - } +// Loader loader = null; +// +// if (getContext() != null) { +// loader = getContext().getLoader(); +// } + Loader loader =this.getContainer().getLoader(); ClassLoader classLoader = null; if (loader != null) { classLoader = loader.getClassLoader(); From 2105836de95d876a5f01965344fbb2973eda1a27 Mon Sep 17 00:00:00 2001 From: wangdongxun Date: Thu, 22 Sep 2016 19:57:45 +0800 Subject: [PATCH 4/7] repair redis session expire time unit bug --- resources/RedisDataCache.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/RedisDataCache.properties b/resources/RedisDataCache.properties index 0ab2265..23ae4ac 100644 --- a/resources/RedisDataCache.properties +++ b/resources/RedisDataCache.properties @@ -2,5 +2,6 @@ redis.hosts=127.0.0.1:6379 # Redis Password redis.password= +redis.db=0 # set true to enable redis cluster mode redis.cluster.enabled=false \ No newline at end of file From 6fd45ae926e47ed17c8d99000563032a356b3108 Mon Sep 17 00:00:00 2001 From: wangdongxun Date: Thu, 22 Sep 2016 19:58:55 +0800 Subject: [PATCH 5/7] add tomcat dependency for intellij idea --- pom.xml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pom.xml b/pom.xml index f38516d..29f1546 100644 --- a/pom.xml +++ b/pom.xml @@ -43,5 +43,19 @@ commons-logging 1.2 + + + + + org.apache.tomcat + tomcat-catalina + 7.0.42 + + + + org.apache.tomcat + tomcat-servlet-api + 7.0.42 + \ No newline at end of file From d175860c6b299cbd215c48f74450b232e26e4d5c Mon Sep 17 00:00:00 2001 From: wangdongxun Date: Thu, 22 Sep 2016 20:09:26 +0800 Subject: [PATCH 6/7] add modify explain --- resources/ReadMe.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/resources/ReadMe.txt b/resources/ReadMe.txt index d6af936..15ca48d 100644 --- a/resources/ReadMe.txt +++ b/resources/ReadMe.txt @@ -40,3 +40,8 @@ Steps to be done, Note: * The Redis session manager supports, both single redis master and redis cluster based on the redis.properties configuration. + +modify explain: + * add redis select database + * repair redis session time unit bug,timeout=session.getMaxInactiveInterval(),unit =seconds + From 918ca2e8b16055057fd340d353d0eebc74659323 Mon Sep 17 00:00:00 2001 From: wangdongxun Date: Thu, 22 Sep 2016 22:18:16 +0800 Subject: [PATCH 7/7] default timeout=1800 seconds --- src/com/r/tomcat/session/management/RequestSessionManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/r/tomcat/session/management/RequestSessionManager.java b/src/com/r/tomcat/session/management/RequestSessionManager.java index c4be4f8..0ed3ce3 100644 --- a/src/com/r/tomcat/session/management/RequestSessionManager.java +++ b/src/com/r/tomcat/session/management/RequestSessionManager.java @@ -301,7 +301,7 @@ public class RequestSessionManager extends ManagerBase implements Lifecycle //time uite seconds int timeOut=session.getMaxInactiveInterval(); if(timeOut<1){ - timeOut=-1; + timeOut=1800; } // log.trace("Setting expire timeout on session [" + customSession.getId() + "] to " + (getContext().getSessionTimeout() * 60)); // requestSessionCacheUtils.expire(customSession.getId(), (getContext().getSessionTimeout() * 60));