Last Updated on 04/05/2021 by Patryk Bandurski
Content Enricher is definitely a useful integration pattern. It allows easily enrich message content with new data gathered from external resources. In the vast majority of Mule’s apps, I have been using the Message Enricher Component. Today I will point main differences in Mule 3.x and Mule 4.x regarding this element.
Content Enricher pattern
When you think about message processing you have in mind a specific entity. There are times when you have not enough data to proceed message further. As a result, you need some pulling and enriching mechanisms. Below you can see the Enricher pattern diagram from the enterprise integration patterns site/book. As you can see the message, before entering Enricher, has smaller content. Then component looks for additional data and append it to the original message. In consequence with have an enriched message.
We can distinguish three different kinds of enrichment:
- computing additional data based on the current message/payload,
- lookup in the current environment,
- external system call.
Mule event holds payload and variables. We may enrich the current payload, but we can also make enrichment by creating additional variables. Below I have prepared a Mule Event diagram presenting the event structure. We can add additional data both to payload and attributes. Depending on the choice we may do it differently in Anypoint Studio.
For Payload enrichment you may use DataWeave Transformer in cases of option one and two. Like in the example below:
%dw 2.0
output application/java
---
payload + { timestamp: now()}
Message Enricher in Mule 3
So let’s see how it looks in Mule 3. Below we have an application that processes incoming record and inserts it into a database. We would like to enrich the record with loan data using the HTTP Request connector.
The above flow, unfortunately, does not work. Because it replaces the current record with the response from the Loan Broker application. How we should fix this in order to enrich the message, not to replace it? MuleSoft has introduced Message Enricher scope. We may wrap the message processor within it, like in the diagram below. As you can see, Loan Broker HTTP is not wrapped in Message Enricher. This scope has two properties called source and target. The first one holds enrichment data, the latter holds a place where additional data should be saved.
Using this scope has fixed our application. We now have a record with an additional flow variable containing loan data. It is also clearly depicted that the Loan Broker HTTP connector is used to enrich the original message. However, I would like to processes loan data before I return it to the variable. Message Enricher scope requires only one message processor within. In order to place a chain of calls, we need to extract them to a subflow and reference it using the Flow Reference component like in the flow below.
Enrichment in Mule 4
In Mule 4 Message Enricher scope has been removed. As a replacement Target Variable/Value properties have been introduced. About that a little bit later. Now you don’t need to wrap components within the extra scope. On the flow below, the Loan Broker HTTP connector is used to enrich the original payload. You can’t say that by just looking, however, I know that because I have prepared it :).
When you click on HTTP Request component you will see configuration like below:
It means that the result from Loan Broker HTTP connector will be saved in vars.loan. You have two attributes in Output section:
- Target (target): name of the variable that will hold data
- Target Value (targetValue): value to be stored within variable
It is available not only for connectors but other components as well for example for Flow Reference. Using Flow reference to enrich message I can perform advanced processing before. In the example below I have extracted logic into a different subflow and I have made a reference to it.
In lines 3 and 4 I have defined enrichment attributes Target and Target Value.
<flow-ref doc:name="complex-enrichment-SubFlow"
name="complex-enrichment-SubFlow"
target="loan"
targetValue="#[payload]"/>
If I remove target attribute from line 3, Mule engine will assign, by default, outcome value to payload.
Summary
I like the simplicity that was introduced with Mule 4 regarding content enricher pattern. However, I think that it is harder to read such flow, as I can not clearly see if component/connector is used for enrichment purposes. It would be a good idea to introduce some small overlay-icon indicating enrichment. It would be easier to read. I found one more difference. In Message Enricher scope I could define more than one target, in other words, I could enrich the message with more than one content. I can not see if this is viable in Mule 4. Apart from these small drawbacks, I think that is nicely done and integrated consistently for all connectors.
One thought on “Enrichment Pattern in Mule 4”