In this article we will learn about the Choice Flow Control Component in Mule
Introduction
In a Mule flow, the Flow Control Components route messages to different distinations.There are various kinds of flow controls available in Mule Studio out of which we will explore the "Choice" control whose purpose is to route messages based on expressions.
What we need to have for doing the experiment?
We need
- Download Mule ESB
- Everything should be up and running properly
Let us start the experiment
In this experiment we will move the file to the destination folder based on their file types.For example, we will have a source folder where we will have various kinds of files.We are interested in moving only text(.txt), power point(.pptx) and zipped(.zip) files to 3 different output folders.For any other file types present in the source folder, we will display some informational message.
So let us create a source folder say "Input" and put various kinds of files into it

As can be figured out that there are various kinds of files present inside the source folder.
Now let us first drag and drop a File-Inbound Endpoint control into the "Message Flow".Then from the "Flow Control" palatte, drag and drop a "Choice" Control.

At this point of time, the flow will be as under

The default syntax can be as under
<choice doc:name="Choice">
<when expression=""> </when>
<when expression=""> </when>
<otherwise> </otherwise>
</choice>
It is exactly like the "Switch" statement in the programming language where a statement executes if it satisfies the condition else some default result gets executed.
Let us now go back to the "Configuration XML" section and add the below code
<choice doc:name="Choice">
<when expression="#[message.inboundProperties['originalFilename'].endsWith('.txt')]">
<processor-chain>
<file:outbound-endpoint path="C:\Only Text Files" responseTimeout="10000" doc:name="Only Text Files"/>
</processor-chain>
</when>
<when expression="#[message.inboundProperties['originalFilename'].endsWith('.pptx')]">
<processor-chain>
<file:outbound-endpoint path="C:\Only Power-Point Files" responseTimeout="10000" doc:name="Only PPT Files"/>
</processor-chain>
</when>
<when expression="#[message.inboundProperties['originalFilename'].endsWith('.zip')]">
<processor-chain>
<file:outbound-endpoint path="C:\Only Zipped Files" responseTimeout="10000" doc:name="Only Zip Files"/>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="Invalid File Type(s)" level="INFO" doc:name="Logger"/>
</processor-chain>
</otherwise>
</choice>
The configuration is quite self explanatory.When the file extension ends with ".txt", we are moving those files to the "Only Text Files" Folder.When the file extension ends with ".pptx", we are moving those files to the "Only Power-Point Files" Folder.When the file extension ends with ".zip", we are moving those files to the "Only Zipped Files" Folder.When nothing matched, we are displaying the message "Invalid File Type(s)".
The complete flow looks as under

And the full configuration file is as under
<flow name="TestFlow" doc:name="TestFlow">
<file:inbound-endpoint path="C:\input" responseTimeout="10000" pollingFrequency="30000" doc:name="Input" />
<choice doc:name="Choice">
<when expression="#[message.inboundProperties['originalFilename'].endsWith('.txt')]">
<processor-chain>
<file:outbound-endpoint path="C:\Only Text Files" responseTimeout="10000" doc:name="Only Text Files"/>
</processor-chain>
</when>
<when expression="#[message.inboundProperties['originalFilename'].endsWith('.pptx')]">
<processor-chain>
<file:outbound-endpoint path="C:\Only Power-Point Files" responseTimeout="10000" doc:name="Only PPT Files"/>
</processor-chain>
</when>
<when expression="#[message.inboundProperties['originalFilename'].endsWith('.zip')]">
<processor-chain>
<file:outbound-endpoint path="C:\Only Zipped Files" responseTimeout="10000" doc:name="Only Zip Files"/>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="Invalid File Type(s)" level="INFO" doc:name="Logger"/>
</processor-chain>
</otherwise>
</choice>
</flow>
We are now all set to start our application.Let's run it.And the output is

If we open the folder, the output will be as under

References
- Choice Flow Control Reference
- Flow Control Overview
Conclusion
Hope you have enjoyed the tutorial.More to come soon.