• Subscription Options

  • What I write about

  • RSS Latest News From MuleSoft

  • Archives

  • Technical Features

  • Visitors Online

  • 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: , , , ,

    5 Responses to “Interceptors”

    1. Mule Envelope Interceptors | blog.ricston.com Says:

      [...] Interceptors 08/24 [...]

    2. Slim Says:

      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

    3. Antoine Borg Says:

      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

    4. Interceptors | blog.ricston.com mule Says:

      [...] Continued here: Interceptors | blog.ricston.com [...]

    5. Stacks of Interceptors | blog.ricston.com Says:

      [...] Interceptors 08/24 [...]

    Leave a Reply

    Add video comment
    © Copyright 2005-2008 Ricston, All Rights Reserved
     Sitemap   Privacy Policy    Legal