【发布时间】:2011-04-25 23:53:05
【问题描述】:
我的 webapp 有登录的用户。有超时。在会话到期之前,我想执行一个方法来清理一些锁。
我已经实现了sessionListener,但是一旦我到达public void sessionDestroyed(HttpSessionEvent event),会话就已经消失了,我需要从中获取一些数据,所以我想执行一个方法(它需要会话处于活动状态并且能够访问FacesConfig.getCurrentInstance()) 在会话实际过期之前。
我该怎么做?有任何想法吗?这是我的会话监听器:
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());
}
}
但我在FacesContext.getCurrentInstance().getApplication() 得到NullPointerException,因为getCurrentInstance 为空或getApplication 返回空
【问题讨论】: