3 Replies Latest reply on Dec 2, 2006 12:48 PM by Chris Lowe

    Injection at destroy time

    Chris Lowe Apprentice

      I have some stateless beans injected into a stateful bean that are only required at destroy time. Currently when destroy is invoked, these beans are null. Code is as follows:

      @Name("sessionTimeoutHandler")
      @Stateful
      @Scope(ScopeType.SESSION)
      public class SessionTimeoutHandlerHome implements SessionTimeoutHandler, Serializable
      {
       private static final long serialVersionUID = 1L;
      
       @Logger
       private Log log;
      
       @In(create=true)
       private SessionDao sessionDao; // this is stateless
      
       @In(create=true)
       private AuditLog auditLog; // and so is this
      
       private Session currentSession;
      
       ...
      
       @Remove @Destroy
       public void destroy() {
       // Session has timed out, so update the user session entry and perform
       // cleanup.
       currentSession.setClosed(new Date());
      
       sessionDao.merge(currentSession);
      
       auditLog.addAuditEntry("User session timeout for session id #0" currentSession.getId());
      
       log.info("User session timeout for session id #0" currentSession.getId());
       }
      }


      Should I be getting the stateless beans injected at this point? I'm sure I've seen this working in the past.

      When creating the bean I did notice what I believe to be inconsistent behaviour with respect to @Logger injection. I initially created the bean using method 1 below, by simply specifying the bean as an @Out parameter and instantiating the object within an action. Using this method, the @Logger object on SessionTimeoutHandler was null when @Destroy was called. However, when I changed to method 2 the logger was available - but still no stateless beans.
      Creation Method 1 - Using @Out
      @Name("login")
      @Stateful
      @Scope(ScopeType.EVENT)
      public class LoginAction implements Login, Serializable
      {
       @Out(required=false)
       private SessionTimeoutHandler sessionTimeoutHandler;
      
      
       public String login() {
       ...
      
       SessionTimeoutHandler sessionTimeoutHandler = new SessionTimeoutHandler();
      
       // create a session timeout handler
       sessionTimeoutHandler.setCurrentSession(currentSession);
       }
      }
      
      
      Creation Method 2 - Using @In(create = true)
      @Name("login")
      @Stateful
      @Scope(ScopeType.EVENT)
      public class LoginAction implements Login, Serializable
      {
       @In(create = true)
       @Out(required = false)
       private SessionTimeoutHandler sessionTimeoutHandler;
      
      
       public String login() {
       ...
      
       // create a session timeout handler
       sessionTimeoutHandler.setCurrentSession(currentSession);
       }
      }


      I am using Seam 1.1 CR2.