MuleSoft - Flow Control and Transformers


Advertisements

Flow Control (Routers)

The main task of Flow Control component is to take the input Mule event and route it to one or more separate sequences of components. It is basically routing the input Mule event to other sequence(s) of components. Therefore, it is also called as Routers. Choice and Scatter-Gather routers are the most used routers under Flow Control component.

Choice Router

As the name suggests, this router applies DataWeave logic to choose one of two or more routes. As discussed earlier, each route is a separate sequence of Mule event processors. We can define choice routers as the router that dynamically routes message through a flow according to a set of DataWeave expressions used to evaluate message content.

Schematic diagram of Choice Router

The effect of using Choice router is just like adding conditional processing to a flow or an if/then/else code block in most of the programming languages. Following is the schematic diagram of a Choice Router, having three options. Among those, one is the default router.

Choice Router

Scatter-Gather Router

Another most used routing event processor is Scatter-Gather component. As its name implies, it works on the fundamentals of scatters (copy) and Gather (Consolidates). We can understand its working with the help of following two points −

  • First, this router copies (Scatter) a Mule event to two or more parallel routes. The condition is that each route must be a sequence of one or more event processors which is like a sub-flow. Each route in this case will create a Mule event by using a separate thread. Every Mule event will have its own payload, attributes as well as variables.

  • Next, this router gathers the created Mule events from each route and then consolidates them together into a new Mule event. After this, it passes this consolidated Mule event to the next event processor. Here the condition is that the S-G router will pass a consolidated Mule event to the next event processor only when every route is completed successfully.

Schematic Diagram of Scatter-Gather Router

Following is the schematic diagram of a Scatter-Gather Router having four event processors. It executes every route in parallel and not sequentially.

Scatter Gather Router

Error Handling by Scatter-Gather Router

First, we must have knowledge on the kind of error that can be generated within Scatter-Gather component. Any error might be generated within event processors leading the Scatter-Gather component to throw an error of type Mule: COMPOSITE_ERROR. This error will be thrown by the S-G component only after every route either fails or completes.

To handle this error type, a try scope can be used in each route of Scatter-Gather component. If the error is successfully handled by try scope, then the route will be able to generate a Mule event, for sure.

Transformers

Suppose if we want to set or remove a part of any Mule event, Transformer component is the best choice. Transformer components are of the following types −

Remove variable transformer

As the name implies, this component takes a variable name and removes that variable from the Mule event.

Configuring removing variable transformer

The table below shows the name of fields and their description to be considered while configuring removing variable transformer −

Sr.No Field & Explanation
1

Display Name (doc:name)

We can customize this to display a unique name for this component in our Mule working flow.

2

Name (variableName)

It represents the name of the variable to remove.

Set payload transformer

With the help of set-payload component, we can update the payload, which can be a literal string or DataWeave expression, of the message. It is not recommended to use this component for complex expressions or transformations. It can be used for simple ones like selections.

The table below shows the name of fields and their description to be considered while configuring set payload transformer −

Field Usage Explanation
Value (value) Mandatory The value filed is required for setting a payload. It will accept a literal string or DataWeave expression defining how to set the payload. The examples are like “some string”
Mime Type (mimeType) Optional It’s optional but represents the mime type of the value assigned to the payload of message. The examples are like text/plain.
Encoding (encoding) Optional It’s also optional but represents the encoding of the value that is assigned to the payload of message. The examples are like UTF-8.

We can set a payload through XML configuration code −

With Static Content − Following XML configuration code will set the payload by using static content −

<set-payload value = "{ 'name' : 'Gaurav', 'Id' : '2510' }" 
   mimeType = "application/json" encoding = "UTF-8"/>

With Expression Content − Following XML configuration code will set the payload by using Expression content −

<set-payload value = "#['Hi' ++ ' Today is ' ++ now()]"/>

The above example will append today’s date with the message payload “Hi”.

Set Variable Transformer

With the help of set variable component, we can create or update a variable to store values which can be simple literal values like strings, message payloads or attribute objects, for use within the flow of Mule application. It is not recommended to use this component for complex expressions or transformations. It can be used for simple ones like selections.

Configuring set variable transformer

The table below shows the name of fields and their description to be considered while configuring set payload transformer −

Field Usage Explanation
Variable Name (variableName) Mandatory It is required filed and it represents the name of the variable. While giving the name, follow the naming convention like it must contain number, characters and underscores.
Value (value) Mandatory The value filed is required for setting a variable. It will accept a literal string or DataWeave expression.
Mime Type (mimeType) Optional It’s optional but represents the mime type of the variable. The examples are like text/plain.
Encoding (encoding) Optional It’s also optional but represents the encoding of the variable. The examples are like ISO 10646/Unicode(UTF-8).

Example

The example below will set the variable to the message payload −

Variable Name = msg_var
Value = payload in Design center and #[payload] in Anypoint Studio

Similarly, the example below will set the variable to the message payload −

Variable Name = msg_var
Value = attributes in Design center and #[attributes] in Anypoint Studio.
Advertisements