Tanzu GemFire on Cloud Foundry 2.1

Connecting a Spring Boot App to VMware Tanzu GemFire with Session State Caching

Last Updated March 03, 2025

This topic describes the two ways in which you can connect a Spring Boot app to VMware Tanzu GemFire on Cloud Foundry:

  • Using a Tomcat app with a WAR file. This is the default method for Tomcat apps.
  • Using the spring-session-data-gemfire library. This method requires that you use the correct version of these libraries.

Use the Tomcat App

To get a Spring Boot app running with session state caching (SSC) on Tanzu GemFire on Cloud Foundry, you must create a WAR file using the spring-boot-starter-tomcat plugin instead of the spring-boot-maven plugin to create a JAR file.

For example, if you want your app to use SSC, you cannot use spring-boot-maven to build a JAR file and push your app to VMware Tanzu Platform on Cloud Foundry (Tanzu Platform on Cloud Foundry), because the Java buildpack does not pull in the necessary JAR files for SSC when it detects a Spring JAR file.

To build your WAR file, add this dependency to your pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
</dependency>

For a full example of running a Spring Boot app that connects with SSC, run this app and use the following for your 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>io.pivotal.gemfire.demo</groupId>
  <artifactId>HttpSessionCaching-Webapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>HttpSessionCaching-Webapp</name>
  <description>Demo project for GemFire Http Session State caching</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

Use a Spring Session Data GemFire App

You can connect your Spring app to Tanzu GemFire on Cloud Foundry to do session state caching. Use the correct version of the spring-session-data-gemfire library; apps built for Tanzu GemFire on Cloud Foundry v1.6.0 and later versions are compatible with Spring Session Data GemFire v2.1.5.RELEASE and later versions.

Upgrade Tanzu GemFire on Cloud Foundry and Spring Session Data GemFire

  1. Before your operator upgrades Tanzu GemFire on Cloud Foundry, stop your app. This avoids breaking the app in this upgrade process.

  2. Upgrade Tanzu GemFire on Cloud Foundry. See Upgrading Tanzu GemFire on Cloud Foundry for details.

  3. Rebuild your app using a build.gradle file that depends on the correct version of VMware GemFire. Here is an example build.gradle file:

    version = '0.0.1-SNAPSHOT'
    
    buildscript {
      ext {
        springBootVersion = '2.1.8.RELEASE'
      }
      repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
      }
      dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
      }
    }
    
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'idea'
    
    idea{
      module{
        downloadSources = true
        downloadJavadoc = true
      }
    }
    
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    
    repositories {
      mavenCentral()
      maven { url "https://repo.spring.io/libs-milestone" }
      maven { url "https://repo.spring.io/milestone" }
      maven { url "http://repo.springsource.org/simple/ext-release-local" }
      maven { url "http://repo.spring.io/libs-release/" }
      maven { url "https://repository.apache.org/content/repositories/snapshots" }
    }
    
    
    dependencies {
      compile("org.springframework.boot:spring-boot-starter-web:2.1.8.RELEASE")
      compile("org.springframework.session:spring-session-data-gemfire:2.1.5.RELEASE")
      compile("org.springframework.geode:spring-gemfire-starter:1.1.0.RELEASE")
    }
    
  4. Clear the session state region.

  5. Start the rebuilt app.