The best solution I have found so far is to count the sessions when they are created and destroyed.
public class VaadinSessionListener{ private static volatile int activeSessions = 0; public static class VaadinSessionInitListener implements SessionInitListener{ @Override public void sessionInit(SessionInitEvent event) throws ServiceException { incSessionCounter(); } } public static class VaadinSessionDestroyListener implements SessionDestroyListener{ @Override public void sessionDestroy(SessionDestroyEvent event) { if(event.getSession() != null && event.getSession().getSession() != null){ decSessionCounter(); } } } public static Integer getActiveSessions() { return activeSessions; } private synchronized static void decSessionCounter(){ if(activeSessions > 0){ activeSessions--; } } private synchronized static void incSessionCounter(){ activeSessions++; } }
then add SessionListeners to the init () method of VaadinServlet
@WebServlet(urlPatterns = "/*", asyncSupported = true) @VaadinServletConfiguration(productionMode = true, ui = MyUI.class) public static class Servlet extends VaadinServlet { @Override public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); getService().addSessionInitListener(new VaadinSessionListener.VaadinSessionInitListener()); getService().addSessionDestroyListener(new VaadinSessionListener.VaadinSessionDestroyListener()); } }
source share