Tuesday, April 17, 2012

Spring has some little known built-in interceptor classes which can come in handy to enable method tracing at runtime. This will be useful to understand the system behavior if something goes wrong and one want to turn on tracing the flow of execution, even though there are not enough debug/trace information present in the code.

org.springframework.aop.interceptor package has some pre-built classes which can be easily configured to enable tracing and timing of method executions using AOP instrumentation.

SimpleTraceInterceptor: Will do method level tracing.

DebugInterceptor: Will provide the same information as SimpleTraceInterceptor with additional count indicating the number of times the method has been called.

PerformanceMonitorInterceptor: Logs the method execution timings.

There are few other built in interceptors for Jamon based performance monitoring, throttling the number of concurrent calls to the method etc. These interceptors are very simple, but can come in handy in several situations. I would explain how to configure such an interceptor in a Spring setup.

Spring Configuration

useDyamicLogger will log the trace/debug meesages under the logger defined for the intercepted method. For example, with useDyanmicLogger=true, the logs will be under the logger com.mycompany.service.MyService. Otherwise, the log will be under org.springframework.aop.DebugInterceptor.