The sample code for this tutorial is fairly simple, but it uses some of Mule’s most common message processors. It implements a basic use case:
-
It receives an HTTP request.
-
It extracts data from the request to route a message through the application.
-
It decides how to create a response.
Studio Visual Editor
XML or Standalone Editor
<?xml version="1.0" encoding="UTF-8"?>
<mule 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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="9090" doc:name="HTTP Listener Configuration"/>
<flow name="exampleFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="#[message.inboundProperties['http.query.params']['url_key']]" doc:name="Set Original Payload"/>
<flow-ref name="exampleFlow2" doc:name="exampleFlow2"/>
<choice doc:name="Choice">
<when expression="#[flowVars['my_variable'].equals('var_value_1')]">
<set-payload value="#['response_payload_1']" doc:name="Set Response Payload"/>
</when>
<otherwise>
<set-payload value="#['response_payload_2']" doc:name="Set Response Payload"/>
</otherwise>
</choice>
</flow>
<flow name="exampleFlow2">
<choice doc:name="Choice">
<when expression="#['payload_1'.equals(payload)]">
<flow-ref name="exampleSub_Flow1" doc:name="exampleSub_Flow1"/>
</when>
<otherwise>
<flow-ref name="exampleSub_Flow2" doc:name="exampleSub_Flow2"/>
</otherwise>
</choice>
</flow>
<sub-flow name="exampleSub_Flow1">
<set-variable variableName="my_variable" value="#['var_value_1']" doc:name="my_variable"/>
</sub-flow>
<sub-flow name="exampleSub_Flow2">
<set-variable variableName="my_variable" value="#['var_value_2']" doc:name="my_variable"/>
</sub-flow>
</mule>
We analyze the above code by breaking it up into sections. The first section is the entry point of the application, which contains the HTTP listener in exampleFlow
.
Studio Visual Editor
-
Main Flow, exampleFlow
.
-
Take data from the HTTP request.
-
Make call to exampleFlow2
for further processing.
-
Make a decision based on the value of the invocation variable my_variable
, which was set by exampleFlow2
.
-
Creates response payload 1
.
-
Creates response payload 2
.
XML or Standalone Editor
<flow name="exampleFlow"> (1)
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="GET" doc:name="HTTP"/>
<set-payload value="#[message.inboundProperties['http.query.params']['url_key']]" doc:name="Set Original Payload"/> (2)
<flow-ref name="exampleFlow2" doc:name="exampleFlow2"/> (3)
<choice doc:name="Choice"> (4)
<when expression="#[flowVars['my_variable'].equals('var_value_1')]">
<set-payload value="#['response_payload_1']" doc:name="Set Response Payload"/> (5)
</when>
<otherwise>
<set-payload value="#['response_payload_2']" doc:name="Set Response Payload"/> (6)
</otherwise>
</choice>
</flow>
1 |
Main Flow, exampleFlow . |
2 |
Take data from the HTTP request. |
3 |
Make call to exampleFlow2 for further processing. |
4 |
Make a decision based on the value of the invocation variable my_variable , which was set by exampleFlow2 . |
5 |
Creates response payload 1 . |
6 |
Creates response payload 2 . |
The second part of the app, exampleFlow2
, is basically for routing. It does not perform any real action over the message or its payload. Rather, it delegates that to two other subflows, based on the payload received as input.
Studio Visual Editor
-
Evaluate the payload that enters the flow.
-
Make call to exampleSub_Flow1
.
-
Make call to exampleSub_Flow2
.
XML or Standalone Editor
<flow name="exampleFlow2">
<choice doc:name="Choice">
<when expression="#['payload_1'.equals(payload)]"> (1)
<flow-ref name="exampleSub_Flow1" doc:name="exampleSub_Flow1"/> (2)
</when>
<otherwise>
<flow-ref name="exampleSub_Flow2" doc:name="exampleSub_Flow2"/> (3)
</otherwise>
</choice>
</flow>
1 |
Evaluate the payload that enters the flow. |
2 |
Make call to exampleSub_Flow1 . |
3 |
Make call to exampleSub_Flow2 . |
Finally we have the various subflows called exampleSub_Flow<number>
, whose only task is to set a value for an invocation variable named my_variable
.
Studio Visual Editor
-
Set my_variable
to var_value_1
.
-
Set my_variable
to var_value_2
.
XML or Standalone Editor
<sub-flow name="exampleSub_Flow1">
<set-variable variableName="my_variable" value="#['var_value_1']" doc:name="my_variable"/> (1)
</sub-flow>
<sub-flow name="exampleSub_Flow2">
<set-variable variableName="my_variable" value="#['var_value_2']" doc:name="my_variable"/> (2)
</sub-flow>
1 |
Set my_variable to var_value_1 . |
2 |
Set my_variable to var_value_2 . |