Contact Us 1-800-596-4880

Using Interceptors

Mule interceptors are useful for attaching behaviors to multiple components. The interceptor 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. (See also Spring AOP ). Interceptors are very useful for attaching behavior such as profiling and permission and security checks to your components.

AbstractEnvelopeInterceptor is an envelope filter that executes before and after the component is invoked. Good for logging and profiling.

Interceptor Event Flow

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

interceptor

Writing Interceptors

If you want to intercept a message flow to a component on the inbound message flow, you should implement the Interceptor interface. It has a single method:

MuleEvent process(MuleEvent event);

The event parameter contains the current message. Developers can extract the current MuleMessage from the message and manipulate it as needed. The process method must return a MuleMessage that will be passed on to the component (or to the next interceptor in the chain).

The AbstractEnvelopeInterceptor works in the same way, except that it exposes two methods that get invoked before and after the event processing:

MuleEvent before(MuleEvent event) throws MuleException;

MuleEvent after(MuleEvent event) throws MuleException;

Configuring Interceptors

Interceptors can be configured on your components as follows:

<flow name="MyService">
    <custom-interceptor class="org.my.CustomInterceptor"/>
    <logging-interceptor/>
    <component>
        <interceptor-stack ref="testInterceptorStack"/>
    </component>
    <timer-interceptor/>
    <prototype-object class="org.my.ComponentImpl"/>
</flow>
When you configure interceptors, you must specify the object factory explicitly (in this example, <prototype-object>) instead of using the <component class> shortcut.

You can also define interceptor stacks , which are one or more interceptors that can be referenced using a logical name. To use an interceptor stack, you must first configure it in the global section of the Mule XML configuration file (above the <model> element):

<interceptor-stack name="default">
    <custom-interceptor class="org.my.CustomInterceptor"/>
    <logging-interceptor/>
</interceptor-stack>

You can configure multiple <interceptor> elements on your components, and you can mix using built-in interceptors, custom interceptors, and references to interceptor stacks.

Interceptor Configuration Reference

Logging Interceptor

The logging interceptor (ported from 1.x). Logging interceptor does not have attributes or child elements.

Custom Interceptor

A user-implemented interceptor.

Attributes of <custom-interceptor…​>

Name Type Required Default Description

class

class name

yes

An implementation of the Interceptor interface.

Child Elements of <custom-interceptor…​>

Name Cardinality Description

spring:property

0..*

Spring-style property element for custom configuration.

Interceptor stack

Attributes of <interceptor-stack…​>

Name Type Required Default Description

name

name

yes

The name used to identify this interceptor stack.

Child Elements of <interceptor-stack…​>

Name Cardinality Description

abstract-interceptor

0..1

A placeholder for an interceptor element.

Interceptor Stack

A reference to a stack of interceptors defined globally.

Attributes of <interceptor-stack…​>

Name Type Required Default Description

ref

string

yes

The name of the interceptor stack to use.

There are no child elements.

Timer Interceptor

The timer interceptor (ported from 1.x).

There are no attributes or child elements.