Content Enricher is definitely an useful integration pattern. It allows easily enrich message content with new data gathered from external resource. In vast majority of Mule’s apps I have been using 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 your 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 mechanism. Below you can see Enricher pattern diagram from enterprise integration patterns site/book. As you can see message, before entering Enricher, has smaller content. Then component looks for additional data and append it to 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 current environment,
  3. external system call.

Mule event hold payload and variables. We may enrich current payload, but we can also make enrichment by creating additional variables. Below I have prepared Mule Event diagram presenting 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:

</p>
<p>%dw 2.0<br />
output application/java<br />
---<br />
payload + { timestamp: now()}</p>
<p>

Naming convention

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

 

The second option is using Message Enricher. This processor enrich event by creating additional variables that can be used later on.

Previous approach in Mule 3.x

So let’s see how it looks in Mule 3.x. Below we have an application that processes incoming record and insert it into a database. We would like to enrich record with loan data using HTTP Request connector.

Flow without enricher processor
Flow without enricher processor

Above flow, unfortunately does not work. Because is replaces current record with response from Loan Broker application. How we should fix this in order to enrich message, not to replace it? MuleSoft has introduced Message Enricher scope. We may wrap message processor within it, like in the diagram below. As you can seen, 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 one holds 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 additional flow variable containing loan data. It is also clearly depicted that Loan Broker HTTP connector is used to enrich original message. However I would like to processes loan data before I return it to 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 Flow Reference component like in the flow below.

Complex message enrichment
Complex message enrichment

Enrichment in Mule 4.x

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 component within extra element. On the flow below, Loan Broker HTTP connector is used to enrich original payload. You can’t say that by just looking, however I known that because I have prepared it :).

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 line 4 and 5 I have defined enrichment attributes Target and Target Value. 

<br />
&lt;flow-ref doc:name=&quot;complex-enrichment-SubFlow&quot;<br />
          doc:id=&quot;f0a14500-d4b4-4252-a854-89f91936f22&quot;<br />
          name=&amp;quot;complex-enrichment-SubFlow&quot;<br />
          target=&quot;loan&quot;<br />
          targetValue=&quot;#[payload]&quot;/&gt;<br />

If I remove target attribute from line 4, 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 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 message with more than one content. I can not see if this is viable in Mule 4. Apart from this small drawbacks I think that is is nicely done and integrated consistently for all connectors.

Enrichment Pattern in Mule 4
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *