java 过期的方法_java – 在会话过期之前调用方法

我的webapp有登录用户.有一个超时.在会话过期之前,我想执行一个方法来清理一些锁.

我实现了一个sessionListener,但是一旦我达到了public void sessionDestroyed(HttpSessionEvent事件),会话已经消失了,我需要一些数据,所以我想执行一个方法(需要会话活动并且能够访问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());

}

}

但是我在FacesContext.getCurrentInstance().getApplication()中得到NullPointerException,因为getCurrentInstance为null或getApplication返回null

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值