5 Replies Latest reply on Nov 26, 2012 5:25 AM by Martin Kouba

    Injecting a session scope bean in to a HttpSessionListener?

    Tim Troup Newbie

      The log file of my Java EE 6 web application must make it easy to parse out a given users path through the application. To achieve this I tried to inject a @SessionScoped bean (SessionStore) in to a HttpSessionListener and then set the session id on this bean as follows:

       

      public class ServletSessionListener implements HttpSessionListener {

         

          @Inject

          Instance<SessionStore> sessionStore;

       

           public void sessionCreated(HttpSessionEvent se) {

              System.out.println(se.getSession().getAttributeNames());

              sessionStore.setSessionId(se.getSession().getId());

          }

       

      @Override

          public void sessionDestroyed(HttpSessionEvent se) {

       

          }

      }

       

      I then inject the session scoped bean (SessionStore) into an interceptor that deals with the cross cutting concern of logging:

       

      @Inject

          Instance<SessionStore> sessionStore;

         

          @AroundInvoke

          public Object log(InvocationContext ctx) throws Exception {

              //log method being invoked plus session id here...

       

              Object returnMe = ctx.proceed();

              return returnMe;

          }

       

      Sadly this doesn't work and I get a stack trace when navigating to my app hosted in Glassfish running Weld 1.1.8 final:

       

      java.lang.IllegalArgumentException: Should never reach here at org.apache.catalina.connector.SessionTracker.track(SessionTracker.java:168) at org.apache.catalina.connector.Request.doGetSession(Request.java:2939) at org.apache.catalina.connector.Request.getSession(Request.java:2583)

       

      My current hypothesis is that it is unwise or unsupported to inject something that is session scoped in to a servlet such as HttpSessionListener?