Envelope Interceptors
Last week, I implemented a simple interceptor and included it in a Mule service. This gives me the ability to intercept a message just before it is passed on to the component. Mule has two types of interceptors – the standard interceptor I talked about, and the Envelope Interceptor
Envelope interceptors get their name from the fact that they wrap themselves around the component (enveloping it
) and can intercept the message just before the component gets it and just after the component returns a result.
This is useful for certain tasks like profiling as it allows you to get the message before any of the outbound routers or transformations are invoked (or after the inbound routers and transformations).
I created the following envelope interceptor that you can use to see how they work:
public class RicstonEnvelope extends EnvelopeInterceptor { public void after(Invocation invocation) throws MuleException { try { invocation.getMessage().setPayload (invocation.getMessage(). getPayloadAsString()+" >"); } catch (Exception e) { e.printStackTrace(); } } public void before(Invocation invocation) throws MuleException { try { invocation.getMessage().setPayload("< "+ invocation.getMessage(). getPayloadAsString()); } catch (Exception e) { e.printStackTrace(); } } }
As you can see here, there are two methods (unlike the standard interceptor) which get invoked before and after the component – hence the names. Used in this way, any textual payload is going to be wrapped with the angle brackets (< and >) if we use a bridge component.
I added this interceptor to a Mule service:
<component> <custom-interceptor class="com.ricston.tests.RicstonEnvelope"/> <prototype-object class="com.ricston.tests.SimpleBridge"/> </component>
And ran the following tests to verify things:
public void testEnvelopeInterceptor () { MuleClient myClient; MuleMessage reply = null; String samplePayload = "Ricston"; try { myClient = new MuleClient (); myClient.dispatch("vm://envelope.test", samplePayload, null); reply = myClient.request("vm://envelope.return", 5000); } catch (MuleException e) { fail (e.getDetailedMessage()); } assertNotNull (reply); assertNotNull (reply.getPayload()); assertEquals ("< "+samplePayload+" >", (String)reply.getPayload()); }

If you enjoyed this post, make sure you subscribe to my RSS feed!
Tags: after, assertions, before, dispatch, envelopeInterceptor, FunctionalTestCase, interceptor, invocation, MuleClient, VM endpoints
September 12th, 2009 at 6:47 PM
[...] Read more here: Mule Envelope Interceptors | blog.ricston.com [...]
September 21st, 2009 at 10:03 AM
[...] Envelope Interc … 08/31 [...]