| Ejb interceptors in depth |
|
|
|
| Written by F.Marchioni | |||||
| Monday, 23 March 2009 11:12 | |||||
|
EJB 3.0 interceptors provide the bean developer with fine grained control over the method invocation flow. In this tutorial we'll see from practical examples how it can we can take advantage of it.
An interceptor is a method that you can interpose in the invocation flow of an enterprise bean. You can define an interceptor to intercept an enterprise bean's business methods: the interceptor method will be executed before any of the bean's business methods are invoked. The great benefit of interceptors is that they give you a way to add functionality to your business methods without modifying the methods' code.
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import org.jboss.ejb3.annotation.RemoteBinding;
@Stateless(name="SampleEJB")
@RemoteBinding(jndiBinding="SampleEJB")
@Interceptors(value=com.sample.MyInterceptor.class)
public class SampleBean implements Sample {
public void doSomething(String param) {
callWebService(param);
}
}
In this class we've declared a custom interceptor named com-sample.MyInterceptor :
package com.sample;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class MyInterceptor {
@AroundInvoke
public Object log(InvocationContext ctx) throws Exception
{
System.out.println("*** TracingInterceptor intercepting " + ctx.getMethod().getName());
long start = System.currentTimeMillis();
String param = (String)ctx.getParameters()[0];
if (param == null)
ctx.setParameters(new String[]{"default"});
try
{
return ctx.proceed();
}
catch(Exception e)
{
throw e;
}
finally
{
long time = System.currentTimeMillis() - start;
String method = ctx.getClass().getName();
System.out.println("*** TracingInterceptor invocation of " + method + " took " + time + "ms");
}
}
}
How does it work ? when a method of your SampleEJB is invoked, the Interceptor is invoked. The interceptors has full access to the method name invoked and its parameter.
On what kind of components you can apply Interceptors ? They can be used on stateless session beans, stateful session beans, and message driven beans. How many kind of Interceptors can you apply ? You can apply Interceptors at three different level: 1) Default interceptors: These interceptors needs to be declared in your ejb-jar.xml and are valid across all your EJB deployed :
<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>sample.interceptor.MyDefaultInterceptor</interceptor-class>
</interceptor-binding>
...
</assembly-descriptor>
2) Class level interceptors: This is the kind of interceptor we've seen in our example: it is valid across all methods of an EJB. @Stateless 3) Method level interceptors:
Interceptors in the Bean class: Interceptors do not need to be written in a separate file: you can code interceptors in the EJB class as well:
@Stateless(name="SampleEJB")
@RemoteBinding(jndiBinding="SampleEJB")
@Interceptors(value=com.sample.MyInterceptor.class)
public class SampleBean implements Sample {
public void doSomething(String param) {
callWebService(param);
}
@AroundInvoke
public Object log(InvocationContext ctx) throws Exception
{
try
{
return ctx.proceed();
}
catch(Exception e)
{
throw e;
}
}
}
How do you exclude the DefaultInterceptor ?
@ExcludeDefaultInterceptors
public class TestEJB implements Test
{
@AroundInvoke
public Object customInterceptor(InvocationContext ctx) throws Exception
{
System.out.println("*** CustomInterceptor intercepting");
return ctx.proceed();
}
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated on Sunday, 25 July 2010 13:43 |






