The choice flow control dynamically routes messages based on message payload or properties. It adds conditional programming to a flow, similar to an
if/then/else
code block.
A choice flow control uses expressions to evaluate the content of a message, then it routes the message to one of the routing options within its scope (see image below). It directs messages to the first routing option in the scope that matches the routing configurations (evaluates to true). If none of expressions evaluate to true, the choice flow control directs the message to the default (
else
) route.
The implementation in Mule as follows
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="choicedemoFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<logger message="#[message.inboundProperties.'http.query.params'.get('source')]" level="INFO" doc:name="Logger"/>
<choice doc:name="Choice">
<when expression="#[message.inboundProperties.'http.query.params'.get('source')=='File']">
<file:outbound-endpoint path="src/main/resources" responseTimeout="10000" doc:name="File"/>
</when>
<otherwise>
<vm:outbound-endpoint exchange-pattern="one-way" path="abc" doc:name="VM"/>
</otherwise>
</choice>
</flow>
<flow name="choicedemoFlow1">
<vm:inbound-endpoint exchange-pattern="one-way" path="abc" doc:name="VM"/>
<logger message="THe message retrived is #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
No comments:
Post a Comment