Mule Controlling Message Flow
About flow control and filter elements.
To multicast a message.
To route message based on conditions.
To filter messages.
About synchronous and asynchronous flows.
To create an asynchronous flow.
Walkthrough 8-1: Multicast a message
In this walkthrough, you will create one flow that calls each of the three airline services and combines
and returns the results back to the web form. You will:
• Use a Scatter-Gather router to concurrently call all three flight services.
• Use a Combine Collections transformer to combine a collection of three ArrayLists of objects
into one collection.
• Use DataWeave to sort the flights and return them as JSON to the form.

Add a router to call all three airline services
1. Return to getFlights.xml.
2. Add a Scatter-Gather flow control element before the Flow Reference in getFlightsFlow.
3. Drag the Flow Reference component into the Scatter-Gather router.

4. Add a second Flow Reference component to the Scatter-Gather router.
5. In the Properties view for the Flow Reference, set the flow name to getDeltaFlightsFlow.
6. Add a third Flow Reference component to the Scatter-Gather router.
7. In the Properties view for the Flow Reference, set the flow name to getAmericanFlightsFlow.
8. Add a breakpoint to the Set payload and destination flowVar component.
9. Add a breakpoint to the Logger.
10. Save the file.
Test the application
11. Debug the application.
12. Make a request to http://localhost:8081/flights and submit the form.
13. Step through the application to the Logger;; you should step through each of the airline flows.
14. Look the Mule Debugger view and see what the data type of the payload is after the router.

15. Click the Resume button.
Flatten the results
16. Add a Combine Collections transformer before the Logger.

Test the application
17. Save the file to redeploy the application.
18. Make a request to http://localhost:8081/flights and submit the form.
19. Step through the application to the Logger after the router;; you should see the payload is now
one ArrayList of HashMaps.

20. Step through to the end of the application;; you should see a jumble of data returned.

Review HTML form code to see what data format it expects
21. Open FlightFinder.html in src/main/resources and locate in what format it expects the data to be
sent back.

Return the data as JSON and sort by price
22. In getFlights.xml, add a Transform Message component after the Combine Collections
transformer.
23. Change its name to Java to JSON

24. In the Java to JSON Properties view, change the output type to application/json.
25. Write a DataWeave expression to transform the payload and order by price.
Test the application
26. Save the file and run the application.
27. Make a request to http://localhost:8081/flights and submit the form;; you should see SFO flights
for all three airlines returned and the flights should be sorted by price.
28. Submit the form for different destinations;; flights for SFO are returned every time.
Use the destination in the airline flows
29. Locate the Set Destination transformer in the getAmericanFlightsFlow.
Note: There are two options at this point: 1) to remove the HTTP endpoints and Set Destination
transformers from each of the airline flows or 2) to use a more complicated expression to check
and see if a destination flow variable already exists so that the individual airline flows can still be
called and tested individually.
30. Modify the value so it only uses the expression to set the destination flowVars variable if it does
not already exist.
#[(flowVars.destination ==empty &&
message.inboundProperties.'http.query.params'.code == empty) ? 'SFO' :
(flowVars.destination != empty ? flowVars.destination :
message.inboundProperties.'http.query.params'.code)]
Note: This expression is located in the course snippets.txt file and can be copied from there.
31. Use the same expression in the Set Destination in getDeltaFlightsFlow and
getUnitedFlightsFlow.
Test the application
32. Save the file to redeploy the application.
33. Make a request to http://localhost:8081/flights and submit the form;; you should still see SFO
flights.
34. Submit the form for a different destination including LAX, CLE, or PDX;; you should see the
correct flights returned.

35. Submit the form for a destination of PDF or FOO;; you should get an error in the console but no
return error message to the form.
36. Submit the form for SFO, LAX, CLE, or PDX and a different airline;; you should still see flights for all three airlines returned.
About flow control and filter elements.
To multicast a message.
To route message based on conditions.
To filter messages.
About synchronous and asynchronous flows.
To create an asynchronous flow.
Walkthrough 8-1: Multicast a message
In this walkthrough, you will create one flow that calls each of the three airline services and combines
and returns the results back to the web form. You will:
• Use a Scatter-Gather router to concurrently call all three flight services.
• Use a Combine Collections transformer to combine a collection of three ArrayLists of objects
into one collection.
• Use DataWeave to sort the flights and return them as JSON to the form.
Add a router to call all three airline services
1. Return to getFlights.xml.
2. Add a Scatter-Gather flow control element before the Flow Reference in getFlightsFlow.
3. Drag the Flow Reference component into the Scatter-Gather router.
4. Add a second Flow Reference component to the Scatter-Gather router.
5. In the Properties view for the Flow Reference, set the flow name to getDeltaFlightsFlow.
6. Add a third Flow Reference component to the Scatter-Gather router.
7. In the Properties view for the Flow Reference, set the flow name to getAmericanFlightsFlow.
8. Add a breakpoint to the Set payload and destination flowVar component.
9. Add a breakpoint to the Logger.
10. Save the file.
Test the application
11. Debug the application.
12. Make a request to http://localhost:8081/flights and submit the form.
13. Step through the application to the Logger;; you should step through each of the airline flows.
14. Look the Mule Debugger view and see what the data type of the payload is after the router.
15. Click the Resume button.
Flatten the results
16. Add a Combine Collections transformer before the Logger.
Test the application
17. Save the file to redeploy the application.
18. Make a request to http://localhost:8081/flights and submit the form.
19. Step through the application to the Logger after the router;; you should see the payload is now
one ArrayList of HashMaps.
20. Step through to the end of the application;; you should see a jumble of data returned.
Review HTML form code to see what data format it expects
21. Open FlightFinder.html in src/main/resources and locate in what format it expects the data to be
sent back.
22. In getFlights.xml, add a Transform Message component after the Combine Collections
transformer.
23. Change its name to Java to JSON
24. In the Java to JSON Properties view, change the output type to application/json.
25. Write a DataWeave expression to transform the payload and order by price.
Test the application
26. Save the file and run the application.
27. Make a request to http://localhost:8081/flights and submit the form;; you should see SFO flights
for all three airlines returned and the flights should be sorted by price.
28. Submit the form for different destinations;; flights for SFO are returned every time.
Use the destination in the airline flows
29. Locate the Set Destination transformer in the getAmericanFlightsFlow.
Note: There are two options at this point: 1) to remove the HTTP endpoints and Set Destination
transformers from each of the airline flows or 2) to use a more complicated expression to check
and see if a destination flow variable already exists so that the individual airline flows can still be
called and tested individually.
30. Modify the value so it only uses the expression to set the destination flowVars variable if it does
not already exist.
#[(flowVars.destination ==empty &&
message.inboundProperties.'http.query.params'.code == empty) ? 'SFO' :
(flowVars.destination != empty ? flowVars.destination :
message.inboundProperties.'http.query.params'.code)]
Note: This expression is located in the course snippets.txt file and can be copied from there.
31. Use the same expression in the Set Destination in getDeltaFlightsFlow and
getUnitedFlightsFlow.
Test the application
32. Save the file to redeploy the application.
33. Make a request to http://localhost:8081/flights and submit the form;; you should still see SFO
flights.
34. Submit the form for a different destination including LAX, CLE, or PDX;; you should see the
correct flights returned.
35. Submit the form for a destination of PDF or FOO;; you should get an error in the console but no
return error message to the form.
36. Submit the form for SFO, LAX, CLE, or PDX and a different airline;; you should still see flights for all three airlines returned.
This is blog is helpful for everyone. keep on sharing this type of articles
ReplyDeleteMulesoft Online Training
Mulesoft Training in Hyderabad