rabbitmq
Module rabbitmq
API
Declarations

ballerinax/rabbitmq Ballerina library
Clients
rabbitmq: Caller
Provides the functionality to manipulate the messages received by the consumer services.
basicAck
Acknowledges one or several received messages.
check caller->basicAck(true);
Parameters
- multiple boolean (default false) - Set to
true
to acknowledge all messages up to and including the called on message andfalse
to acknowledge just the called on message
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred
basicNack
Rejects one or several received messages.
check caller->basicNack(true, requeue = false);
Parameters
- multiple boolean (default false) - Set to
true
to reject all messages up to and including the called on message andfalse
to reject just the called on message
- requeue boolean (default true) -
true
if the rejected message(s) should be re-queued rather than discarded/dead-lettered
Return Type
- Error? - A
rabbitmq:Error
if an I/O error is encountered or else()
rabbitmq: Client
The Ballerina interface to provide AMQP Channel related functionality.
Constructor
Initializes a rabbitmq:Client
object.
rabbitmq:Client rabbitmqClient = check new(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT);
init (string host, int port, *ConnectionConfiguration connectionData)
- host string - The host used for establishing the connection
- port int - The port used for establishing the connection
- connectionData *ConnectionConfiguration - The connection configurations
queueDeclare
function queueDeclare(string name, QueueConfig? config) returns Error?
Declares a non-exclusive, auto-delete, and non-durable queue with the given configurations.
check rabbitmqClient->queueDeclare("MyQueue");
Parameters
- name string - The name of the queue
- config QueueConfig? (default ()) - The configurations required to declare a queue
Return Type
- Error? -
()
if the queue was successfully generated or else arabbitmq:Error
if an I/O error occurred
queueAutoGenerate
Declares a queue with a server-generated name.
string queueName = check rabbitmqClient->queueAutoGenerate();
exchangeDeclare
function exchangeDeclare(string name, ExchangeType exchangeType, ExchangeConfig? config) returns Error?
Declares a non-auto-delete, non-durable exchange with no extra arguments. If the arguments are specified, then the exchange is declared accordingly.
check rabbitmqClient->exchangeDeclare("MyExchange", rabbitmq:DIRECT_EXCHANGE);
Parameters
- name string - The name of the exchange
- exchangeType ExchangeType (default DIRECT_EXCHANGE) - The type of the exchange
- config ExchangeConfig? (default ()) - The configurations required to declare an exchange
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
queueBind
Binds a queue to an exchange with the given binding key.
check rabbitmqClient->queueBind("MyQueue", "MyExchange", "routing-key");
Parameters
- queueName string - The name of the queue
- exchangeName string - The name of the exchange
- bindingKey string - The binding key used to bind the queue to the exchange
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
publishMessage
function publishMessage(AnydataMessage message) returns Error?
Publishes a message. Publishing to a non-existent exchange will result in a channel-level protocol error, which closes the channel.
check rabbitmqClient->publishMessage(messageInBytes, "MyQueue");
Parameters
- message AnydataMessage - The message to be published
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
consumeMessage
function consumeMessage(string queueName, boolean autoAck, typedesc<AnydataMessage> T) returns T|Error
Retrieves a message synchronously from the given queue providing direct access to the messages in the queue.
rabbitmq:AnydataMessage message = check rabbitmqClient->consumeMessage("MyQueue");
Parameters
- queueName string - The name of the queue
- autoAck boolean (default true) - If false, should manually acknowledge
- T typedesc<AnydataMessage> (default <>) - Optional type description of the required data type
Return Type
- T|Error - A
rabbitmq:AnydataMessage
object containing the retrieved message data or else arabbitmq:Error
if an I/O error occurred
consumePayload
Retrieves the payload synchronously from the given queue.
string message = check rabbitmqClient->consumePayload("MyQueue");
Parameters
- queueName string - The name of the queue
- autoAck boolean (default true) - If false, should manually acknowledge
- T typedesc<anydata> (default <>) - Optional type description of the required data type
Return Type
- T|Error - Message payload in the required format if executed successfully or else a
rabbitmq:Error
basicAck
function basicAck(AnydataMessage message, boolean multiple) returns Error?
Acknowledges one or several received messages.
check rabbitmqClient->basicAck(<message>);
Parameters
- message AnydataMessage - The message to be acknowledged
- multiple boolean (default false) - Set to
true
to acknowledge all messages up to and including the called on message andfalse
to acknowledge just the called on message
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
basicNack
function basicNack(AnydataMessage message, boolean multiple, boolean requeue) returns Error?
Rejects one or several received messages.
check rabbitmqClient->basicNack(<message>);
Parameters
- message AnydataMessage - The message to be rejected
- multiple boolean (default false) - Set to
true
to reject all messages up to and including the called on message andfalse
to reject just the called on message
- requeue boolean (default true) -
true
if the rejected message(s) should be re-queued rather than discarded/dead-lettered
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
queueDelete
Deletes the queue with the given name although it is in use or has messages in it.
If the ifUnused
or ifEmpty
parameters are given, the queue is checked before deleting.
check rabbitmqClient->queueDelete("MyQueue");
Parameters
- queueName string - The name of the queue to be deleted
- ifUnused boolean (default false) - True if the queue should be deleted only if it's not in use
- ifEmpty boolean (default false) - True if the queue should be deleted only if it's empty
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
exchangeDelete
Deletes the exchange with the given name.
check rabbitmqClient->exchangeDelete("MyExchange");
Parameters
- exchangeName string - The name of the exchange
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
queuePurge
Purges the content of the given queue.
check rabbitmqClient->queuePurge("MyQueue");
Parameters
- queueName string - The name of the queue
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
close
Closes the rabbitmq:Client
.
check rabbitmqClient->close();
Parameters
- closeCode int? (default ()) - The close code (for information, go to the [Reply Codes] (#https://www.rabbitmq.com/resources/specs/amqp0-9-1.pdf))
- closeMessage string? (default ()) - A message indicating the reason for closing the channel
Return Type
- Error? - A
rabbitmq:Error
if an I/O error occurred or else()
'abort
Aborts the RabbitMQ rabbitmq:Client
. Forces the rabbitmq:Client
to close and waits for all the close operations
to complete. Any encountered exceptions in the close operations are discarded silently.
check rabbitmqClient->'abort(320, "Client Aborted");
Parameters
- closeCode int? (default ()) - The close code (for information, go to the [Reply Codes] (#https://www.rabbitmq.com/resources/specs/amqp0-9-1.pdf))
- closeMessage string? (default ()) - A message indicating the reason for closing the channel
Return Type
- Error? - A
rabbitmq:Error
if an I/O error is encountered or else()
Service types
rabbitmq: Service
The RabbitMQ service type.
Constants
rabbitmq: DEFAULT_HOST
Constant for the default host.
rabbitmq: DEFAULT_PORT
Constant for the default port.
rabbitmq: DIRECT_EXCHANGE
Constant for the RabbitMQ Direct Exchange type.
rabbitmq: FANOUT_EXCHANGE
Constant for the RabbitMQ Fan-out Exchange type.
rabbitmq: TOPIC_EXCHANGE
Constant for the RabbitMQ Topic Exchange type.
Enums
rabbitmq: Protocol
Represents protocol options.
Members
Listeners
rabbitmq: Listener
Ballerina RabbitMQ Message Listener. Provides a listener to consume messages from the RabbitMQ server.
Constructor
Initializes a Listener object with the given connection configuration. Sets the global QoS settings,
which will be applied to the entire rabbitmq:Listener
.
rabbitmq:Listener rabbitmqListener = check new(rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT);
init (string host, int port, QosSettings? qosSettings, *ConnectionConfiguration connectionData)
- host string - The host used for establishing the connection
- port int - The port used for establishing the connection
- qosSettings QosSettings? () - The consumer prefetch settings
- connectionData *ConnectionConfiguration - The connection configuration
attach
Attaches the service to the rabbitmq:Listener
endpoint.
check rabbitmqListener.attach(service, "serviceName");
Parameters
- s Service - The type descriptor of the service
Return Type
- error? -
()
or else arabbitmq:Error
upon failure to register the service
'start
function 'start() returns error?
Starts consuming the messages on all the attached services.
check rabbitmqListener.'start();
Return Type
- error? -
()
or else arabbitmq:Error
upon failure to start
detach
Stops consuming messages and detaches the service from the rabbitmq:Listener
endpoint.
check rabbitmqListener.detach(service);
Parameters
- s Service - The type descriptor of the service
Return Type
- error? -
()
or else arabbitmq:Error
upon failure to detach the service
gracefulStop
function gracefulStop() returns error?
Stops consuming messages through all consumer services by terminating the connection and all its channels.
check rabbitmqListener.gracefulStop();
Return Type
- error? -
()
or else arabbitmq:Error
upon failure to close theChannelListener
immediateStop
function immediateStop() returns error?
Stops consuming messages through all the consumer services and terminates the connection with the server.
check rabbitmqListener.immediateStop();
Return Type
- error? -
()
or else arabbitmq:Error
upon failure to close ChannelListener.
Annotations
rabbitmq: Payload
The annotation which is used to define the payload parameter in the onMessage
service method.
rabbitmq: ServiceConfig
The annotation, which is used to configure the subscription.
Records
rabbitmq: AnydataMessage
Represents the anydata message, which a RabbitMQ server sends to its subscribed services.
Fields
- content anydata - The content of the message
- routingKey string - The routing key to which the message is sent
- exchange string(default "") - The exchange to which the message is sent. The default exchange is a direct exchange with no name (empty string) pre-declared by the broker.
- deliveryTag? int - The delivery tag of the message
- properties? BasicProperties - Basic properties of the message - routing headers etc.
rabbitmq: BasicProperties
Basic properties of the message - routing headers etc.
Fields
- replyTo? string - The queue name to which the reply should be sent
- contentType? string - The content type of the message
- contentEncoding? string - The content encoding of the message
- correlationId? string - The client-specific ID that can be used to mark or identify messages between clients
- headers? map<anydata> - A map of additional arbitrary headers to be included in the message
rabbitmq: BytesMessage
Represents the subtype of rabbitmq:AnydataMessage
record where the message content is a byte array.
Fields
- Fields Included from *AnydataMessage
- content anydata
- routingKey string
- exchange string
- deliveryTag int
- properties BasicProperties
- content byte[] - Message content in bytes
rabbitmq: CertKey
Represents combination of certificate, private key and private key password if encrypted.
Fields
- certFile string - A file containing the certificate
- keyFile string - A file containing the private key in PKCS8 format
- keyPassword? string - Password of the private key if it is encrypted
rabbitmq: ConnectionConfiguration
Configurations related to initializing the RabbitMQ client and listener.
Fields
- username? string - The username used for establishing the connection
- password? string - The password used for establishing the connection
- virtualHost? string - The virtual host to use when connecting to the broker
- connectionTimeout? decimal - Connection TCP establishment timeout in seconds and zero for infinite
- handshakeTimeout? decimal - The AMQP 0-9-1 protocol handshake timeout in seconds
- shutdownTimeout? decimal - Shutdown timeout in seconds, zero for infinite, and the default value is 10. If the consumers exceed this timeout, then any remaining queued deliveries (and other Consumer callbacks) will be lost
- heartbeat? decimal - The initially-requested heartbeat timeout in seconds and zero for none
- validation boolean(default true) - Configuration related to constraint validation check
- secureSocket? SecureSocket - Configurations for facilitating secure connections
- auth? Credentials - Configurations related to authentication
rabbitmq: Credentials
Configurations related to authentication.
Fields
- username string - Username to use for authentication
- password string - Password/secret/token to use for authentication
rabbitmq: ExchangeConfig
Additional configurations used to declare an exchange.
Fields
- durable boolean(default false) - Set to
true
if a durable exchange is declared
- autoDelete boolean(default false) - Set to
true
if an auto-delete exchange is declared
- arguments? map<anydata> - Other properties (construction arguments) for the queue
rabbitmq: QosSettings
QoS settings to limit the number of unacknowledged messages on a channel.
Fields
- prefetchCount int - The maximum number of messages that the server will deliver. Give the value as 0 if unlimited
- prefetchSize? int - The maximum amount of content (measured in octets) that the server will deliver and 0 if unlimited
- global boolean(default false) -
true
if the settings should be shared among all the consumers
rabbitmq: QueueConfig
Additional configurations used to declare a queue.
Fields
- durable boolean(default false) - Set to true if declaring a durable queue
- exclusive boolean(default false) - Set to true if declaring an exclusive queue
- autoDelete boolean(default true) - Set to true if declaring an auto-delete queue
- arguments? map<anydata> - Other properties (construction arguments) of the queue
rabbitmq: RabbitmqPayload
Defines the Payload remote function parameter.
rabbitmq: RabbitMQServiceConfig
Configurations required to create a subscription.
Fields
- queueName string - The name of the queue to be subscribed
- config? QueueConfig - The configurations required to declare a queue
- autoAck boolean(default true) - If false, should manually acknowledge
rabbitmq: SecureSocket
Configurations for facilitating secure connections.
Fields
- cert TrustStore|string - Configurations associated with
crypto:TrustStore
or single certificate file that the client trusts
- key? KeyStore|CertKey - Configurations associated with
crypto:KeyStore
or combination of certificate and private key of the client
- protocol? record {| name Protocol |} - SSL/TLS protocol related options
- verifyHostName boolean(default true) - Enable/disable host name verification
Errors
rabbitmq: Error
Represents the RabbitMQ module related errors.
rabbitmq: PayloadBindingError
Represents an error, which occurred due to payload binding.
rabbitmq: PayloadValidationError
Represents an error, which occurred due to payload constraint validation.
Union types
rabbitmq: ExchangeType
ExchangeType
Types of exchanges supported by the Ballerina RabbitMQ Connector.
Import
import ballerinax/rabbitmq;
Metadata
Released date: 5 months ago
Version: 3.2.0
License: Apache-2.0
Compatibility
Platform: java21
Ballerina version: 2201.11.0
GraalVM compatible: Yes
Pull count
Total: 0
Current verison: 0
Weekly downloads
Keywords
service
client
messaging
network
pubsub
Contributors