Java Reference
In-Depth Information
Object interceptedObject =
invocationContext.getTarget();
Method interceptedMethod =
invocationContext.getMethod();
System.out.println("Entering " +
interceptedObject.getClass().getName() + "." +
interceptedMethod.getName() + "()");
Object o = invocationContext.proceed();
System.out.println("Leaving " +
interceptedObject.getClass().getName() + "." +
interceptedMethod.getName() + "()");
return o;
} }
The above example sends a message to the application server log just before and just
after an intercepted method is executed. The purpose of implementing something
like this would be to aid in debugging applications.
For simplicity, the above example simply uses System.out.
println to output messages to the application server log. A real
application more than likely would use a logging API such as the
Java Logging API or Log4j.
The first thing we do in our interceptor method is to obtain a reference to the object
and method being intercepted. We then output a message to the log indicating
the class and method being invoked, this code is executed just before we let the
intercepted method execute, which we do by invoking invocationContext. pro-
ceed() . We store the return value of this method in a variable, and then add some
additional logic to be executed just after the method finishes. In our example, we
simply send an additional line of text to the application server log. Finally our
method returns the return value of invocationContext.proceed() .
Decorating the EJB with the @Interceptors
annotation
In order for an EJB's method to be intercepted, it must be decorated with the
@Interceptors annotation, this annotation has a single class array attribute.
This attribute contains all the interceptors to be executed before and/or after
the method call.
 
Search WWH ::




Custom Search