java – 如何获取Vaadin中的所有会话
作者:互联网
我想知道有多少用户实时连接到我的应用程序.我有想法循环开放的会话数,但我找不到如何做到这一点.如果您有其他方法,欢迎提出建议.
解决方法:
到目前为止,我发现的最佳解决方案是在创建和销毁会话时对其进行计数.
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
@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());
}
}
标签:java,session,vaadin 来源: https://codeday.me/bug/20191007/1868664.html