pak*_*ore 15 java jsf servlets
我的webapp有登录用户.暂停时间.在会话到期之前,我想执行一个方法来清理一些锁.
我实现了一个sessionListener
但是一旦我到达public void sessionDestroyed(HttpSessionEvent event)
会话已经不见了,我需要一些来自它的数据,所以我想FacesConfig.getCurrentInstance()
在会话实际到期之前执行一个方法(需要会话存活并且能够访问).
我怎样才能做到这一点?有任何想法吗?这是我的Session Listener:
public class MySessionListener implements HttpSessionListener {
private static final Logger log = LoggerFactory.getLogger(MySessionListener.class);
public MySessionListener() {
}
public void sessionCreated(HttpSessionEvent event) {
log.debug("Current Session created : "
+ event.getSession().getId()+ " at "+ new Date());
}
public void sessionDestroyed(HttpSessionEvent event) {
// get the destroying session...
HttpSession session = event.getSession();
prepareLogoutInfoAndLogoutActiveUser(session);
log.debug("Current Session destroyed :"
+ session.getId()+ " Logging out user...");
/*
* nobody can reach user data after this point because
* session is invalidated already.
* So, get the user data from session and save its
* logout information before losing it.
* User's redirection to the timeout page will be
* handled by the SessionTimeoutFilter.
*/
// Only if needed
}
/**
* Clean your logout operations.
*/
public void prepareLogoutInfoAndLogoutActiveUser(HttpSession httpSession) {
UserBean user = FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(FacesContext.getCurrentInstance(), "#{user}", UserBean.class);
LockBean lock = FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(FacesContext.getCurrentInstance(), "#{lock}", LockBean.class);
lock.unlock(user.getUsername());
log.info("Unlocked examination for user: "+user.getUsername());
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到的NullPointerException
是FacesContext.getCurrentInstance().getApplication()
因为要么getCurrentInstance
为null,要么getApplication
返回null
sta*_*ker 18
您可以通过实现HttpSessionBindingListener来实现这一点,您需要通过调用注册一个持有锁的会话registerSession
(可能不会更改字符串"sessionBindingListener").容器将valueUnbound()
在会话超时后和会话销毁之前回调该方法.
public class ObjectLock implements Serializable,HttpSessionBindingListener {
public void valueBound(HttpSessionBindingEvent event) {
log.info("valueBound:" + event.getName() + " session:" + event.getSession().getId() );
}
public void registerSession() {
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put( "sessionBindingListener", this );
log.info( "registered sessionBindingListener" );
}
public void valueUnbound(HttpSessionBindingEvent event) {
log.info("valueUnBound:" + event.getName() + " session:" + event.getSession().getId() );
// add you unlock code here:
clearLocksForSession( event.getSession().getId() );
}
}
Run Code Online (Sandbox Code Playgroud)
一个更优雅的解决方案
只需@PreDestroy
向Session-Bean 添加注释即可!如果Session将被销毁,它将事先在所有SessionBeans上调用PreDestroy,在那里你可以注销一切!
虽然目前这对许多ApplicationServers不起作用,但似乎是JSF规范中不明确的部分.因此,必须使用已接受的答案(HttpSessionBindingListener),直到@PreDestroy在所有服务器上按预期工作.