我的web應用程序有登錄的用戶。有一個超時。在會話過期之前,我想執行一個方法來清理一些鎖。會話過期前的調用方法
我已經實現了一個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
返回null
工程就像一個魅力。 +1並被接受。 – pakore 2010-10-15 15:17:26
我把它放在'UserBean'類下,它知道'LockBean'。我已經通過以前的問題實現了這個問題...... BalusC:D,所以我只需要添加'lock.unlock'行並且它工作。謝謝你們。 – pakore 2010-10-15 15:19:29
實現'HttpSessionBindingListener'確實更好。您只需確保在JSF請求期間調用registerSession()(但是,如果ObjectLock已經是會話範圍的託管bean,那麼該方法是無用的 – BalusC 2010-10-15 15:22:36