Learn about content enrichment using the Mule Message Enricher component to enrich the payload with additional information without disturbing the payload.
2. 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.
9/3/2017 Ankit Lawaniya 2
4. 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.
9/3/2017 Ankit Lawaniya 4
5. 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.
9/3/2017 Ankit Lawaniya 5
6. 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.
9/3/2017 Ankit Lawaniya 6
7. 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.
9/3/2017 Ankit Lawaniya 7