主要使用application, listener, 把用戶id和用戶對象放到ConcurrentHashMap中,再存入application中
1、登錄時把id和對象放到application中
2、登出時把remove掉
3、listener sessionDestroyed的時候,把對象session的key從application中去掉
一、登錄時
// 2009.10.29 防止多處登錄
Object onlineUsersObject = session.getServletContext().getAttribute(Constants.ONLINE_USERS_KEY);
logger.info("on line user object is null: {}", null == onlineUsersObject);
if (null != onlineUsersObject) { // 不為空,說明有用戶在線。
Map<Long, UserInfo> onlineUsersMap = (Map<Long, UserInfo>) onlineUsersObject;
logger.info("----------001、打印在線用戶 id 列表(檢查用戶是否已經在列表中)---");
for (Long id : onlineUsersMap.keySet()) {
logger.info(String.valueOf(id));
}
logger.info("------------------------------------------------------------------------");
for (Long id : onlineUsersMap.keySet()) {
if (id.equals(userInfo.getId())) {
logger.info("{} == {}", id, userInfo.getId());
msg = super.getMessage(request, "login.failed.username.hadLogined");
super.renderJavaScript(response, "window.onload=function(){alert('" + msg
+ "');location.href='login.do" + queryString + "'}");
return null;
}
}
}
// 2009.10.29 防止多處登錄
// Object onlineUsersObject = session.getServletContext().getAttribute(Constants.ONLINE_USERS_KEY);
if (null != onlineUsersObject) {
Map<Long, UserInfo> onlineUsersMap = (Map<Long, UserInfo>) onlineUsersObject;
onlineUsersMap.put(ui.getId(), ui);
} else {
Map<Long, UserInfo> onlineUsersMap = new ConcurrentHashMap<Long, UserInfo>();
onlineUsersMap.put(ui.getId(), ui);
session.getServletContext().setAttribute(Constants.ONLINE_USERS_KEY, onlineUsersMap);
}
logger.info("----------002、登錄後打印在線用戶列表-------------");
for (Long id : ((Map<Long, UserInfo>) session.getServletContext().getAttribute(Constants.ONLINE_USERS_KEY)).keySet()) {
logger.info(String.valueOf(id));
}
logger.info("------------------------------------------------------------------------");
二、登出時
HttpSession session = request.getSession(false);
UserInfo ui = (UserInfo) session.getAttribute(Constants.USER_INFO);
// 在 application 中刪除已經注銷的用戶
Object onlineUsersObject = session.getServletContext().getAttribute(Constants.ONLINE_USERS_KEY);
if (null != onlineUsersObject) {
Map<Long, UserInfo> onlineUsersMap = (Map<Long, UserInfo>) onlineUsersObject;
logger.info("user is null ? {}", ui);
if (null != ui) {
onlineUsersMap.remove(ui.getId());
}
logger.info("------003、手動注銷前打印在線用戶列表-------------");
for (Long id : onlineUsersMap.keySet()) {
logger.info(String.valueOf(id));
}
logger.info("------------------------------------------------------------");
}
if (null != session) {
session.removeAttribute(Constants.USER_INFO);
session.invalidate();
}
if (null != onlineUsersObject) {
Map<Long, UserInfo> onlineUsersMap = (Map<Long, UserInfo>) onlineUsersObject;
onlineUsersMap.remove(ui);
logger.info("------004、手動注銷後打印在線用戶列表-------------");
for (Long id : onlineUsersMap.keySet()) {
logger.info(String.valueOf(id));
}
logger.info("------------------------------------------------------------");
}
三、OnlineSessionListener
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
logger.info("sessionDestroyed:{};session id is {}", session, session.getId());
Object onlineUsersObject = session.getServletContext().getAttribute(Constants.ONLINE_USERS_KEY);
UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_INFO);
logger.info("userInfo is null:{}", null == userInfo);
if (null != onlineUsersObject && null != userInfo) {
Map<Long, UserInfo> onlineUsersMap = (Map<Long, UserInfo>) onlineUsersObject;
logger.info("---------------從在線列表中移除超時用戶id前打印在線用戶列表-------------");
for (Long id : onlineUsersMap.keySet()) {
logger.info(String.valueOf(id));
}
logger.info("------------------------------------------------------------");
for (Long id : onlineUsersMap.keySet()) {
if (id.equals(userInfo.getId())) {
logger.info(String.valueOf(id));
onlineUsersMap.remove(id);
}
}
logger.info("---------------從在線列表中移除超時用戶id後打印在線用戶列表-------------");
for (Long id : onlineUsersMap.keySet()) {
logger.info(String.valueOf(id));
}
logger.info("------------------------------------------------------------");
}