How to get all sessions in Vaadin

I want to know how many users are connected to my application in real time. I had the idea to focus on the number of open sessions, but I can not find how to do it. If you have another way to do this, your suggestions are welcome.

+6
source share
2 answers

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) { /* * 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 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); /* * Vaadin SessionListener */ getService().addSessionInitListener(new VaadinSessionListener.VaadinSessionInitListener()); getService().addSessionDestroyListener(new VaadinSessionListener.VaadinSessionDestroyListener()); } } 
+6
source

[Challenge]

There is no answer. I mistakenly thought that the cited method answers the question, but it is not. Consider this challenge ; rather than deleting this answer. I will leave it so that others do not make a mistake.


VaadinSession.getAllSessions()

With Vaadin 7.2, a static method has been added, VaadinSession.getAllSessions . For a story, see Ticket # 13053 .

This method returns a Collection VaadinSession attached to a single HttpSession .

This method tells you how many VaadinSession objects are running for a single HttpSession user, but does not tell you how many shared users are on your Vaadin application server.

+4
source

Source: https://habr.com/ru/post/972957/


All Articles