如何在Vaadin中获得所有会话 [英] How to get all sessions in Vaadin
本文介绍了如何在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.
推荐答案
到目前为止,我发现的最佳解决方案是在会话创建和销毁时计算会话。
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++;
}
}
然后在VaadinServlet init()方法中添加SessionListeners
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());
}
}
这篇关于如何在Vaadin中获得所有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文