Skip to content

vertx-howtos/web-session-infinispan-howto

Repository files navigation

Web Session Storage with Infinispan Client

Build Status

This document will show you how to store Vert.x Web Sessions in Infinispan.

What you will build

You will build a Vert.x Web application that shows:

  • the session id and when the session was created

  • when the page was generated

run

The application fits in a single ServerVerticle class.

What you need

  • A text editor or an IDE

  • Java 11 or higher

  • Maven or Gradle

  • An Infinispan cluster (or Docker)

Create a project

The code of this project contains Maven and Gradle build files that are functionally equivalent.

Using Maven

Here is the content of the pom.xml file you should be using:

Using Gradle

Assuming you use Gradle with the Kotlin DSL, here is what your build.gradle.kts file should look like:

Storing and retrieving session data with Infinispan

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:

Running the application

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:

  1. create a Vertx context, then

  2. deploy ServerVerticle.

You can run the application from:

  1. your IDE, by running the main method from the ServerVerticle class, or

  2. with Maven: mvn compile exec:java, or

  3. with Gradle: ./gradlew run (Linux, macOS) or gradle run (Windows).

Testing the application

  1. 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.

  2. Now, let’s simulate failover to another web server by stopping the application and starting it again.

  3. 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).

Summary

This document covered:

  1. creating an InfinispanSessionStore instance,

  2. registering the Vert.x Web SessionHandler,

  3. modifying session data.

About

Web Session Storage with Infinispan Client

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published