在应用程序关闭时清理 JSF 会话作用域 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();

未被执行。我错过了什么?谢谢。

要在 tomcat 中重新启动时禁用会话持久性,您可以在$TOMCAT_HOME/conf/context.xml

<!-- Uncomment this to disable session persistence across Tomcat restarts
    <Manager pathname="" /> -->

也许它会帮助你。

相关内容

  • 没有找到相关文章

最新更新