From 92adf755baf33be4becc0bfec6342304067d1a36 Mon Sep 17 00:00:00 2001 From: Ranjith Manickam Date: Sun, 2 Dec 2018 23:43:37 +0530 Subject: [PATCH] Bug fixes and added redis sentinel supportability changes. --- README.md | 2 +- .../java/tomcat/request/session/Session.java | 9 +++---- .../request/session/redis/SessionManager.java | 25 +++++++------------ 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index ffb965d..d7ea3bd 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Going forward, we no need to enable sticky session (JSESSIONID) in Load balancer - Apache Tomcat 9 ## Downloads: - - [latest version (2.0.5)](https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/2.0.5) + - [latest version (3.0)](https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/3.0) - [older versions](https://github.com/ran-jit/tomcat-cluster-redis-session-manager/wiki) #### Pre-requisite: diff --git a/src/main/java/tomcat/request/session/Session.java b/src/main/java/tomcat/request/session/Session.java index cd6f7bb..2ccb873 100644 --- a/src/main/java/tomcat/request/session/Session.java +++ b/src/main/java/tomcat/request/session/Session.java @@ -73,10 +73,8 @@ public class Session extends StandardSession { super.setAttribute(key, value); if ((value != null || oldValue != null) - && (value == null && oldValue != null || oldValue == null && value != null - || !value.getClass().isInstance(oldValue) || !value.equals(oldValue))) { - if (this.manager instanceof SessionManager && ((SessionManager) this.manager) - .getSaveOnChange()) { + && (value == null && oldValue != null || oldValue == null && value != null || !value.getClass().isInstance(oldValue) || !value.equals(oldValue))) { + if (this.manager instanceof SessionManager && ((SessionManager) this.manager).getSaveOnChange()) { ((SessionManager) this.manager).save(this, true); } else { this.changedAttributes.put(key, value); @@ -100,8 +98,7 @@ public class Session extends StandardSession { @Override public void removeAttribute(String name) { super.removeAttribute(name); - if (this.manager instanceof SessionManager && ((SessionManager) this.manager) - .getSaveOnChange()) { + if (this.manager instanceof SessionManager && ((SessionManager) this.manager).getSaveOnChange()) { ((SessionManager) this.manager).save(this, true); } else { this.dirty = true; diff --git a/src/main/java/tomcat/request/session/redis/SessionManager.java b/src/main/java/tomcat/request/session/redis/SessionManager.java index de71715..83c7ef4 100644 --- a/src/main/java/tomcat/request/session/redis/SessionManager.java +++ b/src/main/java/tomcat/request/session/redis/SessionManager.java @@ -121,8 +121,7 @@ public class SessionManager extends ManagerBase implements Lifecycle { @Override public Session createSession(String sessionId) { if (sessionId != null) { - sessionId = - (this.dataCache.setnx(sessionId, SessionConstants.NULL_SESSION) == 0L) ? null : sessionId; + sessionId = (this.dataCache.setnx(sessionId, SessionConstants.NULL_SESSION) == 0L) ? null : sessionId; } else { do { sessionId = generateSessionId(); @@ -168,8 +167,7 @@ public class SessionManager extends ManagerBase implements Lifecycle { @Override public Session findSession(String sessionId) throws IOException { Session session = null; - if (sessionId != null && this.sessionContext.get() != null && sessionId - .equals(this.sessionContext.get().getId())) { + if (sessionId != null && this.sessionContext.get() != null && sessionId.equals(this.sessionContext.get().getId())) { session = this.sessionContext.get().getSession(); } else { byte[] data = this.dataCache.get(sessionId); @@ -234,7 +232,6 @@ public class SessionManager extends ManagerBase implements Lifecycle { private void initialize() { try { this.dataCache = new RedisCache(); - this.serializer = new SerializationUtil(); Context context = getContextIns(); ClassLoader loader = (context != null && context.getLoader() != null) ? context.getLoader().getClassLoader() : null; @@ -254,24 +251,22 @@ public class SessionManager extends ManagerBase implements Lifecycle { ? this.sessionContext.get().getMetadata().getAttributesHash() : null; byte[] currentHash = serializer.getSessionAttributesHashCode(newSession); - if (forceSave || newSession.isDirty() - || (isPersisted = - (this.sessionContext.get() != null) ? this.sessionContext.get().isPersisted() : null) - == null + if (forceSave + || newSession.isDirty() + || (isPersisted = (this.sessionContext.get() != null) ? this.sessionContext.get().isPersisted() : null) == null || !isPersisted || !Arrays.equals(hash, currentHash)) { SessionMetadata metadata = new SessionMetadata(); metadata.setAttributesHash(currentHash); - this.dataCache - .set(newSession.getId(), serializer.serializeSessionData(newSession, metadata)); + this.dataCache.set(newSession.getId(), serializer.serializeSessionData(newSession, metadata)); newSession.resetDirtyTracking(); setValues(true, metadata); } int timeout = getSessionTimeout(newSession); this.dataCache.expire(newSession.getId(), timeout); - LOGGER.trace("Session [" + newSession.getId() + "] expire in [" + timeout + "] seconds."); + LOGGER.debug("Session [" + newSession.getId() + "] expire in [" + timeout + "] seconds."); } catch (IOException ex) { LOGGER.error("Error occurred while saving the session object in data cache..", ex); @@ -289,15 +284,13 @@ public class SessionManager extends ManagerBase implements Lifecycle { } else { remove(session); } - LOGGER.trace( - "Session object " + (session.isValid() ? "saved: " : "removed: ") + session.getId()); + LOGGER.debug("Session object " + (session.isValid() ? "saved: " : "removed: ") + session.getId()); } } catch (Exception ex) { LOGGER.error("Error occurred while processing post request process..", ex); } finally { this.sessionContext.remove(); - LOGGER.trace( - "Session removed from ThreadLocal:" + ((session != null) ? session.getIdInternal() : "")); + LOGGER.debug("Session removed from ThreadLocal:" + ((session != null) ? session.getIdInternal() : "")); } }