当 JSF 应用程序关闭或 tomact 关闭时,我需要使活动会话对象无效并清理。以下是我在 application scoped bean 中编写的代码
@PreDestroy
public void shutdown(){
Map appMap = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();
Map<String,HttpSession> userSessionMap=(Map<String,HttpSession>)appMap.get("USERS");
log.info("userSessionMap " + userSessionMap);
Set<Entry<String, HttpSession>> entrySet = userSessionMap.entrySet();
for(Entry<String, HttpSession> entry:entrySet){
HttpSession session = entry.getValue();
log.info("HttpSession " + session.getId() + " calling invalidate");
session.invalidate();
}
}
并且以下被覆盖 HttpSessionListener
@Override
public void sessionDestroyed(HttpSessionEvent se){
HttpSession session = se.getSession();
String id = session.getId();
LoginActionController controller = (LoginActionController) session.getAttribute("userInfo");
log.info("HttpSession " + id + ", LoginActionController " + controller + " is being destroyed...");
if(controller != null){
log.info("User " + controller.getUserName() + " is logged out");
String userName = controller.getUserName();
ServletContext context = session.getServletContext();
Object obj = context.getAttribute("USERS");
if(obj instanceof Map){
Map map = (Map) obj;
map.remove(userName);
RWFacade rWFacade =(RWFacade)session.getAttribute("rWFacade");
rWFacade.closeFacade();
}
}
}
运行此代码时
session.invalidate();
没有被执行。我错过了什么?谢谢。