HTTP Session State Caching
Disable Near Caching Within the App
Near caching is when an app locally caches data. Near caching uses an embedded cache within the app. Web apps that deploy Tomcat with VMware Tanzu GemFire session state caching have near caching by default. To keep an app stateless, you will want to disable near caching.
There are two methods for disabling near caching. Choose and implement one of these methods:
Create and use a custom buildpack (that disables near caching) that resides in an external repository. Modify the app setup to acquire the configuration from the external repository. This method facilitates having a single configuration that may be used by a variety of apps.
Create and use a custom buildpack (that disables near caching) for the app.
Disable Caching Using an External Repository for Configuration
There are two parts to this method for disabling near caching within the app. The first part builds a repository to hold the configuration and custom buildpack, and then pushes the repository to the space such that the app will have access. The second part changes the app configuration such that it uses the custom buildpack.
The procedure to build the repository:
Make a directory to hold the configuration:
mkdir tomcat-config
Make other needed files and directories within the newly created directory:
cd tomcat-config mkdir public mkdir -p tomcat-1.0.0/conf touch Staticfile
Edit
Staticfile
to contain:root: public directory: visible
Create
tomcat-1.0.0/conf/context.xml
, and edit the file such that it contains the following content:<?xml version='1.0' encoding='UTF-8'?> <!-- ~ Copyright 2013-2019 the original author or authors. ~ ~ Licensed under the Apache License, Version 2.0 (the "License"); ~ you may not use this file except in compliance with the License. ~ You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, software ~ distributed under the License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~ See the License for the specific language governing permissions and ~ limitations under the License. --> <Context> <Resources allowLinking='true'/> <Manager className='org.apache.geode.modules.session.catalina.Tomcat8DeltaSessionManager' enableLocalCache='false' regionAttributesId='PARTITION_REDUNDANT_HEAP_LRU'/> </Context>
Place a compressed TAR format version of the
tomcat-1.0.0
directory into thepublic
directory:tar -czf tomcat-1.0.0.tar.gz tomcat-1.0.0/ cp tomcat-1.0.0.tar.gz public/
Use
cf push
to place the configuration into the VMware Tanzu Application Service for VMs space that will host this configuration:cf push tomcat-config
Issue the command
cf apps
to acquire the URL of the
tomcat-config
app. Prepend the listed URL withhttp://
to have the URL that will identify the location of the configuration needed by the app.For example, your complete URL will look something like
http://tomcat-config.apps.yellow-green.cf-app.com
.Within the
tomcat-config
directory, editpublic/index.yml
to have URL-specific contents. Using the example URL as a guide, the contents of thispublic/index.yml
file will contain:--- 1.0.0: http://tomcat-config.apps.yellow-green.cf-app.com/tomcat-1.0.0.tar.gz
Push the app a second time with its completed configuration:
cf push tomcat-config
The second part of this procedure modifies the app such that it uses the custom buildpack:
Edit the
manifest.yml
file by appending this URL-specific configuration to theapplications
section of themanifest.yml
file:env: JBP_CONFIG_TOMCAT: "{ tomcat: { external_configuration_enabled: true }, external_configuration: { repository_root: \"http://tomcat-config.apps.yellow-green.cf-app.com\" } }"
Substitute your complete URL for the URL in this example.
A complete
manifest.yml
file will appear similar to:--- applications: - name: http-session-caching path: build/libs/http-session-caching-0.0.1.war buildpack: java_buildpack_offline env: JBP_CONFIG_TOMCAT: "{ tomcat: { external_configuration_enabled: true }, external_configuration: { repository_root: \"http://tomcat-config.apps.yellow-green.cf-app.com\" } }"
Push, bind, and start your app with a buildpack of version 4.18 or a more recent version:
cf push -f ./manifest.yml --no-start -b https://github.com/cloudfoundry/java-buildpack.git#v4.18 cf bind-service APP-NAME SERVICE-INSTANCE-NAME cf start APP-NAME
To verify that local caching is disabled for the app, use
cf ssh
to access the app and visually verify thatenableLocalCache='false'
appears within thecontext.xml
file. Use this sequence of commands:cf ssh APP-NAME find ./ -name *.xml cat ./app/.java-buildpack/tomcat/conf/context.xml
Disable Caching Using a Custom Java Buildpack
This procedure creates and uses a custom java buildpack that
disables near caching in the app.
Once created, the cf push
specifies the custom Java buildpack.
Clone the git buildpack repository:
git clone git@github.com:cloudfoundry/java-buildpack.git
Change directories to the newly created repository:
cd java-buildpack
Edit the Apache Geode ruby configuration file such that it disables caching within the app. The file to edit is
lib/java_buildpack/container/tomcat/tomcat_geode_store.rb
. The single change is the stringtrue
to instead befalse
. Before the change, here is the portion of the file to be changed, with the string to change highlighted:def add_manager(context) context.add_element 'Manager', 'className' => SESSION_MANAGER_CLASS_NAME, 'enableLocalCache' => 'true', 'regionAttributesId' => REGION_ATTRIBUTES_ID end
After changing the highlighted string from
true
tofalse
, here is the portion of the file with the change highlighted:def add_manager(context) context.add_element 'Manager', 'className' => SESSION_MANAGER_CLASS_NAME, 'enableLocalCache' => 'false', 'regionAttributesId' => REGION_ATTRIBUTES_ID end
Create your custom Java buildpack on the platform with a command of the form:
cf create-buildpack BUILDPACK PATH POSITION --enable
where
BUILDPACK
is the name you choose for your buildpack.After building your application, push it such that it uses your buildpack:
cf push -f ./manifest.yml -b BUILDPACK
Bind your app as described in Bind an App to a Service Instance.
Restage the app to ensure proper configuration:
cf restage APP-NAME