Skip to main content

Actions

The ballerinax/asb package exposes the following clients:

ClientPurpose
Message SenderSend individual, batch, and scheduled messages to Azure Service Bus queues and topics.
Message ReceiverReceive messages from queues and subscriptions, settle messages, and manage locks.
AdministratorManage Azure Service Bus entities: create, get, update, delete, and list queues, topics, subscriptions, and rules.

For event-driven integration, see the Trigger Reference.


Message sender

Send individual, batch, and scheduled messages to Azure Service Bus queues and topics.

Configuration

FieldTypeDefaultDescription
connectionStringstringRequiredThe Azure Service Bus connection string.
entityType`QUEUETOPIC`Required
topicOrQueueNamestringRequiredThe name of the queue or topic.
amqpRetryOptionsAmqpRetryOptions()Retry options for AMQP operations (maxRetries, delay, maxDelay, tryTimeout, retryMode).

Initializing the client

import ballerinax/asb;

configurable string connectionString = ?;

asb:MessageSender sender = check new ({
connectionString: connectionString,
entityType: asb:QUEUE,
topicOrQueueName: "my-queue"
});

Operations

Send messages

send

Sends a single message to the configured queue or topic.

Parameters:

NameTypeRequiredDescription
messageasb:MessageYesThe message to send, including body, optional contentType, messageId, sessionId, timeToLive, and application properties.

Returns: error?

Sample code:

check sender->send({
body: "Hello from Ballerina",
contentType: asb:TEXT,
applicationProperties: {"source": "ballerina"}
});
sendPayload

Sends a message with just the payload content to the configured queue or topic. The payload is stored in the message as a byte stream.

Parameters:

NameTypeRequiredDescription
payloadanydataYesThe payload to send as the message body.

Returns: error?

Sample code:

check sender->sendPayload("Order processed successfully");
sendBatch

Sends a batch of messages to the configured queue or topic.

Parameters:

NameTypeRequiredDescription
messageBatchasb:MessageBatchYesA batch of messages to send, containing an array of asb:Message records.

Returns: error?

Sample code:

check sender->sendBatch({
messages: [
{body: "Message 1", contentType: asb:TEXT},
{body: "Message 2", contentType: asb:TEXT}
]
});

Scheduling

schedule

Schedules a message for future delivery at the specified time.

Parameters:

NameTypeRequiredDescription
messageasb:MessageYesThe message to schedule.
scheduledEnqueueTimetime:CivilYesThe UTC time at which the message should become available.

Returns: int|error

Sample code:

int sequenceNumber = check sender->schedule(
{body: "Scheduled message", contentType: asb:TEXT},
{year: 2026, month: 3, day: 13, hour: 10, minute: 0}
);
cancel

Cancels a previously scheduled message using its sequence number.

Parameters:

NameTypeRequiredDescription
sequenceNumberintYesThe sequence number returned by the schedule operation.

Returns: error?

Sample code:

check sender->cancel(sequenceNumber);

Connection

close

Closes the sender connection and releases resources.

No parameters.

Returns: error?

Sample code:

check sender->close();

Message receiver

Receive messages from queues and subscriptions, settle messages, and manage locks.

Configuration

FieldTypeDefaultDescription
connectionStringstringRequiredThe Azure Service Bus connection string.
entityConfigQueueConfig|TopicSubsConfigRequiredEntity configuration: either {queueName: "..."} for queues or {topicName: "...", subscriptionName: "..."} for topic subscriptions.
receiveMode`PEEK_LOCKRECEIVE_AND_DELETE`PEEK_LOCK
maxAutoLockRenewDurationint300Maximum duration (in seconds) to automatically renew the message lock.
amqpRetryOptionsAmqpRetryOptions()Retry options for AMQP operations.

Initializing the client

import ballerinax/asb;

configurable string connectionString = ?;

asb:MessageReceiver receiver = check new ({
connectionString: connectionString,
entityConfig: {
queueName: "my-queue"
},
receiveMode: asb:PEEK_LOCK
});

Operations

Receive messages

receive

Receives a single message from the queue or subscription.

Parameters:

NameTypeRequiredDescription
serverWaitTimeint?NoMaximum wait time in seconds for a message to arrive. Defaults to 60.
deadLetteredbooleanNoWhen true, receives from the dead-letter sub-queue instead of the main queue or subscription. Defaults to false.

Returns: asb:Message|error?

Returns () if no message arrives within serverWaitTime. Always assign to a nilable type.

Sample code:

asb:Message? message = check receiver->receive(serverWaitTime = 60);
if message is () {
// No message available
return;
}
receivePayload

Receives the message from the configured queue or subscription directly bound to the expected payload type.

Parameters:

NameTypeRequiredDescription
serverWaitTimeint?NoMaximum wait time in seconds for a message to arrive. Defaults to 60.
Ttypedesc<anydata>NoThe expected payload type.
deadLetteredbooleanNoWhen true, receives from the dead-letter sub-queue instead of the main queue or subscription. Defaults to false.

Returns: T|error

Sample code:

string payload = check receiver->receivePayload(serverWaitTime = 60);
receiveBatch

Receives a batch of messages from the queue or subscription.

Parameters:

NameTypeRequiredDescription
maxMessageCountintYesMaximum number of messages to receive in the batch.
serverWaitTimeint?NoMaximum wait time in seconds. Defaults to () (no wait).
deadLetteredbooleanNoWhen true, receives from the dead-letter sub-queue instead of the main queue or subscription. Defaults to false.

Returns: asb:MessageBatch|error?

Returns () if no messages are available. Always assign to a nilable type.

Sample code:

asb:MessageBatch? batch = check receiver->receiveBatch(maxMessageCount = 10);
if batch is () {
// No messages available
return;
}

Message settlement

complete

Completes a message, removing it from the queue. Used in PEEK_LOCK mode.

Parameters:

NameTypeRequiredDescription
messageasb:MessageYesThe message to complete.

Returns: error?

Sample code:

asb:Message? message = check receiver->receive(serverWaitTime = 60);
if message is () {
return;
}
check receiver->complete(message);
abandon

Abandons a message, releasing the lock so it can be received again.

Parameters:

NameTypeRequiredDescription
messageasb:MessageYesThe message to abandon.

Returns: error?

Sample code:

check receiver->abandon(message);
deadLetter

Moves a message to the dead-letter sub-queue.

Parameters:

NameTypeRequiredDescription
messageasb:MessageYesThe message to dead-letter.
deadLetterReasonstring?NoThe reason for dead-lettering.
deadLetterErrorDescriptionstring?NoA description of the error that caused dead-lettering.

Returns: error?

Sample code:

check receiver->deadLetter(message,
deadLetterReason = "ProcessingFailure",
deadLetterErrorDescription = "Unable to parse message body"
);
defer

Defers a message so it can only be received by its sequence number later.

Parameters:

NameTypeRequiredDescription
messageasb:MessageYesThe message to defer.

Returns: int|error

The returned int is the sequence number of the deferred message. Pass it to receiveDeferred to retrieve the message later.

Sample code:

int sequenceNumber = check receiver->defer(message);
// Later, retrieve the deferred message:
asb:Message? deferred = check receiver->receiveDeferred(sequenceNumber);
receiveDeferred

Receives a previously deferred message by its sequence number.

Parameters:

NameTypeRequiredDescription
sequenceNumberintYesThe sequence number of the deferred message.

Returns: asb:Message|error?

Sample code:

asb:Message? deferredMsg = check receiver->receiveDeferred(sequenceNumber);

Lock management

renewLock

Renews the lock on a message in PEEK_LOCK mode to extend processing time.

Parameters:

NameTypeRequiredDescription
messageasb:MessageYesThe message whose lock should be renewed.

Returns: error?

Sample code:

check receiver->renewLock(message);

Connection

close

Closes the receiver connection and releases resources.

No parameters.

Returns: error?

Sample code:

check receiver->closeReceiver();

Administrator

Manage Azure Service Bus entities: create, get, update, delete, and list queues, topics, subscriptions, and rules.

Configuration

FieldTypeDefaultDescription
connectionStringstringRequiredThe Azure Service Bus connection string with Manage rights.

Initializing the client

import ballerinax/asb;

configurable string connectionString = ?;

asb:Administrator admin = check new ({
connectionString: connectionString
});

Operations

Queue management

createQueue

Creates a new queue in the Azure Service Bus namespace.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe name of the queue to create.
queuePropertiesCreateQueueOptions?NoOptional properties such as max size, TTL, lock duration, and dead-lettering settings.

Returns: QueueProperties|error

Sample code:

asb:QueueProperties queue = check admin->createQueue("my-new-queue");

Sample response:

{"name": "my-new-queue", "status": "Active", "maxSizeInMegabytes": 1024}
getQueue

Retrieves the properties of an existing queue.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe name of the queue.

Returns: QueueProperties|error

Sample code:

asb:QueueProperties queue = check admin->getQueue("my-queue");
updateQueue

Updates the properties of an existing queue.

Parameters:

NameTypeRequiredDescription
queuePropertiesQueuePropertiesYesThe updated queue properties.

Returns: QueueProperties|error

Sample code:

asb:QueueProperties queue = check admin->getQueue("my-queue");
asb:QueueProperties updated = check admin->updateQueue({
...queue,
maxDeliveryCount: 15
});
deleteQueue

Deletes a queue from the namespace.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe name of the queue to delete.

Returns: error?

Sample code:

check admin->deleteQueue("my-old-queue");
listQueues

Lists all queues in the Azure Service Bus namespace.

Parameters:

NameTypeRequiredDescription

Returns: QueueProperties[]|error

Sample code:

asb:QueueProperties[] queues = check admin->listQueues();
queueExists

Checks whether a queue with the given name exists.

Parameters:

NameTypeRequiredDescription
queueNamestringYesThe name of the queue to check.

Returns: boolean|error

Sample code:

boolean exists = check admin->queueExists("my-queue");

Topic management

createTopic

Creates a new topic in the Azure Service Bus namespace.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the topic to create.
topicPropertiesCreateTopicOptions?NoOptional properties such as max size, TTL, and duplicate detection.

Returns: TopicProperties|error

Sample code:

asb:TopicProperties topic = check admin->createTopic("my-new-topic");

Sample response:

{"name": "my-new-topic", "status": "Active", "maxSizeInMegabytes": 1024}
getTopic

Retrieves the properties of an existing topic.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the topic.

Returns: TopicProperties|error

Sample code:

asb:TopicProperties topic = check admin->getTopic("my-topic");
updateTopic

Updates the properties of an existing topic.

Parameters:

NameTypeRequiredDescription
topicPropertiesTopicPropertiesYesThe updated topic properties.

Returns: TopicProperties|error

Sample code:

asb:TopicProperties topic = check admin->getTopic("my-topic");
asb:TopicProperties updated = check admin->updateTopic({
...topic,
defaultMessageTimeToLive: 3600
});
deleteTopic

Deletes a topic from the namespace.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the topic to delete.

Returns: error?

Sample code:

check admin->deleteTopic("my-old-topic");
listTopics

Lists all topics in the Azure Service Bus namespace.

Parameters:

NameTypeRequiredDescription

Returns: TopicProperties[]|error

Sample code:

asb:TopicProperties[] topics = check admin->listTopics();
topicExists

Checks whether a topic with the given name exists.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the topic to check.

Returns: boolean|error

Sample code:

boolean exists = check admin->topicExists("my-topic");

Subscription management

createSubscription

Creates a new subscription under a topic.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription to create.
subscriptionPropertiesCreateSubscriptionOptions?NoOptional subscription properties.

Returns: SubscriptionProperties|error

Sample code:

asb:SubscriptionProperties sub = check admin->createSubscription("my-topic", "my-subscription");

Sample response:

{"subscriptionName": "my-subscription", "topicName": "my-topic", "status": "Active"}
getSubscription

Retrieves the properties of an existing subscription.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription.

Returns: SubscriptionProperties|error

Sample code:

asb:SubscriptionProperties sub = check admin->getSubscription("my-topic", "my-subscription");
updateSubscription

Updates the properties of an existing subscription.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionPropertiesSubscriptionPropertiesYesThe updated subscription properties.

Returns: SubscriptionProperties|error

Sample code:

asb:SubscriptionProperties sub = check admin->getSubscription("my-topic", "my-sub");
asb:SubscriptionProperties updated = check admin->updateSubscription("my-topic", {
...sub,
maxDeliveryCount: 20
});
deleteSubscription

Deletes a subscription from a topic.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription to delete.

Returns: error?

Sample code:

check admin->deleteSubscription("my-topic", "my-old-subscription");
listSubscriptions

Lists all subscriptions under a topic.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.

Returns: SubscriptionProperties[]|error

Sample code:

asb:SubscriptionProperties[] subs = check admin->listSubscriptions("my-topic");
subscriptionExists

Checks whether a subscription exists under a topic.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription to check.

Returns: boolean|error

Sample code:

boolean exists = check admin->subscriptionExists("my-topic", "my-subscription");

Rule management

createRule

Creates a new rule under a topic subscription for message filtering.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription.
ruleNamestringYesThe name of the rule to create.
rulePropertiesCreateRuleOptions?NoOptional rule options including filter and action.

Returns: RuleProperties|error

Sample code:

asb:RuleProperties rule = check admin->createRule("my-topic", "my-sub", "high-priority-filter");
getRule

Retrieves the properties of a rule.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription.
ruleNamestringYesThe name of the rule.

Returns: RuleProperties|error

Sample code:

asb:RuleProperties rule = check admin->getRule("my-topic", "my-sub", "my-rule");
updateRule

Updates the properties of an existing rule.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription.
rulePropertiesRulePropertiesYesThe updated rule properties.

Returns: RuleProperties|error

Sample code:

asb:RuleProperties rule = check admin->getRule("my-topic", "my-sub", "my-rule");
asb:RuleProperties updated = check admin->updateRule("my-topic", "my-sub", rule);
deleteRule

Deletes a rule from a subscription.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription.
ruleNamestringYesThe name of the rule to delete.

Returns: error?

Sample code:

check admin->deleteRule("my-topic", "my-sub", "old-rule");
listRules

Lists all rules under a topic subscription.

Parameters:

NameTypeRequiredDescription
topicNamestringYesThe name of the parent topic.
subscriptionNamestringYesThe name of the subscription.

Returns: RuleProperties[]|error

Sample code:

asb:RuleProperties[] rules = check admin->listRules("my-topic", "my-sub");

What's next

  • Trigger Reference: event-driven integration using asb:Listener
  • Setup Guide: obtain the connection string required for all clients
  • Example: complete worked examples for sender, receiver, and trigger