Using Flows for Service Orchestration
IntroductionA Flow is a simple yet very flexible mechanism that enables orchestration of services using the sophisticated message flow capabilities of Mule ESB. Using Flow, you may automate integration processes and construct sophisticated integration solutions by simply building them from the basic Message Sources provided by Mule. Because of the flexibility of Flow, it is much easier to create solutions that more closely match your requirements. Flow is new in Mule 3.
When to Use a Flow
A flow is the most versatile and powerful integration mechanism available in Mule.
Flows are valuable in many situations, including:
- Simple integration tasks
- Scheduled data processing
- Connecting cloud and on-premise applications
- Event processing where multiple services need to be composed
Before you create flows be sure to take a look at the ready-to-use configuration patterns. They can save you a great amount of configuration effort for common use cases.
The Anatomy of a Flow
A flow is in essence just a chain of Message Processors. Think of each Message Processor as a Lego block where a Flow is something you build with them. A flow also has a Message Source, the message source is the source of messages that are processed by the Message Processor chain.
A Flow is configured in XML using the <flow> element. Each flow has a name attribute, a message source (unless it's a private flow), one or more message processors and an optional exception strategy.
Flows seem simple, yet can be quite powerful. In particular, when combined with expressions in Mule, they can allow for very sophisticated processing of the message contents. There are many elements that leverage expressions, including:
When a message is received or generated by the message source the flow is started and the configured message processors are invoked in a chain in the same order as they are configured. Some message processors will accept child message processor elements, in this case these will be processed before returning and continuing processing the main list.
The above describes the behavior when the flow is one-way. If the flow is request-response because an inbound endpoint as a request-response exchange pattern defined then the result of the flow execution is return to the inbound endpoint and then in turn to the callee. If there are no <response> blocks in your flow and if none of the configured message processors perform any response processing then the response used is simply the result from the last Message Processor in the flow. If a <response> block is used then any message processors configured in this element will be used to process the response message. Some message processors (e.g. CXF) perform processing of the response message as part of their default configuration.
Note: When the last element in the flow configuration is a one-way <outbound-endpoint> there's no result of it's execution so the returned payload of the message is going to be NullPayload. If the one-way <outbound-endpoint> is followed by another processor it will receive as input the same message that the outbound-endpoint received instead of NullPayload.
Private Flows are therefore only used if they are referenced from another construct running in the same Mule instance. When configuring Mule using XML the <flow-ref> element is used to include one flow in another.
A private Flow differs from the use of a "Processor Chain" in that a Flow has it's own context and Exception Strategy where as when a processor chain is referenced it is executed in the context of the flow that references it.
You can read more about the reason we added Flow for Mule 3 in the following blog posts:
Mule 3 Architecture, Part 1: Back to Basics
Mule 3 Architecture, Part 2: Introducing the Message Processor