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 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 + 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 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() { 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/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 diff --git a/src/com/r/tomcat/session/management/RequestSessionManager.java b/src/com/r/tomcat/session/management/RequestSessionManager.java index 843c7f5..0ed3ce3 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=1800; + } +// 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();