Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Enrichment Pattern in Mule 4

DZone's Guide to

Enrichment Pattern in Mule 4

Let's take a look at the differences between Mule 3 and Mule 4 in regards to using the Message Enricher component.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Content Enricher is definitely a useful integration pattern. It allows easily enriched message content with new data gathered from an external resource. In the vast majority of Mule's apps, I have been using the Message Enricher Component. Today, I will point out the 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 don't have enough data to proceed a 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, the message (before entering Enricher) has smaller content. Then component looks for additional data and appends it to the original message. In consequence, you'll have an enriched message.

Image title

We can distinguish three different kinds of enrichment:

  1. computing additional data based on the current message/payload
  2. look up in the current environment
  3. external system call

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

Image title

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 nuances introduced with the release of Mule 4 Runtime. Before the release, Message and other components were wrapped within the Message Object. As of Mule 4, we call it an Event. The same is true of components. Previously, they were known as Message Processors, now they are known as Event Processors.

The second option is using the Message Enricher. This processor enriches the 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 records and inserts it into a database. We would like to enrich record with loan data using HTTP Request connector.

Image title

The above flow, unfortunately, does not work because it replaces the current record with a response from the Loan Broker application. How should we fix this in order to enrich the message, not replace it? MuleSoft has introduced the Message Enricher scope. We may wrap a message processor within it, like in the diagram below. As you can see, Loan Broker HTTP is not wrapped in the Message Enricher. This scope has two properties called source and target. The first one holds enrichment data, the latter one holds the place where additional data should be saved.

Image title

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 process the 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 a Flow Reference component like in the flow below.

Image title

Enrichment in Mule 4.x

In the 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 the component within an extra element. 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 knew that because I had prepared it :).

Image title

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

Image title

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

Image title

In line 4 and 5, I have defined enrichment attributes Target and Target Value.

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

If I remove the target attribute from line 4, Mule engine will assign, by default, outcome value to the payload.

Summary

I like the simplicity that was introduced in Mule 4 regarding the content enricher pattern. However, I think that it is harder to read such a flow as I cannot clearly see if the 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 the Message Enricher scope, I could define more than one target; in other words, I could enrich the message with more than one content. I cannot see if this is viable in Mule 4. Apart from these small drawbacks, I think that it is nicely done and integrated consistently for all connectors.

Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

Topics:
integration ,mule 3 ,mule 4 ,comparison ,mulesoft ,message enricher ,naming convention

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}