Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
An interceptor class is a class, distinct from the bean class itself, whose methods are invoked in response to business method invocations and life cycle events on the bean. You can associate a bean class can with any number of interceptor classes.
You can associate an interceptor class with an EJB 3.0 message-driven bean.
To configure an EJB 3.0 message-driven bean with an interceptor class, you must do the following:
Create an interceptor class (see "Creating an Interceptor Class").
This can be any POJO class.
Implement interceptor methods in the interceptor class.
An interceptor method has the following signature:
Object <METHOD>(InvocationContext) throws Exception
An interceptor method may have public, private, protected, or package level access, but must not be declared as final or static.
You can annotate an interceptor method as a life cycle callback (see "Configuring a Life Cycle Callback Interceptor Method on an Interceptor Class of an EJB 3.0 MDB") or as an AroundInvoke
method (see "Configuring an Around Invoke Interceptor Method on an Interceptor Class of an EJB 3.0 MDB").
Associate the interceptor class with your EJB 3.0 message-driven bean (see "Associating an Interceptor Class With an MDB").
Optionally configure the message-driven bean to use singleton interceptors (see "Specifying Singleton Interceptors in an MDB").
This section describes the following:
Example 10-13 shows how to specify an AroundInvoke
interceptor method and a life cycle callback interceptor method in an interceptor class for an EJB 3.0 message-driven bean. After you associate this interceptor class with a message-driven bean (see Example 10-14), each time the onMessage
method is invoked, OC4J intercepts the invocation and invokes the AroundInvoke
method myInterceptor
. When the appropriate life cycle event occurs, OC4J invokes the corresponding life cycle callback interceptor method such as myPreDestroyMethod
.
Example 10-13 Interceptor Class
public class MyInterceptor { ... @AroundInvoke protected Object myInterceptor(InvocationContext ctx) throws Exception { Principal p = ctx.getEJBContext().getCallerPrincipal; if (!userIsValid(p)) { throw new SecurityException( "Caller: '" + p.getName() + "' does not have permissions for method " + ctx.getMethod() ); } return ctx.proceed(); } @PreDestroy public void myPreDestroyMethod (InvocationContext ctx) { ... ctx.proceed(); ... } }
You can associate an interceptor class with an EJB 3.0 message-driven bean using the @Interceptors
annotation. Example 10-14 shows how to associate the interceptor class from Example 10-13 with an EJB 3.0 message-driven bean class.
Note that the life cycle method for @PostConstruct
is a method of the EJB 3.0 message-driven bean class itself (for more information, see "Configuring a Life Cycle Callback Interceptor Method on an EJB 3.0 MDB") while the life cycle method for @PreDestroy
is a life cycle callback interceptor method on the interceptor class associated with this message-driven bean (see "Configuring a Life Cycle Callback Interceptor Method on an Interceptor Class of an EJB 3.0 MDB").
Example 10-14 Associating an Interceptor Class With an EJB 3.0 MDB
@MessageDriven @Interceptors(MyInterceptor.class) public class MessageLogger implements MessageListener { @Resource javax.ejb.MessageDrivenContext mc; public void onMessage(Message message) { .... } @PostConstruct public void initialize() { items = new ArrayList(); } ... }
As Example 10-15 shows, you can configure OC4J to use singleton interceptor classes by setting the @MessageDrivenDeployment
attribute interceptorType to singleton
. All instances of this message-driven bean will share the same instance of MyInterceptor
. The MyInterceptor
class must be stateless.
For more information about this attribute, see Table A-3. For more information on singleton interceptors, see "Singleton Interceptors".
Example 10-15 Specifying a Singleton Interceptor Class With an EJB 3.0 MDB
@MessageDriven @MessageDrivenDeployment(interceptorType="singleton") @Interceptors(MyInterceptor.class) public class MessageLogger implements MessageListener { @Resource javax.ejb.MessageDrivenContext mc; public void onMessage(Message message) { .... } @PostConstruct public void initialize() { items = new ArrayList(); } ... }