7 Replies Latest reply on Jul 13, 2006 10:44 AM by Jeremy Norris

    Interceptors not fired on methods called from within bean.

    Jeremy Norris Newbie

      I am using JBoss 4.0.4.GA (w/ EJB RC7).

      When I define interceptors (ie: @Interceptors(MyInterceptor.class)) on a class, the @AroundInvoke method is only called when a method is called from outside the bean . If a method is called from within the bean the @AroundInvoke method is not fired. Is this the desired behavior? If so, it doesn't seem to be mentioned in the spec.

        • 1. Re: Interceptors not fired on methods called from within bea
          andy Newbie

          this is correct behavior. it IS in the spec - don't ask me exactly where. there have been posts here already related to that.

          • 2. Re: Interceptors not fired on methods called from within bea
            Jeremy Norris Newbie

            Ok, I looked at the spec again, and it says it intercepts *business* methods (which are public, among other things I think). However, if I make the internal methods public, it still does not intercept them when they are called internally.

            • 3. Re: Interceptors not fired on methods called from within bea
              Peter Johnson Master

              Interceptors only work if you call the method via a bean reference (i.e., one you got from JNDI). By calling the method via a bean reference, the server has a chance to determine that an interceptor exists and can deflect the method call to the interceptor. But, within the bean, if you call another method dicrectly, that all gets compiled into the class file and the server does not have any chance to intercept the method call. To get the behavior you want, you need to investigate AOP, most likely AspectJ. (The comments above are based on my suppositions on how this feature works, not on knowledge of the actual implementation, so I could be wrong.)

              • 4. Re: Interceptors not fired on methods called from within bea
                Jeremy Norris Newbie

                 

                "PeterJ" wrote:
                Interceptors only work if you call the method via a bean reference (i.e., one you got from JNDI). By calling the method via a bean reference, the server has a chance to determine that an interceptor exists and can deflect the method call to the interceptor. But, within the bean, if you call another method dicrectly, that all gets compiled into the class file and the server does not have any chance to intercept the method call.


                Interesting. I was under the impression that is was implemented via byte code instrumentation by the underlying provider in the classloader, in the case of JBossAS, jboss-aop. Thus, there should be no real technical restrictions on when the method can be intercepted. The spec may have additional restrictions though.

                Is this bean reference restriction stuff mentioned in the spec? I didn't see anything like this under the Interceptors section.

                "PeterJ" wrote:
                To get the behavior you want, you need to investigate AOP, most likely AspectJ. (The comments above are based on my suppositions on how this feature works, not on knowledge of the actual implementation, so I could be wrong.)


                Thanks for the reply.

                • 5. Re: Interceptors not fired on methods called from within bea
                  Jeremy Norris Newbie

                   

                  "jnorris10" wrote:
                  Is this bean reference restriction stuff mentioned in the spec? I didn't see anything like this under the Interceptors section.


                  This stuff is definitely in the spec. PeterJ is right, Interceptors only work if you call the method via a bean reference. It's true that in theory you may not have this restriction with AOP weaving, but I don't think the spec wants to mandate AOP for EJB3 implementers.

                  • 6. Re: Interceptors not fired on methods called from within bea
                    Carlo de Wolf Master

                    Please try:

                    getBusinessObject(MyInterface.class).sayHelloTo("me");


                    • 7. Re: Interceptors not fired on methods called from within bea
                      Jeremy Norris Newbie

                       

                      "wolfc" wrote:
                      Please try:
                      getBusinessObject(MyInterface.class).sayHelloTo("me");


                      Yeah, I've found that, thanks. That call currently is "Not Implemented" (in EJB3 RC7) although it seems to be implemented in source control now.

                      For now I simply inject "myself" which works fine:

                      ie:
                      @EJB MyInterface myself
                      


                      Although this seems dirty and I'm not sure if this is really supposed to be supported in the spec.