Interceptors
Interceptors provide a mechanism by which you can interfere with the message flow in Mule. This makes them rather powerful and dangerous unless you know what you’re doing.
You can create an interceptor of your own by implementing the Interceptor interface. This interface provides a single method – intercept (Invocation invocation) – that is called whenever a message is about to be handed over to the component.
The intercept () method receives an Invocation which contains information about the current event context as well as the message itself. Inside the interceptor, you can modify the MuleMessage directly and even create a completely new one. What’s important is that you return a MuleMessage. This returned value is what is then passed on to the component, so if you don’t return anything, the component will not be invoked.
Take a look at this simple interceptor:
public class HelloInterceptor implements Interceptor { public MuleMessage intercept(Invocation invocation) throws MuleException { if (invocation.getMessage().getPayload() != null) { return new DefaultMuleMessage ("Hello "+(String) invocation.getMessage().getPayload()); } return invocation.getMessage(); }
In this interceptor, I’m merely prepending the word “Hello ” to any string payload that is received. I also create a new MuleMessage each time but this is not necessary; I could manipulate the current one and return that instead.
I added this to my Mule configuration like so:
<service name="Interceptor"> <inbound> <inbound-endpoint ref="fromTestCase"/> </inbound> <component> <custom-interceptor class="com.ricston.tests.HelloInterceptor"/> <prototype-object class="com.ricston.tests.SimpleBridge"/> </component> </service>
As you can see here, the declaration for the interceptor is listed within the component XML elements. Also note that I do not fill in the class attribute for the component element – since I’m using interceptors, I must declare the component factory I want to use explicitly too. The default one is the prototype-object and is used here.

If you enjoyed this post, make sure you subscribe to my RSS feed!
Tags: component factory, interceptor, invocation, MuleMessage, prototype-object
August 31st, 2009 at 10:02 AM
[...] Interceptors 08/24 [...]
September 8th, 2009 at 3:18 PM
Hello,
i’m using actually mule 2.2.1, and i’m trying to use also interceptors;
I have a question:
what does SimpleBridge refer to ? or is it a simple ‘name’ ..
thanks
September 8th, 2009 at 3:51 PM
Hello there,
“SimpleBridge” is just a simple bridge
In other words it is a simple POJO that will just return the data that it receives and therefore act as a bridge. Similar to an echo component really.
Thanks for reading
Antoine
September 12th, 2009 at 6:50 PM
[...] Continued here: Interceptors | blog.ricston.com [...]
September 14th, 2009 at 10:06 AM
[...] Interceptors 08/24 [...]