DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API
  • On-Demand-Schedulers With MuleSoft CloudHub APIs
  • Ultimate Guide to FaceIO

Trending

  • How We Broke the Monolith (and Kept Our Sanity): Lessons From Moving to Microservices
  • Advanced gRPC in Microservices: Hard-Won Insights and Best Practices
  • Seata the Deal: No More Distributed Transaction Nightmares Across (Spring Boot) Microservices
  • Replacing Legacy Systems With Data Streaming: The Strangler Fig Approach

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.

By 
Ankit Lawaniya user avatar
Ankit Lawaniya
·
Aug. 26, 17 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
21.1K Views

Join the DZone community and get the full member experience.

Join For Free

Mule 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.

Image title

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:Image title

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"
}


Image title


Hope this helps.

Thanks.

Keep learning.

Payload (computing)

Opinions expressed by DZone contributors are their own.

Related

  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API
  • On-Demand-Schedulers With MuleSoft CloudHub APIs
  • Ultimate Guide to FaceIO

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: