Creating a Sample Distributable WAR File for JBoss EAP 8.1

JBoss EAP 8.1 supports HTTP session clustering, replicating a session across multiple JBoss EAP instances. This post won’t go deep into configuring the cluster itself, instead it focuses on the application side. Here’s a minimal WAR file you can deploy to an already-clustered JBoss EAP 8.1 environment to verify that session replication is working.

Below is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.edw</groupId>
    <artifactId>jboss-eap-distributable</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.bom</groupId>
                <artifactId>jboss-eap-ee</artifactId>
                <version>8.1.0.GA-redhat-00001</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>

</project>

And web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0">

    <display-name>JBoss EAP Distributable Hello World</display-name>

    <distributable/>

</web-app>

The element is what tells EAP this webapp’s sessions are eligible for replication across the cluster.

This is my Java file which i use,

package com.edw;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {

    private static final String COUNT_KEY = "count";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(true);
        Integer count = (Integer) session.getAttribute(COUNT_KEY);
        if (count == null) {
            count = 0;
        }
        count++;
        session.setAttribute(COUNT_KEY, count);

        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.println("<html><body>");
        out.println("<h1>Hello World!</h1>");
        out.println("<p>This application is cluster-aware (distributable).</p>");
        out.println("<p>Session ID: " + session.getId() + "</p>");
        out.println("<p>Session Count: " + count + "</p>");
        out.println("<p>Server Time: " + new Date() + "</p>");
        out.println("<p>Check your server logs to see session replication in action if running in a cluster.</p>");
        out.println("</body></html>");
    }
}

Once we have deployed this WAR to a cluster, say two EAP instances at 192.168.5.180 and 192.168.5.181, we can confirm the session is being replicated by reusing the same cookie jar across both nodes,

$ curl -kv http://192.168.5.180:8080/jboss-eap-distributable/hello -c cookies.txt -b cookies.txt

$ curl -kv http://192.168.5.181:8080/jboss-eap-distributable/hello -c cookies.txt -b cookies.txt

If clustering is configured correctly, the second request should return the same session ID and an incremented session count, even though it hit a different node. If the session ID changes or the count resets to 1, that’s a sign session replication (or your load balancer’s sticky-session config) isn’t working as expected.

The whole source code for this project can be seen on the below repository,

https://github.com/edwin/jboss-eap-distributable

Leave a Comment

Your email address will not be published.