代码之家  ›  专栏  ›  技术社区  ›  pakore

在会话到期前调用方法

  •  15
  • pakore  · 技术社区  · 14 年前

    我已经实现了 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());
        }
    }
    

    NullPointerException FacesContext.getCurrentInstance().getApplication() getCurrentInstance 为空或 getApplication 返回空值

    2 回复  |  直到 9 年前
        1
  •  18
  •   stacker    14 年前

    通过实现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() );
        }
    }
    
        2
  •  5
  •   Falco    9 年前

    一个更优雅的解决方案:

    @PreDestroy 会话Bean的注释!如果会话将被破坏,它将提前调用所有会话bean上的PreDestroy,在那里您可以注销和所有内容!