From 2568a09e4fdd5d52263d0feb1b37ae31f5919de6 Mon Sep 17 00:00:00 2001 From: Andrea Del Bene Date: Tue, 23 Jul 2024 10:45:11 +0200 Subject: [PATCH 1/3] code migrated to Hazelcast 5.x and Spring Boot 3.x --- pom.xml | 4 +-- .../guide/config/SessionConfiguration.java | 31 +++++++++---------- .../guide/controller/SessionController.java | 23 +++++++------- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 61701d9..5cd7f0a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.5.4 + 3.2.8 @@ -16,7 +16,7 @@ Session Replication with Spring Session Hazelcast - 4.2.2 + 5.3.8 diff --git a/src/main/java/com/hazelcast/guide/config/SessionConfiguration.java b/src/main/java/com/hazelcast/guide/config/SessionConfiguration.java index 5fe7b10..0a83d7e 100644 --- a/src/main/java/com/hazelcast/guide/config/SessionConfiguration.java +++ b/src/main/java/com/hazelcast/guide/config/SessionConfiguration.java @@ -1,28 +1,25 @@ package com.hazelcast.guide.config; -import com.hazelcast.client.HazelcastClient; -import com.hazelcast.client.config.ClientConfig; -import com.hazelcast.config.AttributeConfig; -import com.hazelcast.config.Config; -import com.hazelcast.config.IndexConfig; -import com.hazelcast.config.IndexType; -import com.hazelcast.config.SerializerConfig; -import com.hazelcast.core.Hazelcast; -import com.hazelcast.core.HazelcastInstance; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.session.FlushMode; import org.springframework.session.MapSession; import org.springframework.session.SaveMode; -import org.springframework.session.Session; import org.springframework.session.config.SessionRepositoryCustomizer; -import org.springframework.session.hazelcast.Hazelcast4IndexedSessionRepository; -import org.springframework.session.hazelcast.Hazelcast4PrincipalNameExtractor; -import org.springframework.session.hazelcast.Hazelcast4SessionUpdateEntryProcessor; +import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; import org.springframework.session.hazelcast.HazelcastSessionSerializer; +import org.springframework.session.hazelcast.PrincipalNameExtractor; import org.springframework.session.hazelcast.config.annotation.SpringSessionHazelcastInstance; import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession; +import com.hazelcast.config.AttributeConfig; +import com.hazelcast.config.Config; +import com.hazelcast.config.IndexConfig; +import com.hazelcast.config.IndexType; +import com.hazelcast.config.SerializerConfig; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; + @Configuration @EnableHazelcastHttpSession class SessionConfiguration { @@ -30,7 +27,7 @@ class SessionConfiguration { private final String SESSIONS_MAP_NAME = "spring-session-map-name"; @Bean - public SessionRepositoryCustomizer customize() { + public SessionRepositoryCustomizer customize() { return (sessionRepository) -> { sessionRepository.setFlushMode(FlushMode.IMMEDIATE); sessionRepository.setSaveMode(SaveMode.ALWAYS); @@ -47,13 +44,13 @@ public HazelcastInstance hazelcastInstance() { // Add this attribute to be able to query sessions by their PRINCIPAL_NAME_ATTRIBUTE's AttributeConfig attributeConfig = new AttributeConfig() - .setName(Hazelcast4IndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) - .setExtractorClassName(Hazelcast4PrincipalNameExtractor.class.getName()); + .setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) + .setExtractorClassName(PrincipalNameExtractor.class.getName()); // Configure the sessions map config.getMapConfig(SESSIONS_MAP_NAME) .addAttributeConfig(attributeConfig).addIndexConfig( - new IndexConfig(IndexType.HASH, Hazelcast4IndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)); + new IndexConfig(IndexType.HASH, HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)); // Use custom serializer to de/serialize sessions faster. This is optional. // Note that, all members in a cluster and connected clients need to use the diff --git a/src/main/java/com/hazelcast/guide/controller/SessionController.java b/src/main/java/com/hazelcast/guide/controller/SessionController.java index d71209c..eb530de 100644 --- a/src/main/java/com/hazelcast/guide/controller/SessionController.java +++ b/src/main/java/com/hazelcast/guide/controller/SessionController.java @@ -1,30 +1,31 @@ package com.hazelcast.guide.controller; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.stream.Collectors; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.session.Session; -import org.springframework.session.hazelcast.Hazelcast4IndexedSessionRepository; +import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.stream.Collectors; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; @RestController public class SessionController { - private static final String principalIndexName = Hazelcast4IndexedSessionRepository.PRINCIPAL_NAME_INDEX_NAME; + private static final String principalIndexName = HazelcastIndexedSessionRepository.PRINCIPAL_NAME_INDEX_NAME; private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); @Autowired - Hazelcast4IndexedSessionRepository sessionRepository; + HazelcastIndexedSessionRepository sessionRepository; /** * Creates a session for the request if there is no session of the request. From 8660ad653c9a79f6d7d07eef9ea6bb8867ac0428 Mon Sep 17 00:00:00 2001 From: Andrea Del Bene Date: Tue, 23 Jul 2024 11:26:23 +0200 Subject: [PATCH 2/3] fixed dependency problem --- .../java/com/hazelcast/guide/controller/SessionController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/hazelcast/guide/controller/SessionController.java b/src/main/java/com/hazelcast/guide/controller/SessionController.java index eb530de..dd5a254 100644 --- a/src/main/java/com/hazelcast/guide/controller/SessionController.java +++ b/src/main/java/com/hazelcast/guide/controller/SessionController.java @@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; +import org.springframework.session.FindByIndexNameSessionRepository; import org.springframework.session.Session; import org.springframework.session.hazelcast.HazelcastIndexedSessionRepository; import org.springframework.web.bind.annotation.GetMapping; @@ -25,7 +26,7 @@ public class SessionController { private static final DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); @Autowired - HazelcastIndexedSessionRepository sessionRepository; + FindByIndexNameSessionRepository sessionRepository; /** * Creates a session for the request if there is no session of the request. From 01c9da36f3c4f5f4d9fee84924dc759af55effaa Mon Sep 17 00:00:00 2001 From: Andrea Del Bene Date: Tue, 23 Jul 2024 11:39:38 +0200 Subject: [PATCH 3/3] Doc updated to Hazelcast 5.3.* and Spring Boot 3.2.x --- .../ROOT/pages/spring-session-hazelcast.adoc | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/modules/ROOT/pages/spring-session-hazelcast.adoc b/docs/modules/ROOT/pages/spring-session-hazelcast.adoc index 0414f01..2a9fc12 100644 --- a/docs/modules/ROOT/pages/spring-session-hazelcast.adoc +++ b/docs/modules/ROOT/pages/spring-session-hazelcast.adoc @@ -18,8 +18,8 @@ https://docs.spring.io/spring-session/docs/current/reference/html5/#introduction == Before you Begin -- JDK 1.8+ -- Apache Maven 3.2+ +- JDK 17+ +- Apache Maven 3.6+ == Enable HazelcastHttpSession @@ -76,9 +76,6 @@ After enabling Hazelcast HTTP Session, you need to provide a Hazelcast instance the session repository. This instance can be either a Hazelcast client or an embedded Hazelcast instance. You can see the details of client and embedded modes https://docs.hazelcast.org/docs/latest/manual/html-single/#hazelcast-topology[here] on the documentation. -Notice some `Hazelcast4` prefixes in the class names. If you use an older Hazelcast version (3.x), you -need to drop these `"4"` s. For instance, use `HazelcastIndexedSessionRepository` instead of -`Hazelcast4IndexedSessionRepository`: [tabs] ==== @@ -97,15 +94,18 @@ public HazelcastInstance hazelcastInstance() { // Add this attribute to be able to query sessions by their PRINCIPAL_NAME_ATTRIBUTE's AttributeConfig attributeConfig = new AttributeConfig() - .setName(Hazelcast4IndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) - .setExtractorClassName(Hazelcast4PrincipalNameExtractor.class.getName()); + .setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE) + .setExtractorClassName(PrincipalNameExtractor.class.getName()); // Configure the sessions map config.getMapConfig(SESSIONS_MAP_NAME) - .addAttributeConfig(attributeConfig).addIndexConfig( - new IndexConfig(IndexType.HASH, Hazelcast4IndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)); + .addAttributeConfig(attributeConfig).addIndexConfig( + new IndexConfig(IndexType.HASH, HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)); // Use custom serializer to de/serialize sessions faster. This is optional. + // Note that, all members in a cluster and connected clients need to use the + // same serializer for sessions. For instance, clients cannot use this serializer + // where members are not configured to do so. SerializerConfig serializerConfig = new SerializerConfig(); serializerConfig.setImplementation(new HazelcastSessionSerializer()).setTypeClass(MapSession.class); config.getSerializationConfig().addSerializerConfig(serializerConfig); @@ -134,7 +134,7 @@ public HazelcastInstance hazelcastInstance() { // these classes need to be deployed over the client. This is required since // Hazelcast updates sessions via entry processors. clientConfig.getUserCodeDeploymentConfig().setEnabled(true).addClass(Session.class) - .addClass(MapSession.class).addClass(Hazelcast4SessionUpdateEntryProcessor.class); + .addClass(MapSession.class).addClass(SessionUpdateEntryProcessor.class); return HazelcastClient.newHazelcastClient(clientConfig); } @@ -221,7 +221,7 @@ Annotation:: class SessionConfiguration { @Bean - public SessionRepositoryCustomizer customize() { + public SessionRepositoryCustomizer customize() { return (sessionRepository) -> { sessionRepository.setFlushMode(FlushMode.IMMEDIATE); sessionRepository.setSaveMode(SaveMode.ALWAYS); @@ -264,7 +264,7 @@ Once you completed the configurations, you can reach to the session repository b [source, java] ---- @Autowired -Hazelcast4IndexedSessionRepository sessionRepository; +FindByIndexNameSessionRepository sessionRepository; ---- Although you do not need to reach this repository explicitly to store or load sessions, some of the methods might be