Enrichment Pattern in Mule 4

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.

Enricher Pattern (Source: https://www.enterpriseintegrationpatterns.com/patterns/messaging/DataEnricher.html)
Enricher Pattern (Source: https://www.enterpriseintegrationpatterns.com/patterns/messaging/DataEnricher.html)

We can distinguish three different kinds of enrichment:

  1. computing additional data based on the current message/payload,
  2. lookup in the current environment,
  3. 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.

Mule Event structure (Mule 4)
Mule Event structure (Mule 4)

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()}

Naming convention

Please be aware of some naming convention nuisance introduced with the release of Mule 4 Runtime. Before the release, the Message and other components were wrapped within the Message Object. As of Mule 4, we call it an Event. The same is with components. Previously they were known as Message Processors, now they are Event Processors.

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.

Flow without enricher processor
Flow without enricher processor

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.

Simple Message Enricher
Simple Message Enricher

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.

Complex message enrichment
Complex message enrichment

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 :).

Enriched event in Mule 4
Enriched event in Mule 4

When you click on HTTP Request component you will see configuration like below:

Output properties
Output properties

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.

Complex enrichment in Mule 4
Complex enrichment in Mule 4

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.

Enrichment Pattern in Mule 4

One thought on “Enrichment Pattern in Mule 4

Leave a Reply

Your email address will not be published.

Scroll to top