Service Activator
Use a Service Activator to expose the same application operation through a message channel and a direct service interface.
The pattern is implemented by keeping the business operation in a reusable function and placing thin protocol-specific adapters around it. A service resource handles non-messaging callers, while a broker listener activates the same function when a message arrives.
Request-reply service activation
Use request-reply service activation when a message sender expects the activated service to return a result. Define typed request and response records, implement the operation as a reusable function, and call that function from both the direct resource function and the broker request handler.
- Visual Designer
- Ballerina Code
- Define the request and response records in Types.
- Add the reusable operation as a Function and select Make visible across the project when the function must be called from multiple artifacts.
- Create an HTTP service for non-messaging callers.
- Add a resource function with the typed request payload and add a Call Function step that invokes the reusable operation.
- Add a RabbitMQ event integration for message-based callers.
- Configure the RabbitMQ queue and listener values with configurable variables.
- Add the
onRequesthandler from RabbitMQ event handlers, define the expected message content, add a Call Function step, and return the function result.
function activateService(ActivationRequest request) returns ActivationResult|error {
return {
requestId: request.requestId,
status: "accepted"
};
}
service /requests on new http:Listener(8080) {
resource function post activate(ActivationRequest request)
returns ActivationResult|error {
return activateService(request);
}
}
@rabbitmq:ServiceConfig {
queueName: "service-requests"
}
service rabbitmq:Service on activatorListener {
remote function onRequest(rabbitmq:AnydataMessage message)
returns ActivationResult|error {
ActivationRequest request = check message.content.ensureType();
return activateService(request);
}
}
One-way command activation
Use one-way command activation when the message channel only needs to trigger the operation and does not need a service result in the reply path. The broker handler validates the message content, calls the shared function, and lets the handler return successfully after the command is accepted.
- Visual Designer
- Ballerina Code
- Reuse the request and response records from Types.
- Reuse the same Function that contains the application operation.
- Add a RabbitMQ event integration for the command queue.
- Configure the listener, queue, and authentication fields with configurable variables.
- Add the
onMessagehandler from RabbitMQ event handlers and define the expected message content. - In the handler flow, add a Call Function step for the shared operation and add any flow-level error handling required by the command contract.
@rabbitmq:ServiceConfig {
queueName: "service-commands"
}
service rabbitmq:Service on commandListener {
remote function onMessage(rabbitmq:AnydataMessage message) returns error? {
ActivationRequest request = check message.content.ensureType();
_ = check activateService(request);
}
}