Jenkins : Invalidate Jenkins HTTP sessions

This script can monitor and invalidate HTTP sessions if there are many open ones on your server.Monitoring Plugin should be installed.

You can run this script in Jenkins Console. But I think it is good idea to create Jenkins task, which will be run periodically (for example hourly). Groovy Plugin needs for Jenkins jobs.

Set maxSessionCount for your server. 

Invalidate sessions may be useful if you wish to improve performance (Memory usage)

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import net.bull.javamelody.*;
  
def chuck = SessionListener.newInstance();
println ("Session Info: ");
println chuck.getAllSessionsInformations();
sessionCount = chuck.getSessionCount();
println ("Number of Session: " + sessionCount);
maxSessionCount = 70;
if ( sessionCount > maxSessionCount ) {
          println ("Open Session " + sessionCount + " more than " + maxSessionCount);
          println "Invalidating...";
          chuck.invalidateAllSessions();
}