Back Forum Reply New

Nested method calls not being intercepted

Hi all,
I've been banging my head against this problem for a few days and I've not gotten anywhere.  I've tried google and the spring reference manual and either the answer isn't there or it's hiding.

The problem is my interceptor doesn't seem to intercept nested calls to methods.

Basically I have created a business class, for example;

Code:
public class MyBusiness {
public void a() {   System.out.println(quot;doing aquot;); }  public void b() {   System.out.println(quot;doing bquot;);   a(); }

}
The intercepter is an implementation of MethodInterceptor and just has the following method;

Code:
public Object invoke(MethodInvocation method) throws Throwable {  System.out.println(quot;Intercepting quot; + method.getMethod().getName()); return method.proceed();

}
I've have tied the two together in a standalone swing application and I'm using the Spring AOP not AspectJ way of doing things.

Now considering the following test method;Code:
public void testInterception() { myBusiness.b();
}
I would have expected to see the following output;

Code:
Intercepting b
doing b
Intercepting a
doing a
But instead all I get is;

Code:
Intercepting b
doing b
doing a
It's as if the interceptor is too busy intercepting quot;bquot; that when quot;aquot; is called.  The interceptor seems to be working correctly otherwise since calling quot;a()quot; in the test method shows that the interceptor is being called.

The above example is just that, an example.  The problematic code is actually pretty long and being swing based there's a number of threads (including the EDT) floating around.  I've printed out the names of the threads alongside where I am calling the methods-to-be-intercepted and can confirm that the first method is called in the EDT, but the nested method is called in a different thread.  Could that be the problem?

Or am I just wrong in thinking that nested method calls will be intercepted?  I had thought that since the method calls are being nested the interceptor is already in an quot;interceptingquot; state when the nested method is actually invoked, hence no second intercept happens.  If that's the case, does anyone have any workarounds or refactoring suggestions?

Many thanks!

Tom

What you are trying does not work (unless you resort to using AspectJ).

See this thread for details.

Regards,
Andreas

Hi Tom

See also this thread...

sho...98amp;postcount=2

This is now documented in the Spring reference documentation, it wasn't hiding.

Cheers
Rick

Hi Rick and Andreas,

Many thanks for the help.  I understand the problem a lot better now.  I was looking here; docs/reference/aop.html and couldn't find the information that was included in Rick's zip.

I guess the best thing for me to do is refactor my code so it doesn't have any self calls.  That's going to be tricky since I'm trying to use interceptors as a replacement (or alternative implementation of) the Event Listener pattern.

The scenario is, I have a time consuming task which eventually finishes and then set's a property.  Usually I would then have a fireEvent() method call in my setter.  What I wanted to do was use the interceptor to catch the call to the setter and have the interceptor inform the listeners - hence remove all the event-based boilerplate code out of class.

I guess AspectJ would be an alternative option to refactoring.  Just have to work out which is best now, I suppose.

Thanks again for your quick and non-quot;RTFM moron!quot; help.

Tom

P.S. I remember seeing Rick's post before, but I couldn't download the zip since I didn't have an account.  Then the activation email got caught by my spam filter which doesn't tell me about any spam until the day after it gets sent.  (Don't ask).  And then when I finally got my login working, I forgot about it...
¥
Back Forum Reply New