Azure Service Bus client library for Python Samples
NOTE: This document outlines the samples for the latest version of the
azure-servicebus
package which has different APIs than the older version (0.50). Please visit this link for samples of version 0.50 of this library.
These are code samples that show common scenario operations with the Azure Service Bus client library. Both sync version and async version of samples are provided.
- send_queue.py (async version) - Examples to send messages to a service bus queue:
- From a connection string
- Enabling Logging
- send_topic.py (async version) - Examples to send messages to a service bus topic:
- From a connection string
- Enabling Logging
- receive_queue.py (async_version) - Examples to receive messages from a service bus queue:
- Receive messages
- receive_subscription.py (async_version) - Examples to receive messages from a service bus subscription:
- Receive messages
- receive_peek.py (async_version) - Examples to peek messages from a service bus queue:
- Peek messages
- receive_deferred_message_queue.py (async_version) - Examples to defer received messages and receive deferred messages from a service bus queue:
- Defer received messages
- Receive deferred messages
- receive_deadlettered_messages.py (async_version) - Examples to receive dead-lettered messages from a service bus queue:
- Receive dead-lettered messages
- receive_iterator_queue.py (async_version) - Examples to receive messages from a service bus queue by iterating over ServiceBusReceiver:
- Receive messages by iterating over ServiceBusReceiver
- send_and_receive_amqp_annotated_message.py (async_version) - Examples to send AMQPAnnotatedMessage to and receive messages from a service bus queue and parse the body:
- Send AMQPAnnotatedMessage of different body types.
- Receive messages and parse the body according to the body type.
- session_pool_receive.py (async_version) - Examples to receive messages from multiple available sessions in parallel with a thread pool:
- Receive messages from multiple available sessions in parallel with a thread pool
- Automatically renew the lock on the session through AutoLockRenewer
- session_send_receive.py (async_version) - Examples to send messages to and receive messages from a session-enabled service bus queue:
- Send messages to a session-enabled queue
- Receive messages from session-enabled queue
- schedule_messages_and_cancellation (async_version) - Examples to schedule messages and cancel scheduled messages on a service bus queue:
- Schedule a single message or multiple messages to a queue
- Cancel scheduled messages from a queue
- schedule_topic_messages_and_cancellation (async_version) - Examples to schedule messages and cancel scheduled messages on a service bus topic:
- Schedule a single message or multiple messages to a topic
- Cancel scheduled messages from a topic
- client_identity_authentication.py (async_version) - Examples to authenticate the client by Azure Activate Directory:
- Authenticate and create the client utilizing the
azure.identity
library
- Authenticate and create the client utilizing the
- authenticate_client_connstr.py (async_version) - Examples to authenticate the client by Connection String:
- Authenticate and create the client utilizing the connection string available in the Azure portal or via Azure CLI.
- authenticate_using_azure_sas_credential.py (async_version) - Examples to authenticate the client by
azure.core.credentials.AzureSasCredential
:- Authenticate and create the client utilizing the
azure.core.credentials.AzureSasCredential
.
- Authenticate and create the client utilizing the
- authenticate_using_azure_named_key_credential.py (async_version) - Examples to authenticate the client by
azure.core.credentials.AzureNamedKeyCredential
:- Authenticate and create the client utilizing the
azure.core.credentials.AzureNamedKeyCredential
.
- Authenticate and create the client utilizing the
- proxy.py (async_version) - Examples to send message behind a proxy:
- Send message behind a proxy
- auto_lock_renew.py (async_version) - Examples to show usage of AutoLockRenewer:
- Automatically renew lock on message received from non-sessionful entity
- Automatically renew lock on the session of sessionful entity
- Configure a callback to be triggered on auto lock renew failures.
- mgmt_queue.py (async_version) - Examples to manage queue entities under a given servicebus namespace:
- Create a queue
- Delete a queue
- Update a queue
- List queues
- Get queue properties
- Get queue runtime information
- mgmt_topic (async_version) - Examples to manage topic entities under a given servicebus namespace:
- Create a topic
- Delete a topic
- Update a topic
- List topic
- Get topic properties
- Get topic runtime information
- mgmt_subscription (async_version) - Examples to manage subscription entities under a given servicebus namespace:
- Create a subscription
- Delete a subscription
- Update a subscription
- List subscription
- Get subscription properties
- Get subscription runtime information
- mgmt_rule (async_version) - Examples to manage rule entities under a given servicebus subscription:
- Create a rule
- Delete a rule
- Update a rule
- List rule
- Get rule properties
- failure_and_recovery.py - A demonstration of potential failure modes from an end-to-end send receive flow, as well as possible recovery patterns.
- deadletter_messages_and_correct.py (async_version) - Comprehensive example of moving messages to the dead-letter queue, retrieving messages from it, and resubmitting corrected messages back into main queue.
- topic_subscription_with_rule_operations.py (async_version) - Example to manage rules on topic subscriptions and to explore different forms of subscription filters.
- connection_to_custom_endpoint_address (async_version) - Example of connecting to the service using a custom endpoint.
Prerequisites
- Python 3.8 or later.
- Microsoft Azure Subscription: To use Azure services, including Azure Service Bus, you'll need a subscription. If you do not have an existing Azure account, you may sign up for a free trial or use your MSDN subscriber benefits when you create an account.
Setup
- Install the Azure Service Bus client library for Python with pip:
pip install azure-servicebus
To run samples that utilize the Azure Active Directory for authentication, please install the azure-identity
library:
pip install azure-identity
- Clone or download this sample repository.
- Open the sample folder in Visual Studio Code or your IDE of choice.
Running the samples
- Open a terminal window and
cd
to the directory that the samples are saved in. - Set the environment variables specified in the sample file you wish to run.
- Follow the usage described in the file, e.g.
python send_queue.py
.
Next steps
Check out the API reference documentation to learn more about what you can do with the Azure Service Bus client library.