4 Replies Latest reply on Sep 19, 2012 8:34 AM by Ste Gr

    [CDI Interceptor] Recursive calls possible?

    Ste Gr Newbie

      Hi,

       

      I have an question about CDI interceptors which I cant' answer myself.

      I have implemented two interceptors which use the same annotation. The annotation contains a parameter to decide which interceptor should be used. If I call a method on the managed bean, the interceptor runs as excpected. But if i do a call to another method of the same bean (the bean is calling a method of himself) no additional interceptor is executed.

       

      Is this the desired behaviour of CDI interceptors?

      • Application calls bean method => interceptor runs
      • Application calls bean method which itself calls a bean mthod => only the first method will be intercepted but not the second one with the same/different interceptor
      • Application calls bean method which calls itself recursively => didn't test it, but should this be possible?

       

      Thanks

        • 1. Re: [CDI Interceptor] Recursive calls possible?
          Martin Kouba Master

          Hi,

          only invocation of a method of a bean via a contextual reference or business method of a session bean via an EJB remote or local reference is intercepted (it is called business method invocation).

           

          In other words what you describe is expected behaviour (if I understand your use-case correctly ;-).

           

          public class Bar {
            
            @Inject
            Foo foo;
          
            public void pingFoo() {
              // This is the invocation via the contextual reference foo - is intercepted
              foo.ping(); 
            }
          
          }
          
          @MyInterceptorBinding
          public class Foo {
          
            public void ping() {
              // This is NOT the invocation via the contextual reference - it is not intercepted
              pong();
            }
          
            public void pong() {
            }
          }
          

          WRT recursive calls -> it is possible (and again it's not business method invocation) but will likely result in StackOverflowError

          1 of 1 people found this helpful
          • 2. Re: [CDI Interceptor] Recursive calls possible?
            Ste Gr Newbie

            Hi,

             

            thanks for your response. You got it as i meant it   Is there any way to achieve what I want? Any extension or something like this?

             

            It is not really required, but would allow me to implement a more flexible interceptor for my use-case. The only workaround I've found is to get a reference to the same bean via the BeanManager. But that is not really a nice approach.

             

             

            @contextual reference: When I print out the objects (the injected foo and <this> in Foo), I get the same output. But <this> will reference the pure POJO without any CDI specifics?

            • 3. Re: [CDI Interceptor] Recursive calls possible?
              Martin Kouba Master

              Hm, I don't know of any such extension... Also this could be tricky to implement. I'd rather find another way (maybe change app desing :-).

               

              WRT contextual reference -> it depends on the bean's scope. If the bean has a normal scope, then the contextual reference for the bean is a client proxy (= not the same object). If the bean has a pseudo-scope (dependent = Foo in our example) you get the reference to the bean instance (the same obj).

              • 4. Re: [CDI Interceptor] Recursive calls possible?
                Ste Gr Newbie

                It would only be a nice-to-have-feature. No need to change the app design

                 

                Thanks for your explanations!