Share sessions data between different apps using httpSessionCache and HazelCast in OpenLiberty

363 Views Asked by At

Usings the OpenLiberty sessionCache-1.0 feature with HazelCast enables you to easily persist and share session data in a HazelCast in-memory cluster as explained here: https://openliberty.io/guides/sessions.html.

However in this setup session data is stored internally in the maps named: com.ibm.ws.session.attr.[app-context-root] & com.ibm.ws.session.meta.[app-context-root] as indicated here (I don't see the OpenLiberty docs clearly specifying this though)

This prevents different apps (with different context-roots) to share session data since they are writing and reading session data from different named maps.

Is there a way to overwrite this name to enable apps with different context-roots to read write from the same map to share session data?

I was going over the httpSession- and httpSessionCache-properties in the OpenLiberty docs but could not find any attribute supporting such a thing.

2

There are 2 best solutions below

1
On BEST ANSWER

To share session between different Web apps within the same EAR, you can use shared-session-context in ibm-application-ext.xml to enable all Web apps use the same session context-root.

https://www.ibm.com/docs/en/was-liberty/base?topic=configuration-osgiapplication#application-ext

Here's an example:

<?xml version="1.0" encoding="UTF-8"?>
<application-ext version="1.1" 
    xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-application-ext_1_1.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://websphere.ibm.com/xml/ns/javaee">
    
    <shared-session-context value="true"/>
</application-ext>
0
On

Sharing session data across different apps is forbidden by the spec:

HttpSession objects must be scoped at the application (or servlet context) level. [...] the object referenced, including the attributes in that object, must never be shared between contexts by the container.

So if you want to share some data between different apps, you have to create separate cache, not related to the sessions. You can also use Hazelcast for it, just not the session cache.

If all apps for example needs to share data that is related to given user, user login could be a key in the cache store.