应用程序关闭时清理JSF会话范围bean

时间:2012-08-21 06:58:53

标签: java jsf-2 session-bean

当JSF应用程序关闭或关闭时,我需要使活动会话对象无效并清理。以下是我在应用程序作用域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();

没有被执行。我错过了什么?感谢。

1 个答案:

答案 0 :(得分:1)

要在tomcat中重新启动时禁用会话持久性,您可以取消注释配置 $ TOMCAT_HOME / CONF / context.xml中

<!-- Uncomment this to disable session persistence across Tomcat restarts

    <Manager pathname="" /> -->

可能会帮助你。