Web Session Storage with Infinispan Client

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 IDE

  • Java 8 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:

package io.vertx.howtos.web.sessions;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.Session;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.SessionStore;
import io.vertx.ext.web.sstore.infinispan.InfinispanSessionStore;

import java.util.Date;

public class ServerVerticle extends AbstractVerticle {

  public static final String TEMPLATE = ""
    + "Session [%s] created on %s%n"
    + "%n"
    + "Page generated on %s%n";

  @Override
  public void start() {
    Router router = Router.router(vertx);

    JsonObject options = new JsonObject()
      .put("servers", new JsonArray()
        .add(new JsonObject()
          .put("host", "localhost")
          .put("port", 11222)
          .put("username", "foo")
          .put("password", "bar"))
      );
    SessionStore store = InfinispanSessionStore.create(vertx, options); // (1)

    router.route().handler(SessionHandler.create(store)); // (2)

    router.get("/").handler(ctx -> {
      Session session = ctx.session();
      session.computeIfAbsent("createdOn", s -> System.currentTimeMillis()); // (3)

      String sessionId = session.id();
      Date createdOn = new Date(session.<Long>get("createdOn"));
      Date now = new Date();

      ctx.end(String.format(TEMPLATE, sessionId, createdOn, now)); // (4)
    });

    vertx.createHttpServer()
      .requestHandler(router)
      .listen(8080);
  }

  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(new ServerVerticle());
  }
}
  1. Create an InfinispanSessionStore using configuration for your Infinispan Cluster (e.g. host, port and credentials).

  2. Register the SessionHandler, which manages Vert.x Web Sessions.

  3. Put the session creation timestamp in the session data.

  4. Create the response body and send it to the client.

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="foo" -e PASS="bar" infinispan/server
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.


Last published: 2021-06-13 00:47:28 +0000.