From 47becf1ee5c933e8f951129730cae9a58c3441f9 Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 24 Jan 2018 09:46:43 +0300 Subject: [PATCH] Spring Session updated to 2.0.0 #1261 --- redisson/pom.xml | 4 +- .../session/RedissonSessionRepository.java | 66 ++++++++++--------- .../session/RedissonSessionManagerTest.java | 2 + 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/redisson/pom.xml b/redisson/pom.xml index 21353ce38..56962fe25 100644 --- a/redisson/pom.xml +++ b/redisson/pom.xml @@ -258,8 +258,8 @@ org.springframework.session - spring-session - [1.2.2,) + spring-session-core + [2.0.0,) provided true diff --git a/redisson/src/main/java/org/redisson/spring/session/RedissonSessionRepository.java b/redisson/src/main/java/org/redisson/spring/session/RedissonSessionRepository.java index 769e1bc24..451b469b5 100644 --- a/redisson/src/main/java/org/redisson/spring/session/RedissonSessionRepository.java +++ b/redisson/src/main/java/org/redisson/spring/session/RedissonSessionRepository.java @@ -15,6 +15,8 @@ */ package org.redisson.spring.session; +import java.time.Duration; +import java.time.Instant; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -35,7 +37,6 @@ import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.session.ExpiringSession; import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.MapSession; import org.springframework.session.Session; @@ -51,7 +52,7 @@ import org.springframework.session.events.SessionExpiredEvent; public class RedissonSessionRepository implements FindByIndexNameSessionRepository, PatternMessageListener { - final class RedissonSession implements ExpiringSession { + final class RedissonSession implements Session { private String principalName; private final MapSession delegate; @@ -63,9 +64,9 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito principalName = resolvePrincipal(delegate); Map newMap = new HashMap(3); - newMap.put("session:creationTime", delegate.getCreationTime()); - newMap.put("session:lastAccessedTime", delegate.getLastAccessedTime()); - newMap.put("session:maxInactiveInterval", delegate.getMaxInactiveIntervalInSeconds()); + newMap.put("session:creationTime", delegate.getCreationTime().toEpochMilli()); + newMap.put("session:lastAccessedTime", delegate.getLastAccessedTime().toEpochMilli()); + newMap.put("session:maxInactiveInterval", delegate.getMaxInactiveInterval().getSeconds()); map.putAll(newMap); updateExpiration(); @@ -76,8 +77,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito } private void updateExpiration() { - if (delegate.getMaxInactiveIntervalInSeconds() >= 0) { - map.expire(delegate.getMaxInactiveIntervalInSeconds(), TimeUnit.SECONDS); + if (delegate.getMaxInactiveInterval().getSeconds() >= 0) { + map.expire(delegate.getMaxInactiveInterval().getSeconds(), TimeUnit.SECONDS); } } @@ -95,11 +96,11 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito Set> entrySet = map.readAllEntrySet(); for (Entry entry : entrySet) { if ("session:creationTime".equals(entry.getKey())) { - delegate.setCreationTime((Long) entry.getValue()); + delegate.setCreationTime(Instant.ofEpochMilli((Long) entry.getValue())); } else if ("session:lastAccessedTime".equals(entry.getKey())) { - delegate.setLastAccessedTime((Long) entry.getValue()); + delegate.setLastAccessedTime(Instant.ofEpochMilli((Long) entry.getValue())); } else if ("session:maxInactiveInterval".equals(entry.getKey())) { - delegate.setMaxInactiveIntervalInSeconds((Integer) entry.getValue()); + delegate.setMaxInactiveInterval(Duration.ofSeconds((Long) entry.getValue())); } else { delegate.setAttribute(entry.getKey(), entry.getValue()); } @@ -172,45 +173,50 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito } @Override - public long getCreationTime() { + public Instant getCreationTime() { return delegate.getCreationTime(); } @Override - public void setLastAccessedTime(long lastAccessedTime) { + public void setLastAccessedTime(Instant lastAccessedTime) { delegate.setLastAccessedTime(lastAccessedTime); if (map != null) { - map.fastPut("session:lastAccessedTime", lastAccessedTime); + map.fastPut("session:lastAccessedTime", lastAccessedTime.toEpochMilli()); updateExpiration(); } } @Override - public long getLastAccessedTime() { + public Instant getLastAccessedTime() { return delegate.getLastAccessedTime(); } @Override - public void setMaxInactiveIntervalInSeconds(int interval) { - delegate.setMaxInactiveIntervalInSeconds(interval); + public void setMaxInactiveInterval(Duration interval) { + delegate.setMaxInactiveInterval(interval); if (map != null) { - map.fastPut("session:maxInactiveInterval", interval); + map.fastPut("session:maxInactiveInterval", interval.getSeconds()); updateExpiration(); } } @Override - public int getMaxInactiveIntervalInSeconds() { - return delegate.getMaxInactiveIntervalInSeconds(); + public Duration getMaxInactiveInterval() { + return delegate.getMaxInactiveInterval(); } @Override public boolean isExpired() { return delegate.isExpired(); } - + + @Override + public String changeSessionId() { + return delegate.changeSessionId(); + } + } private static final Logger log = LoggerFactory.getLogger(RedissonSessionRepository.class); @@ -243,7 +249,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito @Override public void onMessage(String pattern, String channel, String body) { if (createdTopic.getPatternNames().contains(pattern)) { - RedissonSession session = getSession(body); + RedissonSession session = findById(body); if (session != null) { publishEvent(new SessionCreatedEvent(this, session)); } @@ -256,10 +262,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito RedissonSession session = new RedissonSession(id); if (session.load()) { session.clearPrincipal(); - publishEvent(new SessionDeletedEvent(this, session)); - } else { - publishEvent(new SessionDeletedEvent(this, id)); } + publishEvent(new SessionDeletedEvent(this, session)); } else if (expiredTopic.getPatternNames().contains(pattern)) { if (!body.contains(":")) { return; @@ -269,10 +273,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito RedissonSession session = new RedissonSession(id); if (session.load()) { session.clearPrincipal(); - publishEvent(new SessionExpiredEvent(this, session)); - } else { - publishEvent(new SessionExpiredEvent(this, id)); } + publishEvent(new SessionExpiredEvent(this, session)); } } @@ -292,7 +294,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito public RedissonSession createSession() { RedissonSession session = new RedissonSession(); if (defaultMaxInactiveInterval != null) { - session.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval); + session.setMaxInactiveInterval(Duration.ofSeconds(defaultMaxInactiveInterval)); } return session; } @@ -303,7 +305,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito } @Override - public RedissonSession getSession(String id) { + public RedissonSession findById(String id) { RedissonSession session = new RedissonSession(id); if (!session.load() || session.isExpired()) { return null; @@ -312,8 +314,8 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito } @Override - public void delete(String id) { - RedissonSession session = getSession(id); + public void deleteById(String id) { + RedissonSession session = findById(id); if (session == null) { return; } @@ -368,7 +370,7 @@ public class RedissonSessionRepository implements FindByIndexNameSessionReposito Set sessionIds = set.readAll(); Map result = new HashMap(); for (String id : sessionIds) { - RedissonSession session = getSession(id); + RedissonSession session = findById(id); if (session != null) { result.put(id, session); } diff --git a/redisson/src/test/java/org/redisson/spring/session/RedissonSessionManagerTest.java b/redisson/src/test/java/org/redisson/spring/session/RedissonSessionManagerTest.java index 9f217cf2a..4bacc53c9 100644 --- a/redisson/src/test/java/org/redisson/spring/session/RedissonSessionManagerTest.java +++ b/redisson/src/test/java/org/redisson/spring/session/RedissonSessionManagerTest.java @@ -186,6 +186,8 @@ public class RedissonSessionManagerTest { write(executor, "test", "1234"); Cookie cookie = cookieStore.getCookies().get(0); + Thread.sleep(50); + Assert.assertEquals(1, listener.getSessionCreatedEvents()); Assert.assertEquals(0, listener.getSessionDeletedEvents());