2 Replies Latest reply on Apr 18, 2009 7:27 AM by Jabinara Begum

    Monitoring Session timeout

    Ricardo Memoria Newbie

      Hi all,


      I'm using SEAM 2.1.1.GA and my app track and store in the DB the moment user logout. I'm using the following code:


          /**
           * Logout the current user
           */
          @Observer("org.jboss.seam.preDestroyContext.SESSION")
          @Transactional
          public void logout() {
               onlineUsers.remove(userLogin);

              registerLogout();
          }


      But for some reason I really don't know, this method is not always called everytime the session expire. Some users remains in the system as it were forever.


      Do you know another way to track session timeout?


      Regards,
      Ricardo

        • 1. Re: Monitoring Session timeout
          Ricardo Memoria Newbie

          Forget about it... My mistake.

          • 2. Re: Monitoring Session timeout
            Jabinara Begum Newbie
            Hi.

            I am trying out the same thing, to catch the time stamp of session time out and persist it as logout time.  But in the method every object is becoming null so not able to get session object. I am doing like this:

            @Observer("org.jboss.seam.preDestroyContext.SESSION")
                 @Transactional(TransactionPropagationType.REQUIRED)
                 public void sessionTimeoutListener() {
                      System.out.println("Bingo !!!!");
                      HttpSession session = (HttpSession) this.ec.getSession(false);
                      String sessionId = session.getId();
                      em.joinTransaction();
                      List<SessionHistory> sessionHistoryList = em.createQuery("from SessionHistory sh where sh.sessionId = :sessionId order by sh.loginDateTime desc")
                           .setParameter("sessionId", sessionId)
                           .getResultList();
                      SessionHistory sessionHistory = sessionHistoryList.get(0);
                      sessionHistory.setLogoutDateTime(new Date());
                      em.persist(sessionHistory);
                      em.flush();
                      System.out.println("exiting sessiontimeout");
                 }

            For getting session object, I was trying to do in the famous way, using HttpSessionListener.  In this case I m getting the session object, but entity manager is becoming null. It is like this I was doing:
            public class SessionTimeOut implements HttpSessionListener{
                 @In
                 EntityManager em ;
                 public void sessionCreated(HttpSessionEvent arg0) {
                      
                 }
                 @Transactional(TransactionPropagationType.REQUIRED)
                 public void sessionDestroyed(HttpSessionEvent arg0) {
                      System.out.println("Bingo in http!!!!");
                      HttpSession session = arg0.getSession();
                      String sessionId = session.getId();
                      
                      em.joinTransaction();
                      List<SessionHistory> sessionHistoryList = em.createQuery("from SessionHistory sh where sh.sessionId = :sessionId order by sh.loginDateTime desc")
                           .setParameter("sessionId", sessionId)
                           .getResultList();
                      SessionHistory sessionHistory = sessionHistoryList.get(0);
                      sessionHistory.setLogoutDateTime(new Date());
                      em.persist(sessionHistory);
                      em.flush();
                      System.out.println("exiting sessiontimeout");
                      
                 }
            }

            The third way I applied, is using @WebRemote. In that case atleast I was getting an em from PersistenceContext by calling  (EntityManager)Component.getInstance("em");
            But then i am getting "no transaction is active".

            I know its lots of stuffs I have done, but whats the use if I m not getting the results.  Please somebody give an idea about this, before I break my head.
            Thanks