[英]Cleanup JSF session scope bean when application shutdown
I need to invalidate and cleanup active session objects when the JSF application shutdown or tomact shutdown. 当JSF应用程序关闭或tomact关闭时,我需要使活动会话对象无效并进行清理。 The following is the code I have written in application scoped bean
以下是我在应用程序范围的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();
}
}
and the following is overwritten HttpSessionListener 而以下是覆盖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();
}
}
}
when run this code 运行此代码时
session.invalidate();
is not getting executed. 没有得到执行。 I missed anything ?
我错过了什么? Thanks.
谢谢。
To disable session persitence across restart in tomcat you can uncomment a config in $TOMCAT_HOME/conf/context.xml 要在重新启动tomcat中禁用会话持久性,可以在$ TOMCAT_HOME / conf / context.xml中取消注释配置
<!-- Uncomment this to disable session persistence across Tomcat restarts
<Manager pathname="" /> -->
May be it will help you. 可能会对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.