Skip to main content

Actions

The ballerinax/rabbitmq package exposes the following clients:

ClientPurpose
ClientPublish messages, consume messages, and manage queues and exchanges on a RabbitMQ broker.

For event-driven integration, see the Trigger Reference.


Client

Publish messages, consume messages, and manage queues and exchanges on a RabbitMQ broker.

Configuration

FieldTypeDefaultDescription
hoststringRequiredRabbitMQ server hostname (constructor parameter).
portintRequiredRabbitMQ server AMQP port (constructor parameter).
authCredentials()Username and password credentials for authentication.
virtualHoststring()The virtual host to connect to.
connectionTimeoutdecimal()Connection timeout in seconds.
handshakeTimeoutdecimal()TLS handshake timeout in seconds.
shutdownTimeoutdecimal()Shutdown timeout in seconds.
heartbeatdecimal()Heartbeat interval in seconds.
validationbooleantrueEnable constraint validation for messages.
secureSocketSecureSocket()TLS/SSL configuration for secure connections.

Initializing the client

import ballerinax/rabbitmq;

configurable string host = "localhost";
configurable int port = 5672;

rabbitmq:Client rabbitmqClient = check new (host, port);

Operations

Queue management

queueDeclare

Declares a queue on the RabbitMQ server. Creates the queue if it does not exist.

Parameters:

NameTypeRequiredDescription
namestringYesThe name of the queue to declare.
configQueueConfig?NoQueue configuration (durable, exclusive, autoDelete, arguments).

Returns: Error?

Sample code:

check rabbitmqClient->queueDeclare("OrderQueue", config = {
durable: true,
autoDelete: false
});
queueAutoGenerate

Declares a queue with a server-generated unique name and returns the name.

Parameters:

NameTypeRequiredDescription

Returns: string|error

Sample code:

string generatedQueueName = check rabbitmqClient->queueAutoGenerate();

Sample response:

"amq.gen-Xa2Kh5Q7F3mR1s0T"
queueDelete

Deletes a queue from the RabbitMQ server.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe name of the queue to delete.
ifUnusedbooleanNoDelete only if the queue has no consumers.
ifEmptybooleanNoDelete only if the queue is empty.

Returns: Error?

Sample code:

check rabbitmqClient->queueDelete("OrderQueue");
queuePurge

Removes all messages from a queue without deleting the queue itself.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe name of the queue to purge.

Returns: Error?

Sample code:

check rabbitmqClient->queuePurge("OrderQueue");

Exchange management

exchangeDeclare

Declares an exchange on the RabbitMQ server.

Parameters:

NameTypeRequiredDescription
namestringYesThe name of the exchange to declare.
exchangeTypeExchangeTypeNoThe exchange type: DIRECT_EXCHANGE, FANOUT_EXCHANGE, TOPIC_EXCHANGE, or HEADERS_EXCHANGE.
configExchangeConfig?NoExchange configuration (durable, autoDelete, arguments).

Returns: Error?

Sample code:

check rabbitmqClient->exchangeDeclare("OrderExchange", rabbitmq:TOPIC_EXCHANGE, config = {
durable: true
});
exchangeDelete

Deletes an exchange from the RabbitMQ server.

Parameters:

NameTypeRequiredDescription
exchangeNamestringYesThe name of the exchange to delete.

Returns: Error?

Sample code:

check rabbitmqClient->exchangeDelete("OrderExchange");
queueBind

Binds a queue to an exchange with a routing/binding key.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe name of the queue to bind.
exchangeNamestringYesThe name of the exchange to bind to.
bindingKeystringYesThe binding key for the queue-exchange binding.

Returns: Error?

Sample code:

check rabbitmqClient->queueBind("OrderQueue", "OrderExchange", "orders.#");

Publish

publishMessage

Publishes a message to a queue or exchange with the specified routing key.

Parameters:

NameTypeRequiredDescription
messageAnydataMessageYesThe message to publish, containing content, routingKey, and optionally exchange and properties.

Returns: Error?

Sample code:

check rabbitmqClient->publishMessage({
content: "Hello from Ballerina",
routingKey: "OrderQueue"
});

Consume

consumeMessage

Synchronously retrieves a single message from a queue, including metadata.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe queue to consume from.
autoAckbooleanNoIf true, the message is automatically acknowledged.
Ttypedesc<AnydataMessage>NoExpected message type for data binding.

Returns: AnydataMessage|error

Sample code:

rabbitmq:AnydataMessage message = check rabbitmqClient->consumeMessage("OrderQueue");

Sample response:

{"content": "Hello from Ballerina", "routingKey": "OrderQueue", "exchange": "", "deliveryTag": 1}
consumePayload

Synchronously retrieves only the payload content from a queue message.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe queue to consume from.
autoAckbooleanNoIf true, the message is automatically acknowledged.
Ttypedesc<anydata>NoExpected payload type for data binding.

Returns: anydata|error

Sample code:

string payload = check rabbitmqClient->consumePayload("OrderQueue");

Sample response:

"Hello from Ballerina"

Acknowledgement

basicAck

Acknowledges one or more received messages by delivery tag or message reference.

Parameters:

NameTypeRequiredDescription
ackTarget`AnydataMessageint`Yes
multiplebooleanNoIf true, acknowledges all messages up to and including the given delivery tag.

Returns: Error?

Sample code:

rabbitmq:AnydataMessage message = check rabbitmqClient->consumeMessage("OrderQueue", autoAck = false);
check rabbitmqClient->basicAck(message);
basicNack

Rejects one or more received messages, optionally requeuing them.

Parameters:

NameTypeRequiredDescription
ackTarget`AnydataMessageint`Yes
multiplebooleanNoIf true, rejects all messages up to and including the given delivery tag.
requeuebooleanNoIf true, rejected messages are requeued rather than discarded.

Returns: Error?

Sample code:

rabbitmq:AnydataMessage message = check rabbitmqClient->consumeMessage("OrderQueue", autoAck = false);
check rabbitmqClient->basicNack(message, requeue = true);

Connection lifecycle

close

Closes the RabbitMQ client channel and connection gracefully.

Parameters:

NameTypeRequiredDescription
closeCodeint?NoThe close code to send to the server.
closeMessagestring?NoThe close message to send to the server.

Returns: Error?

Sample code:

check rabbitmqClient->close();
abort

Force-closes the client connection, discarding any pending operations or exceptions.

Parameters:

NameTypeRequiredDescription
closeCodeint?NoThe close code.
closeMessagestring?NoThe close message.

Returns: Error?

Sample code:

check rabbitmqClient->'abort();