Skip to main content

Build an Event-Driven Integration

TimeUnder 10 minutes
What you'll buildAn event-driven integration that consumes messages from Orders queue in RabbitMQ broker and processes them.

Event integrations are designed for reactive workflows triggered by messages from a broker. This quick start demonstrates the complete flow: creating a RabbitMQ message listener, adding an event handler to process messages, and implementing the integration logic executed when a message is received.

Prerequisites​

A working WSO2 Integrator environment.

WSO2 Cloud setup

Sign up and launch WSO2 Integrator in the browser-based cloud editor.

RabbitMQ

A running instance, or start one with Docker:

Step 1: Create the integration​

Note

In the cloud editor, you're already inside a project. Skip to Step 2.

  1. Open WSO2 Integrator.
  2. Select the Create New Integration card.
  3. Set Integration Name to OrderProcessor.
  4. Set Project Name to event-integration.
  5. Select Create Integration.
Create a New Integration ProjectCreate a New Integration Project

Step 2: Add a RabbitMQ event listener​

  1. Select your integration from the project overview canvas.
  2. Select + Add Artifact in the design canvas.
  3. Select RabbitMQ under Event Integration.
  4. Set Host to localhost and Port to 5672 (update these if your RabbitMQ instance runs elsewhere).
  5. Set Queue Name to Orders.
  6. Select Create.
Add a RabbitMQ Event Integration ArtifactAdd a RabbitMQ Event Integration Artifact

Step 3: Add onMessage event handler​

  1. In the RabbitMQ service design view, select + Add Handler.
  2. Select onMessage.
  3. Select Save.
Add onMessage Event HandlerAdd onMessage Event Handler

Step 4: Add message processing logic​

  1. Select + inside the resource flow.
  2. Select Call Function.
  3. Select printInfo under log.
  4. Set Msg to Received order.
  5. Select Save.
Add Message Processing LogicAdd Message Processing Logic

Step 5: Run and test the integration​

  1. Select Run.
  2. The integration starts and listens for messages on the Orders queue.
  3. Open the RabbitMQ Management UI at http://localhost:15672 (default credentials: guest/guest).
    • Go to Queues → Orders → Publish message, enter any text as the payload, and select Publish message.
    • Confirm the integration log displays Received order.
Run and Test the IntegrationRun and Test the Integration

Step 6: Deploy to WSO2 Cloud​

Deploy your integration to WSO2 Cloud. Check Save and deploy.

Skip ahead: deploy a ready-made sample

Rather not build it yourself? One-click deploy the OrderProcessor sample straight to WSO2 Cloud.

Deploy to WSO2 Cloud

Source code​

View the generated Ballerina code

The steps above generate the following complete, runnable Ballerina program:

import ballerina/log;
import ballerinax/rabbitmq;

listener rabbitmq:Listener rabbitmqListener = new ("localhost", 5672);

service "Orders" on rabbitmqListener {
remote function onMessage(rabbitmq:AnydataMessage message, rabbitmq:Caller caller) returns error? {
do {
log:printInfo("Received order");
} on fail error err {
// handle error
return error("unhandled error", err);
}
}
}

Supported event sources​

BrokerBallerina Package
Apache Kafkaballerinax/kafka
RabbitMQballerinax/rabbitmq
MQTTballerinax/mqtt
Azure Service Busballerinax/azure.servicebus
Salesforceballerinax/salesforce
GitHub Webhooksballerinax/github

Learn more​

  • Kafka — Consume and produce Kafka messages
  • Azure Service Bus — Integrate with Azure Service Bus queues and topics
  • RabbitMQ — Full RabbitMQ listener and publisher reference
  • MQTT — Handle MQTT messages from IoT and messaging devices
  • CDC for PostgreSQL — React to database changes with change data capture