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

    HTTP Session Listener

    soares gouveia Newbie

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