Thursday, July 14, 2011

Mule - Interceptors

Mule provides interceptors to process the MuleMessage before being processed by the component. The intercepts can also be configured to stop the flow to the component. There are some predefined interceptors provided by Mule that can be identified in MULE INTERCEPTORS.

The following is a simple example on defining a custom interceptor that can perform tasks around the component. This example simply logs the message payload before and after the execution of the component.

Interceptor Class
The Envelope Interceptor that comes by default with Mule package provides two methods namely before and after. These methods get executed before and after the execution of the component to which they are applied.

package org.mybusiness.interceptors;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mule.api.interceptor.Invocation;
import org.mule.interceptor.EnvelopeInterceptor;


public class CustomLoggingInterceptor extends EnvelopeInterceptor {


private static final Log LOG = LogFactory
.getLog(CustomLoggingInterceptor.class);


@Override
public void after(Invocation event) {
LOG.info("------------------------------------------------");
LOG.info("The output message is " + event.getMessage().getPayload());
LOG.info("------------------------------------------------");
}


@Override
public void before(Invocation event) {
LOG.info("------------------------------------------------");
LOG.info("The input message is " + event.getMessage().getPayload());
LOG.info("------------------------------------------------");
}
}

mule-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.2"
    xmlns:file="http://www.mulesource.org/schema/mule/file/2.2"
    xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2" xmlns:smtp="http://www.mulesource.org/schema/mule/smtp/2.2"
    xmlns:http="http://www.mulesource.org/schema/mule/http/2.2"
    xmlns:spring="www.springframework.org/schema/beans"
    xsi:schemaLocation="
       http://www.mulesource.org/schema/mule/core/2.2 http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
       http://www.mulesource.org/schema/mule/stdio/2.2 http://www.mulesource.org/schema/mule/stdio/2.2/mule-stdio.xsd
       http://www.mulesource.org/schema/mule/file/2.2 http://www.mulesource.org/schema/mule/file/2.2/mule-file.xsd
       http://www.mulesource.org/schema/mule/vm/2.2 http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd
       http://www.mulesource.org/schema/mule/smtp/2.2 http://www.mulesource.org/schema/mule/smtp/2.2/mule-smtp.xsd
       http://www.mulesource.org/schema/mule/http/2.2 http://www.mulesource.org/schema/mule/http/2.2/mule-http.xsd
       www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans-2.5.xsd">


    <stdio:connector name="stdioInConnector"
        outputMessage="Data : " promptMessage="Enter your name : " />


    <model>
        <service name="InputService">
            <inbound>
                <stdio:inbound-endpoint system="IN"
                    connector-ref="stdioInConnector" synchronous="true" />
            </inbound>
            <outbound>
                <pass-through-router>
                    <vm:outbound-endpoint path="processMessage" />
                </pass-through-router>
            </outbound>
        </service>


        <service name="ProcessService">
            <inbound>
                <vm:inbound-endpoint path="processMessage" />
            </inbound>
            <component class="org.mybusiness.components.ProcessInputComponent">
                <custom-interceptor
                    class="org.mybusiness.interceptors.CustomLoggingInterceptor" />
            </component>
            <outbound>
                <pass-through-router>
                    <stdio:outbound-endpoint
                        system="OUT" />
                </pass-through-router>
            </outbound>
        </service>
    </model>
</mule>

Sample Output

Enter your name : vijay
2011-07-14 14:47:09 VMMessageDispatcher [INFO] Connected: endpoint.outbound.vm://processMessage
2011-07-14 14:47:09 CustomLoggingInterceptor [INFO] ------------------------------------------------
2011-07-14 14:47:09 CustomLoggingInterceptor [INFO] The input message is vijay
2011-07-14 14:47:09 CustomLoggingInterceptor [INFO] ------------------------------------------------
Here...
2011-07-14 14:47:09 CustomLoggingInterceptor [INFO] ------------------------------------------------
2011-07-14 14:47:09 CustomLoggingInterceptor [INFO] The output message is The data entered is : vijay
The length of the data is : 5
2011-07-14 14:47:09 CustomLoggingInterceptor [INFO] ------------------------------------------------
2011-07-14 14:47:09 StdioMessageDispatcher [INFO] Connected: endpoint.outbound.stdio://system.out
Data : The data entered is : vijay
The length of the data is : 5
Enter your name :

No comments:

Post a Comment