forked from spring-projects/spring-session
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve GH PR spring-projects#148 and PR spring-projects#308 implemen…
…ting a GemFire Adapter to support clustered HttpSessions using Spring Session. * Resolve SGF-373 - Implement a Spring Session Adapter for GemFire backing a HttpSession similar to the Redis support. * Add Spring Session annotation to enable GemFire support with @EnableGemFireHttpSession. * Add extesion of SpringHttpSessionConfiguration to configure GemFire using GemFireHttpSessionConfiguration. * Add implementation of SessionRepository to access clustered, replicated HttpSession state in GemFire with GemFireOperationsSessionRepository. * Utilize GemFire Data Serialization framework to both replicate HttpSession state information as well as handle deltas. * Utilize GemFire OQL query to lookup arbitrary Session attributes by name, and in particular the user authenticated principal name. * Implment unit and integration tests, and in particular, tests for both peer-to-peer (p2p) and client/server topologies. * Set initial Spring Data GemFire version to 1.7.2.RELEASE, which depends on Pivotal GemFire 8.1.0. * Add documentation, Javadoc and samples along with additional Integration Tests.
- Loading branch information
Showing
68 changed files
with
8,601 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
272 changes: 272 additions & 0 deletions
272
docs/src/docs/asciidoc/guides/httpsession-gemfire-clientserver-xml.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
= Spring Session - HttpSession with GemFire Client/Server using XML (Quick Start) | ||
John Blum | ||
:toc: | ||
|
||
This guide describes how to configure Spring Session to transparently leverage Pivotal GemFire to back a web application's | ||
`HttpSession` using XML Configuration. | ||
|
||
NOTE: The completed guide can be found in the <<httpsession-gemfire-clientserver-xml-sample-app,HttpSession with GemFire (Client/Server) using XML Sample Application>>. | ||
|
||
== 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_: | ||
|
||
.pom.xml | ||
[source,xml] | ||
[subs="verbatim,attributes"] | ||
---- | ||
<dependencies> | ||
<!-- ... --> | ||
<dependency> | ||
<groupId>org.springframework.session</groupId> | ||
<artifactId>spring-session-data-gemfire</artifactId> | ||
<version>{spring-session-version}</version> | ||
<type>pom</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>{spring-version}</version> | ||
</dependency> | ||
</dependencies> | ||
---- | ||
|
||
ifeval::["{version-snapshot}" == "true"] | ||
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_: | ||
|
||
.pom.xml | ||
[source,xml] | ||
---- | ||
<repositories> | ||
<!-- ... --> | ||
<repository> | ||
<id>spring-snapshot</id> | ||
<url>https://repo.spring.io/libs-snapshot</url> | ||
</repository> | ||
</repositories> | ||
---- | ||
endif::[] | ||
|
||
ifeval::["{version-milestone}" == "true"] | ||
Since we are using a Milestone version, we need to add the Spring Milestone Maven Repository. | ||
If you are using Maven, include the following `repository` declaration in your _pom.xml_: | ||
|
||
.pom.xml | ||
[source,xml] | ||
---- | ||
<repositories> | ||
<!-- ... --> | ||
<repository> | ||
<id>spring-milestone</id> | ||
<url>https://repo.spring.io/libs-milestone</url> | ||
</repository> | ||
</repositories> | ||
---- | ||
endif::[] | ||
|
||
// tag::config[] | ||
[[httpsession-spring-xml-configuration]] | ||
== Spring XML Configuration | ||
|
||
After adding the required dependencies and repository declarations, we can create our Spring configuration. | ||
The Spring configuration is responsible for creating a Servlet Filter that replaces the `HttpSession` | ||
with an implementation backed by Spring Session and GemFire. | ||
|
||
Add the following Spring Configuration: | ||
|
||
[source,xml] | ||
---- | ||
include::{samples-dir}httpsession-gemfire-clientserver-xml/src/main/webapp/WEB-INF/spring/session-client.xml[tags=beans] | ||
---- | ||
|
||
<1> First, a `Properties` bean is created to reference GemFire configuration common to both the client and server, | ||
stored in the `META-INF/spring/application.properties` file. | ||
<2> The `application.properties` are used along with the `PropertySourcesPlaceholderConfigurer` bean to replace | ||
placeholders in the Spring XML configuration meta-data with property values. | ||
<3> Spring annotation configuration support is enabled with `<context:annotation-config/>` element so that any | ||
Spring beans declared in the XML config that are annotated with either Spring or Standard Java annotations supported | ||
by Spring will be configured appropriately. | ||
<4> `GemFireHttpSessionConfiguration` is registered to enable Spring Session functionality. | ||
<5> Then, a Spring `BeanPostProcessor` is registered to determine whether a GemFire Server at the designated host/port | ||
is running, blocking client startup until the server is available. | ||
<6> Next, we include a `Properties` bean to configure certain aspects of the GemFire client cache using | ||
http://gemfire.docs.pivotal.io/docs-gemfire/reference/topics/gemfire_properties.html[GemFire's System properties]. | ||
In this case, we are just setting GemFire's `log-level` from a sample application specific System property, defaulting | ||
to `warning` if unspecified. | ||
<7> Finally, we create the GemFire client cache and configure a Pool of client connections to talk to the GemFire Server | ||
in our Client/Server topology. In our configuration, we use sensible settings for timeouts, number of connections | ||
and so on. Also, our `Pool` has been configured to connect directly to a server. | ||
|
||
TIP: In typical GemFire deployments, where the cluster includes potentially hundreds of GemFire data nodes (servers), | ||
it is more common for clients to connect to one or more GemFire Locators running in the cluster. A Locator passes meta-data | ||
to clients about the servers available, load and which servers have the client's data of interest, which is particularly | ||
important for single-hop, direct data access. See more details about the http://gemfire.docs.pivotal.io/docs-gemfire/latest/topologies_and_comm/cs_configuration/chapter_overview.html[Client/Server Topology in GemFire's User Guide]. | ||
|
||
NOTE: For more information on configuring _Spring Data GemFire_, refer to the http://docs.spring.io/spring-data-gemfire/docs/current/reference/html/[reference guide]. | ||
|
||
=== Server Configuration | ||
|
||
Now, we have only covered one side of the equation. We also need a GemFire Server for our client to talk to and pass | ||
session state information up to the server to manage. | ||
|
||
In this sample, we will use the following GemFire Server Java Configuration: | ||
|
||
[source,xml] | ||
---- | ||
include::{samples-dir}httpsession-gemfire-clientserver-xml/src/main/resources/META-INF/spring/session-server.xml[tags=beans] | ||
---- | ||
|
||
<1> First, we enable Spring annotation config support with the `<context:annotation-config>` element so that any | ||
Spring beans declared in the XML config that are annotated with either Spring or Standard Java annotations supported | ||
by Spring will be configured appropriately. | ||
<2> A `PropertySourcesPlaceholderConfigurer` is registered to replace placeholders in our Spring XML configuration | ||
meta-data with property values from `META-INF/spring/application.properties`. | ||
<3> We enable the same Spring Session functionality that we used on the client by registering an instance of `GemFireHttpSessionConfiguration`, | ||
except that we set the session expiration timeout to **15 seconds**. We will explain later what this means. | ||
<4> Next, we configure the GemFire Server using GemFire System properties very much like our P2P samples. | ||
With the `mcast-port` set to 0 and no `locators` property specified, our server will be standalone. We also allow a | ||
JMX client (e.g. _Gfsh_) to connect to our server with the use of the GemFire-specific JMX System properties. | ||
<5> Then, we create an instance of the GemFire peer cache using our GemFire System properties. | ||
<6> And finally, we also setup a GemFire `CacheServer` instance on *localhost* listening on port *55321* to accept | ||
our client connection. | ||
|
||
The GemFire Server configuration gets bootstrapped with the following: | ||
|
||
[source,java] | ||
---- | ||
include::{samples-dir}httpsession-gemfire-clientserver-xml/src/main/java/sample/Application.java[tags=class] | ||
---- | ||
|
||
TIP: Instead of a simple Java class with a main method, you could also use _Spring Boot_. | ||
|
||
<1> The `@Configuration` annotation designates this Java class as a source for Spring configuration meta-data using | ||
Spring's annotation configuration support. | ||
<2> Primarily, the configuration comes from the `META-INF/spring/session-server.xml` file, which is also the reason | ||
why _Spring Boot_ was not used in this sample, since using XML seemingly defeats the purpose and benefits | ||
of using Spring Boot. However, this sample is about demonstrating how to use Spring XML to configure | ||
the GemFire client and server. | ||
|
||
== XML Servlet Container Initialization | ||
|
||
Our <<httpsession-spring-xml-configuration,Spring XML Configuration>> created a Spring bean named `springSessionRepositoryFilter` | ||
that implements `Filter`. The `springSessionRepositoryFilter` bean is responsible for replacing the `HttpSession` with | ||
a custom implementation that is backed by Spring Session and GemFire. | ||
|
||
In order for our `Filter` to do its magic, we need to instruct Spring to load our `session-client.xml` configuration file. | ||
We do this with the following configuration: | ||
|
||
.src/main/webapp/WEB-INF/web.xml | ||
[source,xml,indent=0] | ||
---- | ||
include::{samples-dir}httpsession-gemfire-clientserver-xml/src/main/webapp/WEB-INF/web.xml[tags=context-param] | ||
include::{samples-dir}httpsession-gemfire-clientserver-xml/src/main/webapp/WEB-INF/web.xml[tags=listeners] | ||
---- | ||
|
||
The http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#context-create[ContextLoaderListener] | ||
reads the `contextConfigLocation` context parameter value and picks up our _session-client.xml_ configuration file. | ||
|
||
Finally, we need to ensure that our Servlet Container (i.e. Tomcat) uses our `springSessionRepositoryFilter` | ||
for every request. | ||
|
||
The following snippet performs this last step for us: | ||
|
||
.src/main/webapp/WEB-INF/web.xml | ||
[source,xml,indent=0] | ||
---- | ||
include::{samples-dir}httpsession-gemfire-clientserver-xml/src/main/webapp/WEB-INF/web.xml[tags=springSessionRepositoryFilter] | ||
---- | ||
|
||
The http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/DelegatingFilterProxy.html[DelegatingFilterProxy] | ||
will look up a bean by the name of `springSessionRepositoryFilter` and cast it to a `Filter`. For every request that `DelegatingFilterProxy` | ||
is invoked, the `springSessionRepositoryFilter` will be invoked. | ||
// end::config[] | ||
|
||
[[httpsession-gemfire-clientserver-xml-sample-app]] | ||
== HttpSession with GemFire (Client/Server) using XML Sample Application | ||
|
||
|
||
=== Running the httpsession-gemfire-clientserver-xml Sample Application | ||
|
||
You can run the sample by obtaining the {download-url}[source code] and invoking the following commands. | ||
|
||
First, you need to run the server using: | ||
|
||
---- | ||
$ ./gradlew :samples:httpsession-gemfire-clientserver-xml:run [-Dsample.httpsession.gemfire.log-level=info] | ||
---- | ||
|
||
Now, in a separate terminal, you can run the client using: | ||
|
||
---- | ||
$ ./gradlew :samples:httpsession-gemfire-clientserver-xml:tomcatRun [-Dsample.httpsession.gemfire.log-level=info] | ||
---- | ||
|
||
You should now be able to access the application at http://localhost:8080/. In this sample, the web application | ||
is the client cache and the server is standalone. | ||
|
||
=== Exploring the httpsession-gemfire-clientserver-xml 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. | ||
|
||
=== How does it work? | ||
|
||
We interact with the standard `HttpSession` in the `SessionServlet` shown below: | ||
|
||
.src/main/java/sample/SessionServlet.java | ||
[source,java] | ||
---- | ||
include::{samples-dir}httpsession-gemfire-clientserver/src/main/java/sample/SessionServlet.java[tags=class] | ||
---- | ||
|
||
Instead of using Tomcat's `HttpSession`, we are actually persisting the values in GemFire. | ||
Spring Session creates a cookie named SESSION in your browser that contains the id of your session. | ||
Go ahead and view the cookies (click for help with https://developer.chrome.com/devtools/docs/resources#cookies[Chrome] | ||
or https://getfirebug.com/wiki/index.php/Cookies_Panel#Cookies_List[Firefox]). | ||
|
||
NOTE: The following instructions assume you have a local GemFire installation. For more information on installation, | ||
see http://gemfire.docs.pivotal.io/docs-gemfire/latest/getting_started/installation/install_intro.html[Installing Pivotal GemFire]. | ||
|
||
If you like, you can easily remove the session using `gfsh`. For example, on a Linux-based system type the following | ||
at the command-line: | ||
|
||
$ gfsh | ||
|
||
Then, enter the following commands in _Gfsh_ ensuring to replace `70002719-3c54-4c20-82c3-e7faa6b718f3` with the value | ||
of your SESSION cookie, or the session ID returned by the GemFire OQL query (which should match): | ||
|
||
.... | ||
gfsh>connect --jmx-manager=localhost[1099] | ||
gfsh>query --query='SELECT * FROM /ClusteredSpringSessions.keySet' | ||
Result : true | ||
startCount : 0 | ||
endCount : 20 | ||
Rows : 1 | ||
Result | ||
------------------------------------ | ||
70002719-3c54-4c20-82c3-e7faa6b718f3 | ||
NEXT_STEP_NAME : END | ||
gfsh>remove --region=/ClusteredSpringSessions --key="70002719-3c54-4c20-82c3-e7faa6b718f3" | ||
.... | ||
|
||
NOTE: The _GemFire User Guide_ has more detailed instructions on using http://gemfire.docs.pivotal.io/docs-gemfire/latest/tools_modules/gfsh/chapter_overview.html[gfsh]. | ||
|
||
Now visit the application at http://localhost:8080/ again and observe that the attribute we added is no longer displayed. | ||
|
||
Alternatively, you can wait *15 seconds* for the session to timeout (i.e. expire) and refresh the page. Again, the | ||
attribute we added should no longer be displayed in the table. However, keep in mind, that by refreshing the page, | ||
you will inadvertently create a new (empty) session. If you run the query again, you will also see two session IDs, | ||
the new and the old, since GemFire keeps a "tombstone" of the old session around. |
Oops, something went wrong.