Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Message Enricher Scope in Mule

DZone's Guide to

Message Enricher Scope in Mule

Learn how to implement the Mule Message Enricher scope to modify part of an original payload without affecting the entire payload.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

A Mule Scope is a code block which contains a series of message processors. Mule provides some very important scopes, e.g. Async, Cache, Message Enricher, and For Each. etc. For a detailed list of scopes, please read the documentation.

The intention of this post is to discuss the Message Enricher scope with the help of a simple example.

Message Enricher Scope

Many times, we need to modify some part of our original payload without affecting the whole payload. For example, we have a payload like this:

[  
   {  
      name:"abc",
      age:12,
      zip:"",
      street:""
   },
   {  
      name:"abc",
      age:12,
      zip:"",
      street:""
   }
]

We need to fill the zip and street values of the payload; in other words, we need to enrich this message.

To accomplish this, we can use the Message Enricher Scope provided by Mule.

Let's Get Our Hands Dirty

We are going to see the simplest example of using the Message Enricher Scope. Let's create the simplest Mule project and add the Message Enricher scope from the palette menu.

In this application, I am just getting the zip and street values from a Sub-Flow and enriching the original payload with these values. Here is the screen shot of the project:

Project

The most important part is the Enrich elements where we define how to enrich the payload.

Here is the xml configuration of the project:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<flow name="mainFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/for" doc:name="HTTP" />
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---

[{
name:"abc",
age:12,
zip:"",
street: ""

},
{
name:"abc",
age:12,
zip:"",
street: ""

}
]

]]></dw:set-payload>
</dw:transform-message>
        <foreach doc:name="For Each">
            <enricher doc:name="Message Enricher">
                <flow-ref name="subFlow" doc:name="Flow Reference"/>
                <enrich source="#[flowVars.zipCode]" target="#[payload.zip]"/>
                <enrich source="#[flowVars.street]" target="#[payload.street]"/>
            </enricher>
        </foreach>

<logger level="INFO" doc:name="Logger"
message="#[message.payloadAs(java.lang.String)]" />
</flow>
<sub-flow name="subFlow">
<set-variable variableName="zipCode" value="#['010101']"
doc:name="Zip" />
<set-variable variableName="street" value="#['Frei Caneca']"
doc:name="Street" />
</sub-flow>
</mule>

That's it. When you execute the project, you will get the output with the zip and street fields enriched.

In this example, I have shown with a very simple example how to use the Message Enricher scope. In the next article, I will cover other scopes.

Thanks.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mule ,integration ,scopes ,message enricher

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}