在会话到期之前调用方法 [英] Invoke method just before session expires

查看:133
本文介绍了在会话到期之前调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络应用已登录用户。暂停时间。在会话到期之前,我想执行一个方法来清理一些锁。

My webapp has users who login. There is a timeout. Before the session expires, I would like to execute a method to cleanup some locks.

我实现了一个 sessionListener 但是一旦我到达 public void sessionDestroyed(HttpSessionEvent事件)会话已经消失,我需要一些数据,所以我想执行一个方法(需要会话存活并且能够在会话实际到期之前访问 FacesConfig.getCurrentInstance()

I've implemented a sessionListener but once I reach public void sessionDestroyed(HttpSessionEvent event) the session is already gone and I need some data from it, so I would like to execute a method (which needs the session alive and be able to access FacesConfig.getCurrentInstance()) before the session is actually expired.

怎么能我这样做?有任何想法吗?这是我的会话监听器:

How can I do that? Any ideas? This is my 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());
    }
}

但我得到 NullPointerException at FacesContext.getCurrentInstance()。getApplication()因为 getCurrentInstance 为null或 getApplication 返回null

But I'm getting NullPointerException at FacesContext.getCurrentInstance().getApplication() because either getCurrentInstance is null or getApplication returns null

推荐答案

您可以通过实现HttpSessionBindingListener来实现这一点需要通过调用 registerSession 来注册一个持有锁的会话(字符串sessionBindingListener可能不会被更改)。会话超时后,容器将回调 valueUnbound()方法, 会话被销毁。

You can achieve that by implementing a HttpSessionBindingListener you need to register a session which holds a lock by calling registerSession (the string "sessionBindingListener" may not be changed). The container will callback the valueUnbound() method after the session timed out and before the session is destroyed.

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() );
    }
}

这篇关于在会话到期之前调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆