我發現只要實現sessionCreated和sessionDestroyed,它就完全可靠。唯一的缺點是你將失去活動會話Tomcat知道你的應用程序是否沒有運行或重新啓動,但我從來沒有看到它錯過了創建或銷燬的通知。
確保您將會話數據存儲在處理併發性的某些內容中,如ConcurrentHashMap。
這裏有一個簡單的例子:
public class SessionData implements HttpSessionListener {
private final static Map<String, HttpSession> activeSessions = new ConcurrentHashMap<>();
@Override
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
if (session != null) {
String id = session.getId();
System.out.println("HTTP session created. ID: " + id);
activeSessions.put(id, session);
}
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
if (session != null) {
String id = session.getId();
System.out.println("HTTP session destroyed. ID: " + id);
activeSessions.remove(id);
}
}
}
_IT將是極其useful._那你想用它做什麼? – 2015-01-27 01:30:28
好吧,例如我可以立即確定有多少活動會話正在使用相同(和哪些)用戶令牌,我們的應用程序存儲在會話屬性中 – Black 2015-01-27 12:33:54