Orchestrators: Declarative Workflow Automation
Learn how to build predefined and dynamic workflows using Orchestrator
While connecting handlers with channels works great for linear workflows, and Sagas excel at stateful processes, Orchestrator is perfect when you need predefined workflows where the workflow definition is separate from the individual steps.
You'll know you need this when:
You have multi-step business processes (order fulfillment, payment processing, onboarding flows) and the workflow logic is scattered across event handlers
Business stakeholders ask "what are the steps in this process?" and the answer requires reading multiple files
You need to add, remove, or reorder steps in a process and it touches code in many places
Different inputs should trigger different step sequences (e.g., digital vs. physical product fulfillment)
You want each step independently testable and reusable across different workflows
Think of Orchestrator as: A conductor that knows the entire symphony (workflow) and tells each musician (step) when to play, while the musicians focus only on their part.
Creating Your First Orchestrator
An Orchestrator defines a workflow as a sequence of steps (channel names) and implements those steps as internal handlers.
Step 1: Define the Workflow
class ImageProcessingOrchestrator
{
#[Orchestrator(inputChannelName: "process.image")]
public function processImage(): array
{
return [
"resize.image",
"add.watermark",
"optimize.image",
"upload.image"
];
}
}Key parts:
#[Orchestrator]- Tells Ecotone this method defines a workflowinputChannelName- Channel that triggers this workflowReturn array - List of steps (channel names) to execute in order
Durability comes from the routing slip. Each step is an #[InternalHandler] running on its own channel, and the remaining step list travels with the message as a routing slip header. If the worker crashes at step N, the broker redelivers the message; the next consumer reads the remaining steps from the slip and resumes at step N β no replay of completed steps, no orchestrator state to restore. The orchestrator method is a plan, executed step-by-step through your channels.
Step 2: Implement the Steps
What happens when you trigger the workflow:
Message sent to
process.imagechannelOrchestrator returns
["resize.image", "add.watermark", "optimize.image", "upload.image"]Each step executes in sequence, passing data to the next step
Final result is returned
Data Enrichment with Headers
Sometimes you need to add metadata or context without changing the main payload. Use changingHeaders: true for this:
Enriching with Additional Data
Benefits of header enrichment:
Keep original payload unchanged
Add context data for downstream steps
Maintain clean separation of concerns
Executing Orchestrators
There are several ways to trigger orchestrator workflows:
Method 1: Command Handler with Output Channel
Flow:
UploadImageCommandsent to command handlerHandler processes upload and returns
ImageDataResult automatically sent to
process.imagechannelOrchestrator workflow begins
Method 2: From Event Handlers (Business Workflows)
Flow:
OrderPlacedevent occursEvent handler processes it and sends result to
process.orderOrchestrator workflow begins automatically
Method 3: Business Interface Triggering Business Workflow
Business Interface is simple interface where Ecotone delivers implementation. This way we can easily create and entrypoint with interface that is part of our application level code and execute the workflow:
Usage in your application:
Method 4: Custom Orchestrator Gateway
For dynamic workflows where you want to pass the steps programmatically:
Gateway benefits:
Dynamic workflow construction
Runtime step determination
Easy integration with web controllers
Flexible business rule application
Asynchronous Orchestration
Make your workflows asynchronous for better performance and scalability:
Asynchronous Orchestrator
Mixed Synchronous/Asynchronous Steps
Advanced Features
Dynamic Workflow Building
The power of Orchestrator shines when you build workflows dynamically based on business rules:
Conditional Step Execution
Steps can return null to end the workflow early:
Nested Orchestrators
Orchestrators can call other orchestrators as steps:
Testing Orchestrators
Testing orchestrators is straightforward with Ecotone Lite. You can test the entire workflow, individual steps, or specific scenarios.
Testing Individual Steps
Testing Data Enrichment
Testing Asynchronous Orchestrators
Testing Orchestrator Gateways
Key Benefits of Orchestrator
π― Separation of Concerns
Workflow definition is separate from step implementation
Easy to understand the entire process at a glance
Steps can be reused across different workflows
π Reusability
Same steps can be used in multiple workflows
Build libraries of reusable business operations
Mix and match steps for different scenarios
β‘ Dynamic Workflows
Build workflows programmatically based on business rules
Adapt to different customer types, regions, or conditions
Runtime workflow construction
π§ͺ Testability
Test entire workflows end-to-end
Test individual steps in isolation
Easy mocking and stubbing of dependencies
π Scalability
Asynchronous execution support
Individual steps can be scaled independently
Easy to add new steps without changing existing code
π Observability
Clear workflow execution path
Easy to monitor and debug
Step-by-step execution tracking
Summary
Orchestrator is perfect for building predefined workflows where you want to:
π― Separate workflow definition from step implementation
π Reuse steps across different workflows
β‘ Build dynamic workflows based on business rules
π§ͺ Test workflows and steps independently
π Execute consistent, repeatable business processes
The power of Orchestrator lies in its ability to make complex business workflows simple to define, easy to test, and flexible to modify. Whether you're processing orders, onboarding customers, or handling document workflows, Orchestrator provides the structure and flexibility you need to build robust, maintainable business processes.
Last updated
Was this helpful?