Message Dispatcher
Use the Message Dispatcher pattern to coordinate which performer receives each message when several equivalent performers can process the same request.
The pattern is implemented at the point where the integration receives a message and must choose one processing endpoint. Keep the dispatch state close to the entry point, update it before the outbound call, and send the message to the selected performer through a connector.
Stateful round-robin dispatch
Use stateful round-robin dispatch when each incoming message should be sent to the next processor in a fixed set. Store the current processor index in the service, update it with a lock, and call the selected processor through an HTTP client connection. The lock keeps the index update consistent when multiple requests arrive at the same time. For constructs that do not have a full visual representation, switch to pro-code through the Flow Diagram editor.
- Visual Designer
- Ballerina Code
- Create an HTTP service for the dispatcher entry point.
- Add a
GETresource, such as/process, and define a query parameter that carries the message reference, such asresourceUrl. See resource inputs. - Add the outbound processor HTTP connection. Configure its base URL with a configurable variable.
- Add a service-level variable named
nextProcessorwith typeintand default value0. - In the resource flow, add the processor selection logic as a Ballerina code block: read
nextProcessor, advance it inside alock, and store the selected processor ID. - Add the HTTP connector call that includes the selected processor ID in the request path and passes the message reference as a query parameter.
- Return the processor response from the resource function.
service / on new http:Listener(8080) {
int nextProcessor = 0;
isolated resource function get process(string resourceUrl)
returns ProcessingResponse|error {
int currentProcessor;
lock {
currentProcessor = self.nextProcessor;
self.nextProcessor = currentProcessor == processorIds.length() - 1
? 0
: currentProcessor + 1;
}
string processorId = processorIds[currentProcessor];
return check processorClient->/[processorId]/process(resourceUrl = resourceUrl);
}
}