FoMS Experts

News / Technology

CHOICE ROUTER IN A MULE APP

Tough Choices: How to route the app

Tough Choices: How to route the app
Share
FacebookGoogle plusLinkedInStumbleuponTwitter
M. Petricevic8.7.2021

Which way should the Mule message be heading? Under the first, the n-th or under no condition. Sounds familiar?

The Choice router is one of the easiest things for most newcomers to the world of MuleSoft to grasp conceptually. It is as simple as the well-known if-else statement, the conditional statement which practically makes programming what it is. There are, however, few caveats to keep in mind no matter how trivial they might seem.

Much like with the if-else statement there is a situation in which a condition is met and a situation in which no condition is met. There has to be at least one if statement. If there are multiple if statements i.e. multiple conditions, the first condition to be met will tell the program to run the block of code corresponding to that if statement. If-else statements are run in a procedural manner (from top to bottom) by a program. This means that, even if all statements are true, the program will execute the block of code of the first true statement and stop. The first statement means the one closest to the top.

But enough about what is probably already known. Let us take a look at the image. The variable animal has the value donkey.

Now comes the fun part, the Choice router. The arrows point to different routes the application can take. The last route always leads to the Default scope of the Choice router. Every other route is called a When scope.

The When scope and the Default scope correspond to the if-statement and the else-statement, respectively. Whatever we place within these scopes corresponds to blocks of code in a classical if-else statement. However, as we are working in a visual tool, we do not have to write any ifs or elses. That's what the When and Default scopes do for us. Simply writing conditions is enough.

In the example shown in the images, the first condition is met. Therefore, the payload will be set to donkey and the Choice router has done its job. Neither the other When scope with its condition nor the Default scope will be considered. The application moves onto the Logger which is set to display the payload content in the Console.

The best way to think of the Choice router is very literal - it routes the Mule application. It is not a good practice to overuse it whenever we think of an if-else situation, because this approach may end in multiple nested Choice routers and that is hardly the purpose of a nice visual tool. Even if we were to avoid such visual nestedness by using Flow Reference within every When and Default scope, it's easy to imagine the XML file getting unnecessarily long.

There have already been talks of the overuse of the Choice router. Here, it's best to think of small routes and small choices.

RELATED ARTICLES

OTHER ARTICLES

Relax, some like it soft-coded

KEPT IN ONE PLACE FOR ALL TO USE

Relax, some like it soft-coded

MUnit testing: Basics

MOCKER, TESTER, SETTER, SPY

MUnit testing: Basics

OLDER ARTICLES

Register now