Skip to content

Update example to Hazelcast 5.x and Spring Boot 3.x #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions docs/modules/ROOT/pages/spring-session-hazelcast.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
====
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -221,7 +221,7 @@ Annotation::
class SessionConfiguration {

@Bean
public SessionRepositoryCustomizer<Hazelcast4IndexedSessionRepository> customize() {
public SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> customize() {
return (sessionRepository) -> {
sessionRepository.setFlushMode(FlushMode.IMMEDIATE);
sessionRepository.setSaveMode(SaveMode.ALWAYS);
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<version>3.2.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand All @@ -16,7 +16,7 @@
<description>Session Replication with Spring Session Hazelcast</description>

<properties>
<hazelcast.version>4.2.2</hazelcast.version>
<hazelcast.version>5.3.8</hazelcast.version>
</properties>

<dependencies>
Expand Down
31 changes: 14 additions & 17 deletions src/main/java/com/hazelcast/guide/config/SessionConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
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 {

private final String SESSIONS_MAP_NAME = "spring-session-map-name";

@Bean
public SessionRepositoryCustomizer<Hazelcast4IndexedSessionRepository> customize() {
public SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> customize() {
return (sessionRepository) -> {
sessionRepository.setFlushMode(FlushMode.IMMEDIATE);
sessionRepository.setSaveMode(SaveMode.ALWAYS);
Expand All @@ -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
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/hazelcast/guide/controller/SessionController.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
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.FindByIndexNameSessionRepository;
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;
FindByIndexNameSessionRepository<?> sessionRepository;

/**
* Creates a session for the request if there is no session of the request.
Expand Down