1 Reply Latest reply on Nov 5, 2012 1:05 PM by robbatt

    Accessing SessionContext from binded CDI interceptor

    oscarcs

      Hi all,

       

      Does someone know how to access the EJB SessionContext from a CDI interceptor.

       

      I can access it using a classic EJB interceptor, for example:

      public class MyInterceptor {
          @Resource
          private SessionContext sessionContext;    
              
          @AroundInvoke
          public Object doIt(final InvocationContext ctx) throws Exception {
               ...
          }
       }
      

      and then at the session bean:

      @Stateless
      @Local(MyServiceLocal.class)
      @Remote(MyServiceRemote.class)
      @Interceptors(MyInterceptor.class)
      public class MyServiceBean implements MyServiceLocal, MyServiceRemote {
           ...
      }
      

      This works OK but now I want to use a CDI interceptor binding to be able set some parameters in the bean annotation or, even better, make my own CDI stereotype, so the first step consists in changing @Interceptors annotation with my own binding:

      @Stateless
      @Local(MyServiceLocal.class)
      @Remote(MyServiceRemote.class)
      @MyInterceptorBinding
      public class MyServiceBean implements MyServiceLocal, MyServiceRemote {
      
           ...
      } 
      

      Creating the binding annotation:

      @InterceptorBinding
      @Target({METHOD, TYPE})
      @Retention(RUNTIME)
      public @interface MyInterceptorBinding {
      }
      

      Modifying the interceptor implementation as:

      @Interceptor
      @MyInterceptorBinding
      public class MyInterceptor {
          @Resource
          private SessionContext sessionContext;    
              
          @AroundInvoke
          public Object doIt(final InvocationContext ctx) throws Exception {
               ...
          }
       }
      

      Finally i have added the interceptor implementation to beans.xml.

       

      When I execute the application I get a JNDI name not found exception as its trying to access a wrong JNDI name something like "java:\enc\MyInterceptor".

       

      If I change from @Resource to @Inject to get de SessionContext, then I get a CDI bean not found exception.

       

      My be I am wrong but it seems that when you start mixing CDI and EJB features you are lost.

       

      Can anyone offer me some advice in this specific problem?

       

      Thanks in advance.