Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Configuring an Interceptor on an EJB 3.0 MDB Message Listener Method

You can designate one non-business method as the interceptor method for a message-driven bean (see "Using Annotations"). The method must have a signature of:

    public Object <MethodName>(InvocationContext) throws Exception

For more information, see "Understanding EJB 3.0 Interceptors".

## Using Annotations

Example 10-3 shows how to designate a method of a message-driven bean class as an interceptor method using the `@AroundInvoke` annotation. Each time the `onMessage` method is invoked, OC4J intercepts the invocation and invokes the interceptor method `myInterceptor`. The `onMessage` method invocation proceeds only if the interceptor method returns `InvocationContext.proceed()`.

Example 10-3 @AroundInvoke in an EJB 3.0 Message-Driven Bean

    @MessageDriven
    public class MessageLogger implements MessageListener
    {
        @Resource javax.ejb.MessageDrivenContext mc;

        public void onMessage(Message message)
        {
        ....
        }

        @AroundInvoke
        public Object myInterceptor(InvocationContext ctx) throws Exception
        {
            if (!userIsValid(ctx. getEJBContext().getCallerPrincipal()))
            {
                throw new SecurityException(
                    "Caller: '" + ctx.getEJBContext().getCallerPrincipal().getName() +
                    "' does not have permissions for method " + ctx.getMethod()
                );
            }
            return ctx.proceed();
        }
    }