This document will show you how to store Vert.x Web Sessions in Infinispan.
-
A text editor or an IDE
-
Java 11 or higher
-
Maven or Gradle
-
An Infinispan cluster (or Docker)
The code of this project contains Maven and Gradle build files that are functionally equivalent.
Assuming you use Gradle with the Kotlin DSL, here is what your build.gradle.kts
file should look like:
The Vert.x Web SessionStore
interface provides methods to read and modify session data.
The InfinispanSessionStore
implementation can be found in the io.vertx:vertx-web-sstore-infinispan
artifact.
Here is the code of the ServerVerticle
class:
If you’re not familiar with Infinispan, check out the introduction.
To run an Infinispan server on your machine with Docker, open your terminal and execute this:
docker run -p 11222:11222 -e USER="admin" -e PASS="bar" infinispan/server:13.0
Note
|
If you already have an Infinispan cluster running, don’t forget to update the configuration (host, port and credentials) in ServerVerticle .
|
The ServerVerticle
already has a main
method, so it can be used as-is to:
-
create a
Vertx
context, then -
deploy
ServerVerticle
.
You can run the application from:
-
your IDE, by running the
main
method from theServerVerticle
class, or -
with Maven:
mvn compile exec:java
, or -
with Gradle:
./gradlew run
(Linux, macOS) orgradle run
(Windows).
-
Browse to http://localhost:8080. If you request this page for the first time, the session creation time should be the same as the page generation time.
-
Now, let’s simulate failover to another web server by stopping the application and starting it again.
-
After the new web server has started, browse to http://localhost:8080 again. The session id and session creation time shouldn’t have changed (because session data has been persisted in Infinispan).
This document covered:
-
creating an
InfinispanSessionStore
instance, -
registering the Vert.x Web
SessionHandler
, -
modifying session data.