Fork me on GitHub

Interceptor Design Notes

YAAFI supports Dynamic Proxies (introduced with JDK 1.3) and Interceptors which was inspired by HiveMind. The intention for implementing these features is to provide better debugging and diagnostic support such as method invocation tracing, error logs or performance monitoring.

Dynamic Proxies and Interceptors for Rookies

A dynamic proxy is a dynamically created class implementing a list of interfaces and using an invocation handler. For the caller there is no difference between the real objext and the proxy object but invoking an interface method allows the invocation handler to delegate the method invocation to the real object. A dynamic proxy only intercepts methods defined in an interface but not abstract methods defined in a base class. This limitation is not relevant since you are accessing your services only by their interface, aren't you?!

Related Concepts

To make interceptors really useful some more infrastructure is needed

Item Description
AvalonInterceptorInvocationHandler Delegates the invocation of the dynamic proxy to the service instance and doing most of the work such as creating an AvalonInterceptorContext and invoking the registered interceptors in the correct order
AvalonInterceptorContext For each intercepted service invocation a new AvalonInterceptorContext is created which is passed to all registered interceptors. The AvalonInterceptorContext contains information about the service and method being invoked and allows access to a request context, a thread context using thread local storage and a transaction id.
BaseInterceptorServiceImpl Is the base class of all existing interceptors to factor out common functionality.

YAAFI and Interceptors

A few things to keep in mind when working with interceptors

  • Interceptors only work in conjunction with dynamic proxies. If you turn off the proxy creation no interceptor will be invoked for the service
  • Interceptors can be defined on the container level thereby applying the interceptors to all services
  • Interceptors can be alos defined on the service level thereby applying the interceptors to this service. The service inherits all interceptors defined on the container level.
  • Interceptors are implemented as Avalon services giving you access to all of the infrastructure and functionality such as dynamic reconfiguration
  • For an interceptor there is never an dynamic proxy created no matter what you define in role configuration file