Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all sessions in Vaadin

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.

like image 953
deltascience Avatar asked Jul 28 '14 12:07

deltascience


People also ask

Is vaadin stateless?

Vaadin Fusion: StatelessBy default, Fusion will not create server sessions and will use the token-based authentication mechanism, keeping the server stateless. Since server endpoints don't use a session, a server can handle more concurrent users, enabling easier horizontal scaling and high availability of services.

What is flow in vaadin?

Vaadin Flow is a framework for building web apps 100% in Java. Flow automates everything on the frontend. The strong abstraction layer and server-driven architecture provide security and stability, allowing you to focus on creating user value.


1 Answers

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());    
    }
}
like image 125
d2k2 Avatar answered Oct 22 '22 23:10

d2k2