3 Replies Latest reply on Mar 22, 2010 12:21 AM by soares

    HTTP Session Listener

    soares

      Hi guys,


      I'm need to inject an EntityManager in my HTTPSessionListener class, because I need
      to update the database when some user has its session expired, So I'm using the
      sessionDestroyed method, to handle the database data, and to conclude it, I need to inject
      a EntityManager using @In annotation. I'm using Seam 2.2.0 with Tomcat 6. My code is bellow:


      Thank you!


      public class SessionListener implements HttpSessionListener
      {
      
          @In
          private EntityManager entityManager;
      
          public void sessionCreated(HttpSessionEvent se)
          {
              System.out.println("CREATED: " + se.getSession().getId());
          }
      
          public void sessionDestroyed(HttpSessionEvent se)
          {
              HttpSession session = se.getSession();
      
              System.out.println("DESTROYED: " + session.getId());
      
              Integer processUserId = (Integer) session
                      .getAttribute(UserAuthentication.LOGGED_PROCESS_USER_ID);
      
              if (entityManager != null && processUserId != null && processUserId > 0)
              {
                  System.out.println("UPDATE DATABASE!!");
                  ProcessUser processUser = entityManager
                          .find(ProcessUser.class, processUserId);
                  processUser.setOnline(false);
                  entityManager.merge(processUser);
              }
      
              String message = new StringBuffer("#### USER ID: " + processUserId).toString();
              System.out.println(message);
          }
      }
      

        • 1. Re: HTTP Session Listener
          soares

          I forgot to say: the injected EntityManager is always null. I think it happens because the Seam session is already dead... But I'd like to find a good way to update the database on session expires. Thanks!

          • 2. Re: HTTP Session Listener
            kirpi4ik

            Hi,



            You can create a Session scoped component and add the method annotated with @Destroy. In this method you can release your session-related objects(or whatever you need to do). This method will be invoked before your session is destroyed.




            with best regards,


            Dima

            • 3. Re: HTTP Session Listener
              soares

              Yeah, it should really works! thanks a lot Dima!