How to get Spring Session for GemFire session information by function execution service via gfsh
search cancel

How to get Spring Session for GemFire session information by function execution service via gfsh

book

Article ID: 294418

calendar_today

Updated On:

Products

VMware Tanzu Gemfire

Issue/Introduction

This article describes  how to log Spring Session for VMware Tanzu GemFire session information by function execution service using gfsh.


Environment

Product Version: 9.10

Resolution

Step1:
Create a on-region function execution service class like the below.
import org.apache.geode.cache.Declarable;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.cache.execute.RegionFunctionContext;
import org.apache.geode.cache.partition.PartitionRegionHelper;
import org.apache.geode.cache.Region;
import java.util.*;
import static org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepository.GemFireSession;

public class DumpSessionsFunction implements Function, Declarable {

    public void execute(FunctionContext context) {
        RegionFunctionContext rfc = (RegionFunctionContext) context;
        Region region = PartitionRegionHelper.getLocalDataForContext(rfc);
        String sessions = getSessions(region);
        context.getCache().getLogger().info(sessions);
        context.getResultSender().lastResult(true);
    }
 

    private String getSessions(Region region) {
        Set keySet = region.entrySet();
        String header = "This member contains the following " + keySet.size() + " primary sessions: " + "\n";
        
        StringBuffer str = new StringBuffer();

        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Region.Entry e = (Region.Entry) iterator.next();
            str.append("Session Key: " + e.getKey() + "\n");
            str.append("Session Value: "+ e.getValue() + "\n\n");
        }

		return header + str.toString();

    }

    public String getId() {
        return getClass().getSimpleName();
    }

    public boolean optimizeForWrite() {
        return true;
    }

    public static int sizeObject(Object o) {
		return ReflectionObjectSizer.getInstance().sizeof(o);
	}

}

Step2:
Compile this function code and create a jar, deploy it into Gemfire Cluster.
For example:
user1$ javac DumpSessionsFunction.java
user1$ jar cf DumpSessionsFunction.jar DumpSessionsFunction.class

gfsh>deploy --jar=/Users/user1/DumpSessionsFunction/DumpSessionsFunction.jar

Deploying files: DumpSessionsFunction.jar
Total file size is: 0.00MB

Continue?  (Y/n): Y
   Member    |       Deployed JAR       | Deployed JAR Location
------------ | ------------------------ | -----------------------------------------------------------------------------------
cacheserver1 | DumpSessionsFunction.jar | /Users/user1/cacheserver1/DumpSessionsFunction.v1.jar
cacheserver2 | DumpSessionsFunction.jar | /Users/user1/cacheserver2/DumpSessionsFunction.v1.jar
cacheserver3 | DumpSessionsFunction.jar | /Users/user1/cacheserver3/DumpSessionsFunction.v1.jar
cacheserver4 | DumpSessionsFunction.jar | /Users/user1/cacheserver4/DumpSessionsFunction.v1.jar

Step3:
This function is an on-region function and should be executed as the following:
execute function --id=DumpSessionsFunction --region=/SpringSessionRegionName
For example:
gfsh>execute function --id=DumpSessionsFunction --region=/SpringSessionRegion1
   Member    | Status | Message
------------ | ------ | ------------------------
cacheserver1 | OK     | [true, true, true, true]

Step4:
You can confirm the session information from cacheservers's log.
For example:
gfsh>show log --member=cacheserver1
[info 2022/01/05 19:26:59.794 JST cacheserver1 <Function Execution Processor3> tid=0xac] This member contains the following 3 primary sessions: 
Session Key: 856c246c-c985-423c-8798-4c23408fdc2a
Session Value: { @type = org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepository$GemFireSession, id = 856c246c-c985-423c-8798-4c23408fdc2a, creationTime = 2022-01-05T10:26:46.592Z, lastAccessedTime = 2022-01-05T10:26:46.592Z, maxInactiveInterval = PT15M, principalName = null }

Session Key: f42d3fc7-33eb-47d6-9dea-dd344dcb923d
Session Value: { @type = org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepository$GemFireSession, id = f42d3fc7-33eb-47d6-9dea-dd344dcb923d, creationTime = 2022-01-05T10:21:32.305Z, lastAccessedTime = 2022-01-05T10:21:32.305Z, maxInactiveInterval = PT15M, principalName = null }

Session Key: 6ece35a5-32d1-4d9e-9a00-ac32d1e56fee
Session Value: null