2

I am working with Guice's method interception feature. What I need to know is how to properly implement multiple interceptors, of the form:

this.bindInterceptor(Matchers.any(), Matchers.any(), new Interceptor1(), new Interceptor2());

Specifically, if there is an invocation of proceed() in both interceptors, what happens? Does the intercepted method get called twice? Or does the proceed() in the first interceptor invoke the second interceptor, which then invokes the method? Or should only one interceptor have a proceed()?

Thanks

  • possible duplicate of [How to define order of method interceptors in Guice?](http://stackoverflow.com/questions/8308203/how-to-define-order-of-method-interceptors-in-guice) – condit Jul 22 '14 at 16:58
  • I understand the ordering of interceptors - what I don't know is when the method itself should be fired by proceed(). – David Stowell Jul 22 '14 at 18:23

1 Answers1

3

Both interceptors can (and should) call proceed. This way they can be used as independent aspects (i.e. transactions and logging). In fact if you don't call proceed from your outer interceptor then the next interceptor will not fire.

The method interceptors will be called in a stack-like fashion based on the order of the bindInterceptor calls. In your example it would look like this:

Interceptor1 entry
Interceptor1 proceed
  Interceptor2 entry
  Interceptor2 proceed
    Method
  Interceptor2 exit
Interceptor1 exit
condit
  • 10,852
  • 2
  • 41
  • 60