Skip to content

Commit 326290b

Browse files
committed
Merge pull request #10351 from vpavic:improve-session-sample
* pr/10351: Polish "Improve Spring Session sample" Improve Spring Session sample
2 parents 17a349b + efc3188 commit 326290b

File tree

10 files changed

+176
-87
lines changed

10 files changed

+176
-87
lines changed

spring-boot-samples/README.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ The following sample applications are provided:
158158
| link:spring-boot-sample-servlet[spring-boot-sample-servlet]
159159
| Web application with a "raw" `Servlet` returning plain text content
160160

161-
| link:spring-boot-sample-session-redis[spring-boot-sample-session-redis]
162-
| Web Application that uses Spring Session to store session data in Redis
161+
| link:spring-boot-sample-session[spring-boot-sample-session]
162+
| Web Application that uses Spring Session to manage session data
163163

164164
| link:spring-boot-sample-simple[spring-boot-sample-simple]
165165
| Simple command line application

spring-boot-samples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<module>spring-boot-sample-secure-oauth2-actuator</module>
7272
<module>spring-boot-sample-secure-oauth2-resource</module>
7373
<module>spring-boot-sample-servlet</module>
74-
<module>spring-boot-sample-session-redis</module>
74+
<module>spring-boot-sample-session</module>
7575
<module>spring-boot-sample-simple</module>
7676
<module>spring-boot-sample-test</module>
7777
<module>spring-boot-sample-test-nomockito</module>

spring-boot-samples/spring-boot-sample-session-redis/pom.xml

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
= Spring Boot Spring Session Sample
2+
3+
This sample demonstrates the Spring Session auto-configuration support. Spring Session
4+
supports multiple session store types, including:
5+
6+
* `Redis`
7+
* `JDBC`
8+
* `Hazelcast`
9+
10+
11+
12+
== Using a different session store
13+
Initially, the project uses JDBC session store backed by an in-memory embedded H2
14+
database. You can try out your favorite session store as explained below.
15+
16+
17+
18+
=== Redis
19+
Add `org.springframework.session:spring-session-data-redis` and
20+
`spring-boot-starter-data-redis` dependencies to the project and make sure it is
21+
configured properly (by default, a Redis instance with the default settings is expected
22+
on your local box).
23+
24+
TIP: Run sample application using Redis session store using
25+
`$mvn spring-boot:run -Predis`.
26+
27+
28+
29+
=== JDBC
30+
Add `org.springframework.session:spring-session-jdbc`,
31+
`org.springframework.boot:spring-boot-starter-jdbc` and `com.h2database:h2` dependencies
32+
to the project. An in-memory embedded H2 database is automatically configured.
33+
34+
TIP: Run sample application using JDBC session store with
35+
`$mvn spring-boot:run -Pjdbc`.
36+
37+
Note that this profile is active by default.
38+
39+
40+
41+
=== Hazelcast
42+
Add `org.springframework.session:spring-session-hazelcast` and `com.hazelcast:hazelcast`
43+
dependencies to the project to enable support for Hazelcast. Since there is a default
44+
`hazelcast.xml` configuration file at the root of the classpath, it is used to
45+
automatically configure the underlying `HazelcastInstance`.
46+
47+
TIP: Run sample application using Hazelcast session store with
48+
`$mvn spring-boot:run -Phazelcast`.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<!-- Your own application should inherit from spring-boot-starter-parent -->
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-samples</artifactId>
8+
<version>2.0.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>spring-boot-sample-session</artifactId>
11+
<name>Spring Boot Session Sample</name>
12+
<description>Spring Boot Session Sample</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/../..</main.basedir>
20+
</properties>
21+
<dependencies>
22+
<!-- Compile -->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
<!-- Test -->
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-maven-plugin</artifactId>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
<profiles>
43+
<profile>
44+
<id>redis</id>
45+
<dependencies>
46+
<dependency>
47+
<groupId>org.springframework.session</groupId>
48+
<artifactId>spring-session-data-redis</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-data-redis</artifactId>
53+
</dependency>
54+
</dependencies>
55+
</profile>
56+
<profile>
57+
<id>jdbc</id>
58+
<activation>
59+
<activeByDefault>true</activeByDefault>
60+
</activation>
61+
<dependencies>
62+
<dependency>
63+
<groupId>org.springframework.session</groupId>
64+
<artifactId>spring-session-jdbc</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-starter-jdbc</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.h2database</groupId>
72+
<artifactId>h2</artifactId>
73+
</dependency>
74+
</dependencies>
75+
</profile>
76+
<profile>
77+
<id>hazelcast</id>
78+
<dependencies>
79+
<dependency>
80+
<groupId>org.springframework.session</groupId>
81+
<artifactId>spring-session-hazelcast</artifactId>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.hazelcast</groupId>
85+
<artifactId>hazelcast</artifactId>
86+
</dependency>
87+
</dependencies>
88+
</profile>
89+
</profiles>
90+
</project>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sample.session.redis;
17+
package sample.session;
1818

1919
import java.util.UUID;
2020

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,16 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sample.session.redis;
17+
package sample.session;
1818

1919
import org.springframework.boot.SpringApplication;
2020
import org.springframework.boot.autoconfigure.SpringBootApplication;
2121

2222
@SpringBootApplication
23-
public class SampleSessionRedisApplication {
23+
public class SampleSessionApplication {
2424

2525
public static void main(String[] args) throws Exception {
26-
SpringApplication.run(SampleSessionRedisApplication.class);
26+
SpringApplication.run(SampleSessionApplication.class);
2727
}
2828

2929
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.8.xsd"
2+
xmlns="http://www.hazelcast.com/schema/config"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<map name="spring:session:sessions">
6+
<attributes>
7+
<attribute extractor="org.springframework.session.hazelcast.PrincipalNameExtractor">principalName</attribute>
8+
</attributes>
9+
<indexes>
10+
<index>principalName</index>
11+
</indexes>
12+
</map>
13+
14+
<network>
15+
<join>
16+
<multicast enabled="false"/>
17+
</join>
18+
</network>
19+
20+
</hazelcast>
Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package sample.session.redis;
17+
package sample.session;
1818

1919
import java.net.URI;
2020

@@ -23,7 +23,6 @@
2323
import org.springframework.boot.builder.SpringApplicationBuilder;
2424
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
2525
import org.springframework.context.ConfigurableApplicationContext;
26-
import org.springframework.data.redis.RedisConnectionFailureException;
2726
import org.springframework.http.HttpHeaders;
2827
import org.springframework.http.HttpMethod;
2928
import org.springframework.http.RequestEntity;
@@ -33,31 +32,20 @@
3332
import static org.assertj.core.api.Assertions.assertThat;
3433

3534
/**
36-
* Tests for {@link SampleSessionRedisApplication}.
35+
* Tests for {@link SampleSessionApplication}.
3736
*
3837
* @author Andy Wilkinson
3938
*/
40-
public class SampleSessionRedisApplicationTests {
39+
public class SampleSessionApplicationTests {
4140

4241
@Test
4342
public void sessionExpiry() throws Exception {
44-
45-
String port = null;
46-
47-
try {
48-
ConfigurableApplicationContext context = new SpringApplicationBuilder()
49-
.sources(SampleSessionRedisApplication.class)
50-
.properties("server.port:0")
51-
.initializers(new ServerPortInfoApplicationContextInitializer())
52-
.run();
53-
port = context.getEnvironment().getProperty("local.server.port");
54-
}
55-
catch (RuntimeException ex) {
56-
if (!redisServerRunning(ex)) {
57-
return;
58-
}
59-
throw ex;
60-
}
43+
ConfigurableApplicationContext context = new SpringApplicationBuilder()
44+
.sources(SampleSessionApplication.class)
45+
.properties("server.port:0")
46+
.initializers(new ServerPortInfoApplicationContextInitializer())
47+
.run();
48+
String port = context.getEnvironment().getProperty("local.server.port");
6149

6250
URI uri = URI.create("http://localhost:" + port + "/");
6351
RestTemplate restTemplate = new RestTemplate();
@@ -79,11 +67,4 @@ public void sessionExpiry() throws Exception {
7967
assertThat(uuid2).isNotEqualTo(uuid3);
8068
}
8169

82-
private boolean redisServerRunning(Throwable ex) {
83-
if (ex instanceof RedisConnectionFailureException) {
84-
return false;
85-
}
86-
return (ex.getCause() == null || redisServerRunning(ex.getCause()));
87-
}
88-
8970
}

0 commit comments

Comments
 (0)