Content Enrichment Using Mule Message Enricher
Learn about content enrichment using the Mule Message Enricher component to enrich the payload with additional information without disturbing the payload.
Join the DZone community and get the full member experience.
Join For FreeMule Message Enricher is one of the scopes in Mule which allows the current message to be augmented using data from a separate resource, which we call the Enrichment Resource. The Mule implementation of the Enrichment Resource (a source of data to augment the current message) can be any message processor.
In simple language, we can say a message enricher enriches the current payload with some additional message or information and this is done without disturbing the current payload.
Enricher is used if the target system needs more information than the source system can provide. It enriches the Mule message by calling an external system or doing some transformation to the existing payload and saving it into some scope of variable, like session, outbound, or invocation, and the transformation happening in the enricher scope doesn't affect the actual payload. Set-property: Save some information extracted from payload or original payload to some invocation or flow scope variable.
Use Cases
The Mule Message Enricher is designed for performing interactions like calling an outbound endpoint and bringing the result back to the main flow, which will be used to add the additional information into the existing payload.
One common scenario involves the need to enrich an incoming message with information that isn’t provided by the source system. You can use a content enricher if the target system needs more information than the source system can provide.
Mule Message Enricher allows the current message to be used in performing a particular task separately without disturbing the original message. Mule Message Enricher is best used in the case when you do not want to lose your existing payload.
Example: If, in your flow, if you need to call an external service with an HTTP outbound/HTTP request component in middle of the flow after getting the response from the HTTP external service call you will find that your current payload is modified with the response of the external service, but you didn't want to change or disturb the existing payload. In that case, we can wrap our HTTP outbound/HTTP request component inside the enricher and it will make a call to the external system, store the result and your existing payload will not be modified.
How Does Message Enricher Work?
Below is the step by step process to demonstrate how Message Enricher works.
Enricher sends a copy of the original message into the processor.
The original message waits.
The copy is processed.
The copy's response is a message.
Part(s) of the response are added to part(s) of the original message.
The enriched message moves forward.
The way in which the message is enriched (or modified) is by explicitly configuring mappings (source->target) between the result from the Enrichment Resource and the message using Mule Expressions. Mule Expressions are used to both select the value to be extracted from the result that comes back from the Enrichment Resource (source) and to define where this value to be inserted into the message (target). The default source, if not configured, is the payload of the result from the Enrichment Resource.
The “enrichment resource” can be any message processor, outbound connector, processor-chain, or flow-ref. If using an outbound-connector then, of course, it should have a request-response exchange pattern.
IMP Note: As only one component can reside inside Message Enricher, use of processor chain is recommended if more components are needed to reside inside Message Enricher.
Let’s walk through how to use Mule Message Enricher in application. In this example, we are receiving the JSON Request consisting Employee data with id and role of the employee through HTTP call from the REST client. Our objective here is to add the additional information which is the name of the employee to the source data. For this, we will be calling Database to get the information about the employee name and that will be added in the source JSON to be sent back to the user.
Flow:
Code:
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf"
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.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<db:generic-config name="Generic_Database_Configuration"
url="jdbc:hsqldb:hsql://localhost:9001" driverClassName="org.hsqldb.jdbcDriver"
doc:name="Generic Database Configuration" />
<http:listener-config name="HTTP_Listener_Configuration"
host="localhost" port="8081" basePath="/enricher" doc:name="HTTP Listener Configuration" />
<flow name="messageEnricherFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/enricher" doc:name="HTTP" />
<byte-array-to-string-transformer
doc:name="Byte Array to String" />
<logger level="INFO" message="Printing id #[json:id]" doc:name="Logger" />
<enricher doc:name="Enricher">
<db:select config-ref="Generic_Database_Configuration"
doc:name="Select * from Employee">
<db:dynamic-query><![CDATA[select name from employee where id='#[json:id]']]></db:dynamic-query>
</db:select>
<enrich target="#[sessionVars.name]" source="#[groovy:payload[0].name]" />
</enricher>
<set-payload value="#[payload]" mimeType="application/json" />
<logger level="INFO" message="#[payload]" doc:name="Logger" />
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
"id" : payload.id,
"name" : sessionVars.name,
"Role": payload.role
}]]></dw:set-payload>
</dw:transform-message>
</flow>
</mule>
Request:
{
"id": 1,
"role": "Application Developer"
}
Response:
{
"id": 1,
"name": "Ankit",
"Role": "Application Developer"
}
Hope this helps.
Thanks.
Keep learning.
Opinions expressed by DZone contributors are their own.
Comments