Table of Contents
About
1.
Objectives
- Multicast events
- Route events based on conditions
- Validate events
Notes
Intro
Module 4: Controlling event flow
Two ways of retrieving data by providing the ability to retrieve
- The combined results of all airlines
- A single airline or combined
At the end of this module, you should be able to
- Multicast events
- Route events based on conditions
- Validate events
Routing events
Topic video
Routers
- Routers send events to one or more groups of event processors (routes)
- Choice: one route executed based on conditional logic
- First Successful: routes executed sequentially until one is successfully executed
- Round Robin: one route executed, which one is selected by iterating through a list maintained across executions
- Scatter-Gather: all routes executed concurrently
Extras
- Topic slides
Event Flow Control Explained | Lightboard session
Choice
Provides multiple paths
Choice Router
- Functions like a switch statement in Java and other languages
How it works
- A condition is specified on each path, and then a default path is set
- It runs from top to bottom looking or a successful condition to execute
- Any successful condition in one of the before the default path is executed, otherwise the default is executed
Scatter-Gather
- Provides multi-threaded functionality
- Very useful when there is need to cut down the response time
How it works
One thread in, Multithreaded, one thread out the other side
Scatter-Gather
- Use if A, B and C can be executed in parallel without having to wait for the result of one in order to call the next step
Scatter
- It spins up multiple threads
- All parts are executed, each with their own thread
Gather part
- Then the ‘gather part’ combines the results that came out of each of the paths into a unified single event coming out
Round Robin
- Useful when communicating with a system that doesn’t have a load balancer to distribute load
- E.g. if you need it to distribute different order between two different JMS queues, or to talk to two different servers without load balancers
- It does the load balancing or the load distributing by different paths
- Not necessary if there is a load balancer between Mule (client) and the system you’re communicating with (consumer)
How it works
Round Robin- When the first request comes in, it goes down path A
- When another request comes in, new event, goes down path B…
- And then it loops back to the top
First Successful
- It has to do with the errors returned i.e. if errors are returned by any of the paths, it will keep moving down the list
- It doesn’t need a condition, neither is it doing multi-threaded execution
How it works
- It will first try the top most one, if that fails
- It tries the second…
Multicasting events
Topic video
The Scatter-Gather router
- Scatter-Gather sends the event to each route concurrently and returns a collection of all results
Collection is an object of objects
- Each object contains attributes and payload from each Mule event returned from a flow
Walkthrough 4-1: Multicast an event
- Use a Scatter-Gather router to concurrently call all three flight services
- Use DataWeave to flatten multiple collections into one collection
Routing events based on conditions
Topic video
The Choice router
Choice Router- Sends the event to one route based on conditional logic
- The conditions are written with DataWeave
Walkthrough 4-2: Route events based on conditions
- Use a Choice router
- Use DataWeave expressions to set the router paths
- Route all flight requests through the router
Validating events
Topic video
Validators
- Provide a way to test some conditions are met and throw an error if the validation failes
To use
- Add the Validation module to a project
- Select a validation operation
Walkthrough 4-3: Validate events
Validate Events- Add the Validation module to a project
- Use an Is true validator to check if a query parameter called code with a value of SFO, LAX, CLE, PDX or PDF is sent with a request
- Return a custom error message if the condition is not met
Summary
Use different routers and validators to control event flow
Use the Choice router to send an event to one route based on conditional logic
Use the Scatter-Gather router to send an event concurrently to multiple routes
- A collection of all results is returned
- Use DataWeave to flatten the collection
Use the Validation module to specify whether an even can proceed in a flow
Test your knowledge
What module and operation will throw an error if a Mule event’s payload is not a number?
- Validation module’s Is number operation
An event contains a payload that is an Array of Objects. How is the event routed in a Scatter-Gather?
- The ENTIRE event is sent to each route and passed in PARALLEL
Refer to the exhibits. What happens to this flow when the Validation module’s Is not null operation throws an error?
[[/img/Validation-question.png]
- The flow stops processing its Mule event and returns an error message to the HTTP Listener operation
A Scatter-Gather processes three separate HTTP requests. Each request returns a Mule event with a JSON payload. What is the final output of the Scatter-Gather?
- An Object containing all three Mule event Objects
How are multiple conditions used in a Choice router to route events?
- To find the FIRST true condition, then distribute the event to the ONE matched route
Anki
Links
- Anypoint Platform Development: Fundamentals - Part 2 Building Mule applications with Anypoint Platform
- Module 8: Consuming web services
- Module 11: Writing DataWeave transformations
References
. “Module 9: Controlling event flow”. Available at: . (Accessed: ↩︎
).