This page last changed on Nov 28, 2005 by rossmason.

Mule interceptors are useful for attaching common behaviour to multiple UMOs. The Interceptor or Command pattern is often referred to as practical AOP (Aspect Oriented Programming) as it allows the developer to intercept processing on an object and potentially alter the processing and outcome. Interceptors a very useful for attaching profiling, permission and security checks, etc, to a component in Mule.

Interceptor Types

Mule has two types of interceptors -

1. org.mule.interceptors.EnvelopeInterceptor - Envelope filter that will execute before and after the event is processed. Good for Logging and profiling.
2. org.mule.umo.UMOInterceptor - Simply gets invoked then forwards processing to the next element. An interceptor can stop further processing by not forwarding control to the next interceptor, for example as permissions checker interceptor.

Interceptor Event Flow

The following shows an example interceptor stack and the event flow.

Cannot resolve external resource into attachment.

Writing Interceptors

If you want to intercept an event flow to a component on the inbound event flow, you should implement the org.mule.umo.UMOInterceptor interface. it has a single method -

UMOMessage intercept(Invocation invocation) throws UMOException;

The invocation param contains the current event and the UMODescriptor object of the target component. Developers can extract the current UMOMessage from the event and manipulate it any way they wish. The intercept method must return a UMOMessage that will be passed on to the component (or the next interceptor in the chain).

The EnvelopInterceptor works in the same way except that it exposes two methods that get invoked before and after the event processing.

UMOMessage before(Invocation invocation) throws UMOException;

UMOMessage after(Invocation invocation) throws UMOException;

Halting Event Flow

If you wihc to halt the event flow you simply need to throw an exception. This will cause the ExceptionStrategy on the component to be invoked.

Configuring Interceptors

Interceptors can be configured on your components using the <interceptor> element i.e.

<mule-descriptor name="MyUMO" implementation="org.my.UMOService">
     ....
     <interceptor className="org.mule.interceptors.LoggingInterceptor"/>
</mule-descriptor>

You cal also define interceptor stacks; these are one or more interceptors that can be referenced using a logical name. To define an interceptor stack you must first configure it in the global section of the Mule Xml config file (above the <model> element) -

<interceptor-stack name="default">
    <interceptor className="org.mule.interceptors.LoggingInterceptor"/>
    <interceptor className="org.mule.interceptors.TimerInterceptor"/>
</interceptor-stack>

Then you can reference this stack on your components using -

<mule-descriptor name="MyUMO" implementation="org.my.UMOService">
     ....
     <interceptor name="default"/>
</mule-descriptor>

You can configure zero or more <interceptor> elements on your components and you can mix using explicit classnames or stack names.

Document generated by Confluence on Nov 27, 2006 10:27