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.
Asked
Active
Viewed 8,427 times
2 Answers
8
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());
}
}

d2k2
- 726
- 8
- 16
-
It didn't work! I added `Servlet` class to my `UI` but it doesn't increment the counter. Is there something else that I have to change ? – deltascience Jul 29 '14 at 08:13
-
The Servlet class should be already available inside the UI. which vaadin version and servlet version are you using? – d2k2 Jul 29 '14 at 09:30
-
Vaadin 7.2 with Maven. And I use `com.vaadin.server.VaadinServlet` in Web.xml – deltascience Jul 29 '14 at 09:53
-
my code example was for Servlet 3.0 specification. you probably have also to add the correct Servlet in the web.xml. com.vaadin.server.VaadinServlet in web.xml is wrong. – d2k2 Jul 29 '14 at 10:37
-
It worked when I put the `Servlet` in an other java class and change the `Servlet` location in my `web.xml`. Thanks man the idea is good. – deltascience Jul 30 '14 at 07:21
7
[Retraction]
Here is a wrong Answer. I mistakenly thought the cited method answers the Question, but it does not. Consider this a retraction; rather than delete this Answer I'll leave it so that others avoid making my mistake.
VaadinSession.getAllSessions()
With Vaadin 7.2 came the addition of a static method, VaadinSession.getAllSessions
. For history, see Ticket # 13053.
That method returns a Collection
of VaadinSession
objects attached to a single HttpSession
.
This method tells you how many VaadinSession objects are running for a single user’s HttpSession, but does not tell you how many overall users are on your Vaadin app server.

Community
- 1
- 1

Basil Bourque
- 303,325
- 100
- 852
- 1,154
-
2This returns the vaadin sessions for a given http session. In order to achieve what was asked, the http sessions need to be collected as described here: http://stackoverflow.com/questions/10770535/is-there-any-method-to-obtain-in-a-servlet-all-valid-session-keys-values-on-jett or with the Vaadin SessionInitListener – sreg Mar 06 '15 at 21:58