Mule enricher as the name suggests helps use to enrich the existing message. For example, we can enrich the payload, or add new headers, attachments to the current message or call out to another processor and assign the result to a flow variable without overwriting the existing payload. We can define the part that we are enriching in the attribute target
.
Enriching message with an outbound property
In the below <enricher>
element we initialize the outbound property header1
to payload specified using attribute source
. We further transform the message using <append-string-transformer>
.
<enricher target="#[header:OUTBOUND:header1]" source="#[payload]"> <append-string-transformer message=" and something more" /> </enricher>
Multiple enrich elements
We can enrich the message multiple times using child enricher elements each of which has its own target
attribute.
<enricher> <append-string-transformer message=" and more " /> <enrich target="#[header:OUTBOUND:header1]" /> <enrich target="#[header:OUTBOUND:header2]" source="#[payload]" /> </enricher>
Enrich using flow variable
The enricher can call out to another processor or flow. In the below example, we call a different flow using flow-ref
, the result returned by the flow is assigned to a flow variable thus the existing payload is not overwritten.
<enricher target="#[variable:status]"> <flow-ref name="determineStatus" /> </enricher> <custom-processor class="com.javarticles.mule.MyProcessor"/> <sub-flow name="determineStatus"> <expression-transformer expression="Processed #[payload]" evaluator="string" /> </sub-flow>
In the custom processor we retrieve the flow variable.
MyProcessor:
package com.javarticles.mule; import org.mule.api.MuleEvent; import org.mule.api.MuleException; import org.mule.api.processor.MessageProcessor; public class MyProcessor implements MessageProcessor { @Override public MuleEvent process(MuleEvent event) throws MuleException { Object enrichedContent = event.getMessage().getInvocationProperty("status"); System.out.println("Flow variable(status)=" + enrichedContent); return event; } }
Here is the complete mule context.
muleContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd"> <flow name="enrichWithAttributes"> <vm:inbound-endpoint path="enrich1-in" exchange-pattern="request-response" /> <enricher target="#[header:OUTBOUND:header1]" source="#[payload]"> <append-string-transformer message=" and something more" /> </enricher> </flow> <flow name="enrichWithElements"> <vm:inbound-endpoint path="enrich2-in" exchange-pattern="request-response" /> <enricher> <append-string-transformer message=" and more " /> <enrich target="#[header:OUTBOUND:header1]" /> <enrich target="#[header:OUTBOUND:header2]" source="#[payload]" /> </enricher> </flow> <flow name="enrichWithFlowRef"> <vm:inbound-endpoint path="enrich3-in" exchange-pattern="request-response" /> <enricher target="#[variable:status]"> <flow-ref name="determineStatus" /> </enricher> <custom-processor class="com.javarticles.mule.MyProcessor"/> </flow> <sub-flow name="determineStatus"> <expression-transformer expression="Processed #[payload]" evaluator="string" /> </sub-flow> </mule>
We send messages to each of the flows and verify the message properties.
MuleEnricherExample:
package com.javarticles.mule; import org.mule.api.MuleContext; import org.mule.api.MuleMessage; import org.mule.api.client.MuleClient; import org.mule.api.context.MuleContextBuilder; import org.mule.api.context.MuleContextFactory; import org.mule.config.DefaultMuleConfiguration; import org.mule.config.spring.SpringXmlConfigurationBuilder; import org.mule.context.DefaultMuleContextBuilder; import org.mule.context.DefaultMuleContextFactory; public class MuleEnricherExample { public static void main(String[] args) throws Exception { DefaultMuleConfiguration dmc = new DefaultMuleConfiguration(); dmc.setId("muleexample"); dmc.setWorkingDirectory("/esb/mule"); SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder( "muleContext.xml"); MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder(); contextBuilder.setMuleConfiguration(dmc); MuleContextFactory contextFactory = new DefaultMuleContextFactory(); MuleContext ctx = contextFactory.createMuleContext(configBuilder, contextBuilder); ctx.start(); try { System.out.println("Test enrich1-in flow"); MuleClient muleClient = ctx.getClient(); MuleMessage response = muleClient.send("vm://enrich1-in", "XYZ", null); System.out.println("Header1: " + response.getInboundProperty("header1")); System.out.println("Test enrich2-in flow"); response = muleClient.send("vm://enrich2-in", "XYZ", null); System.out.println("Header1: " + response.getInboundProperty("header1")); System.out.println("Header2: " + response.getInboundProperty("header2")); System.out.println("Test enrich3-in flow"); response = muleClient.send("vm://enrich3-in", "XYZ", null); } finally { ctx.dispose(); } } }
Output:
Test enrich1-in flow [INFO ] 2018-07-12 18:06:52.701 [main]AbstractLifecycleManager - Initialising: 'connector.VM.mule.default.dispatcher.1153802607'. Object is: VMMessageDispatcher [INFO ] 2018-07-12 18:06:52.701 [main]AbstractLifecycleManager - Starting: 'connector.VM.mule.default.dispatcher.1153802607'. Object is: VMMessageDispatcher Header1: XYZ and something more Test enrich2-in flow [INFO ] 2018-07-12 18:06:52.797 [main]AbstractLifecycleManager - Initialising: 'connector.VM.mule.default.dispatcher.1893987183'. Object is: VMMessageDispatcher [INFO ] 2018-07-12 18:06:52.797 [main]AbstractLifecycleManager - Starting: 'connector.VM.mule.default.dispatcher.1893987183'. Object is: VMMessageDispatcher Header1: XYZ and more Header2: XYZ and more Test enrich3-in flow [INFO ] 2018-07-12 18:06:52.813 [main]AbstractLifecycleManager - Initialising: 'connector.VM.mule.default.dispatcher.2104842259'. Object is: VMMessageDispatcher [INFO ] 2018-07-12 18:06:52.813 [main]AbstractLifecycleManager - Starting: 'connector.VM.mule.default.dispatcher.2104842259'. Object is: VMMessageDispatcher Flow variable(status)=Processed XYZ
Download the source code
This was an example about Mule Enricher.