How to get all sessions in Vaadin

deltascience

I want to know How many users are connected to my application in real time. I got the idea to loop on number of session that are open but I can't find how to do that. If you have another way to do it your suggestions are welcome.

d2k2

Best solution i 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) {

            /*
             * check if HTTP Session is closing
             */
            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 the SessionListeners in the VaadinServlet init() method

@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);


        /*
         * Vaadin SessionListener
         */
        getService().addSessionInitListener(new VaadinSessionListener.VaadinSessionInitListener());
        getService().addSessionDestroyListener(new VaadinSessionListener.VaadinSessionDestroyListener());    
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related