Infinispan can be configured in embedded mode. In this configuration, the Infinispan server resides in the same JVM as your Open Liberty server. Infinispan utilizes JGroups to allow multiple embedded servers to form a cluster. In our context of the sessionCache-1.0 feature, this clustering provides high availablility of user session data. Embedded mode will generally perform faster per request than client/server mode. This is due to the network and serialization costs associated with the remote calls of client/server mode.

To configure embedded mode in Open Liberty, we’ll start with a sample server.xml :

<server> <featureManager> <feature> servlet-4.0 </feature> <feature> mpMetrics-2.0 </feature> <feature> mpReactiveStreams-1.0 </feature> <feature> sessionCache-1.0 </feature> </featureManager> <httpEndpoint host = " * " id = " defaultHttpEndpoint " httpPort = " 9080 " httpsPort = " 9443 " /> <httpSessionCache enableBetaSupportForInfinispan = " true " libraryRef = " InfinispanLib " uri = " file:${shared.resource.dir}/infinispan/infinispan.xml " /> <library id = " InfinispanLib " > <fileset dir = " ${shared.resource.dir}/infinispan " includes = " *.jar " /> </library> </server>

The key piece is the <httpSessionCache> tag. The enableBetaSupportForInfinispan="true" attribute enables the beta Infinispan support code in Open Liberty. The libraryRef="InfinispanLib" attribute references a shared library to load the Infinispan jars from. Finally, the uri="file:${shared.resource.dir}/infinispan/infinispan.xml" attribute points to Infinispan’s own configuration file. Be sure to enabled the sessionCache-1.0 , mpReactiveStreams-1.0 , and mpMetrics-2.0 features. Currently mpMetrics-2.0 and mpReactiveStreams-1.0 are hard dependencies, but we’re aiming to make these optional in the future.

Next, you’ll need to create the infinispan.xml referenced from the server.xml configuration:

<infinispan> <jgroups> <stack-file name = " jgroups-kubernetes " path = " /default-configs/default-jgroups-kubernetes.xml " /> </jgroups> <cache-container> <transport stack = " jgroups-kubernetes " /> </cache-container> </infinispan>

The JGroups stack is what determines how your Infinispan servers forms a cluster. We’re using the default Kubernetes template because we’re running in OpenShift. See Configuring Infinispan 10 for Infinispan’s complete documentation on setting up your cluster.

Next, you’ll need to create a headless Kubernetes service to enable the Kubernetes JGroups transport stack to form a cluster. The key here is for the name of the selector to match one of the labels associated with your Open Liberty applications running in OpenShift. For example, an application defined using the command oc new-app --image-stream=ol-runtime-infinispan-embedded:1.0.0 --name=embedded-servera -l name=ol-runtime-infinispan-embedded has a label of name=ol-runtime-infinispan-embedded . This label then matches the service defined below, and triggers the application to be a part of the service.

oc create -f service.yaml

apiVersion : v1 kind : Service metadata : name : infinispan-embedded spec : clusterIP : None ports : - name: discovery port : 7800 protocol : TCP targetPort : 7800 selector : name : ol-runtime-infinispan-embedded sessionAffinity : None type : ClusterIP status : loadBalancer : {}

Finally, you need to create a jvm.options file in your main Open Liberty server directory. This file sets jgroups.dns.query which points to the DNS record that should return all the members of your Infinispan cluster. If your environment doesn’t support IPv6 then you also want to set -Djava.net.preferIPv4Stack=true . See the following sample jvm.options file:

# Set if IPv6 is not supported. -Djava.net.preferIPv4Stack=true # This value matches the DNS lookup of the headless service defined in the previous step. Your domain might vary. -Djgroups.dns.query=infinispan-embedded.myproject.svc.cluster.local

For an OpenShift environment, server.xml , infinispan.xml , service.yaml , and jvm.options are the four files you’ll need to configure to enable Infinispan in embedded mode. If you wish to run outside of an OpenShift environment, a server.xml with the proper features enabled, a shared library for Infinispan, and the following tag is all that is required: