This guide describes how to configure Apache Geode as a provider in Spring Session to transparently manage
a Web application’s javax.servlet.http.HttpSession
using Java configuration.
The completed guide can be found in the HttpSession with Apache Geode (P2P) Sample Application. |
1. Updating Dependencies
Before using Spring Session, you must ensure that the required dependencies are included.
If you are using Maven, include the following dependencies
in your pom.xml
:
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-geode</artifactId>
<version>3.0.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-web</artifactId>
<version>6.0.13</version>
</dependency>
</dependencies>
Since we are using a SNAPSHOT version, we need to add the Spring Snapshot Maven Repository.
If you are using Maven, include the following repository
declaration in your pom.xml
:
<repositories>
<!-- ... -->
<repository>
<id>spring-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
</repository>
</repositories>
2. Spring Java Configuration
After adding the required dependencies and repository declarations, we can create the Spring configuration.
The Spring configuration is responsible for creating a Servlet
Filter
that replaces the
javax.servlet.http.HttpSession
with an implementation backed by Spring Session and Apache Geode.
Add the following Spring configuration:
@PeerCacheApplication(name = "SpringSessionDataGeodeJavaConfigP2pSample", logLevel = "error") (1)
@EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 30) (2)
public class Config {
}
1 | First, we use the @PeerCacheApplication annotation to simplify the creation of a peer cache instance. |
2 | Then, the Config class is annotated with @EnableGemFireHttpSession to create the necessary server-side Region
(by default, "ClusteredSpringSessions") used to store HttpSession state. |
For more information on configuring Spring Data for Apache Geode, refer to the Reference Guide. |
The @EnableGemFireHttpSession
annotation enables developers to configure certain aspects of both Spring Session
and Apache Geode out-of-the-box using the following attributes:
-
clientRegionShortcut
- specifies Apache Geode data management policy on the client with the ClientRegionShortcut (default isPROXY
). This attribute is only used when configuring the clientRegion
. -
indexableSessionAttributes
- Identifies the Session attributes by name that should be indexed for querying purposes. Only Session attributes explicitly identified by name will be indexed. -
maxInactiveIntervalInSeconds
- controls HttpSession idle-timeout expiration (defaults to 30 minutes). -
poolName
- name of the dedicated Apache GeodePool
used to connect a client to the cluster of servers. This attribute is only used when the application is a cache client. Defaults togemfirePool
. -
regionName
- specifies the name of the Apache GeodeRegion
used to store and manageHttpSession
state (default is "ClusteredSpringSessions"). -
serverRegionShortcut
- specifies Apache Geode data management policy on the server with the RegionShortcut (default isPARTITION
). This attribute is only used when configuring serverRegions
, or when a P2P topology is employed.
3. Java Servlet Container Initialization
Our <<[httpsession-spring-java-configuration-p2p,Spring Java Configuration>> created a Spring bean named
springSessionRepositoryFilter
that implements javax.servlet.Filter
. The springSessionRepositoryFilter
bean
is responsible for replacing the javax.servlet.http.HttpSession
with a custom implementation backed by
Spring Session and Apache Geode.
In order for our Filter
to do its magic, Spring needs to load our Config
class. We also need to ensure our
Servlet container (i.e. Tomcat) uses our springSessionRepositoryFilter
on every HTTP request.
Fortunately, Spring Session provides a utility class named AbstractHttpSessionApplicationInitializer
to make both
steps extremely easy.
You can find an example below:
public class Initializer extends AbstractHttpSessionApplicationInitializer { (1)
public Initializer() {
super(Config.class); (2)
}
}
The name of our class (Initializer ) does not matter. What is important is that we extend
AbstractHttpSessionApplicationInitializer .
|
1 | The first step is to extend AbstractHttpSessionApplicationInitializer . This ensures that a Spring bean named
springSessionRepositoryFilter is registered with our Servlet container and used on every HTTP request. |
2 | AbstractHttpSessionApplicationInitializer also provides a mechanism to easily allow Spring to load
our Config class. |
4. HttpSession with Apache Geode (P2P) Sample Application
4.1. Running the Apache Geode P2P Java Sample Application
You can run the sample by obtaining the source code and invoking the following command:
$ ./gradlew :spring-session-sample-javaconfig-gemfire-p2p:tomcatRun
You should now be able to access the application at http://localhost:8080/.
4.2. Exploring the Apache Geode P2P Java Sample Application
Try using the application. Fill out the form with the following information:
-
Attribute Name: username
-
Attribute Value: john
Now click the Set Attribute button. You should now see the values displayed in the table.
4.3. How does it work?
We interact with the standard HttpSession
in the SessionServlet
shown below:
@WebServlet("/session")
public class SessionServlet extends HttpServlet {
private static final long serialVersionUID = 2878267318695777395L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String attributeName = request.getParameter("attributeName");
String attributeValue = request.getParameter("attributeValue");
request.getSession().setAttribute(attributeName, attributeValue);
response.sendRedirect(request.getContextPath() + "/");
}
}
Instead of using Tomcat’s HttpSession
, we are actually persisting the Session in Apache Geode.