The `azure-servicebus` library (version 7.14.3) is the Microsoft Azure Service Bus client for Python. It provides high-performance, cloud-managed messaging capabilities for real-time and fault-tolerant communication between distributed senders and receivers. It supports various asynchronous messaging patterns, including structured first-in-first-out messaging, publish/subscribe, and scalable queues and topics. The library is actively maintained with regular releases.
pip install azure-servicebus azure-identity aiohttpVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to send and receive a single message using Azure Service Bus queues. It initializes a `ServiceBusClient` from a connection string, then obtains a sender to send a `ServiceBusMessage` and a receiver to receive and complete a `ServiceBusReceivedMessage`. For production, using `azure-identity` with `DefaultAzureCredential` is recommended over connection strings. Ensure the `AZURE_SERVICEBUS_CONNECTION_STRING` and `AZURE_SERVICEBUS_QUEUE_NAME` environment variables are set.
Migrate code to the new API patterns. Refer to the official Azure SDK for Python migration guides for detailed steps. Primarily, `ServiceBusClient` is now the entry point, and `azure-identity` classes like `DefaultAzureCredential` are used for authentication instead of connection strings directly in the client constructor.
Implement robust error handling and retry mechanisms. Ensure message settlement (complete, abandon, defer, dead-letter) is performed promptly. For session-enabled entities, be prepared to re-accept sessions if a `SessionLockLost` exception occurs. Consider adjusting lock durations and prefetch counts.
Treat `ServiceBusClient` instances as singletons where possible, reusing a single client instance throughout the application's lifetime. The `ServiceBusClient` manages connections for all objects created from it (senders, receivers, processors).
Monitor `ThrottledRequests` and `IncomingRequests` metrics in Azure. Implement back-off and retry policies in your client code to gracefully handle throttling. Consider upgrading to a higher Service Bus tier or distributing load across multiple namespaces if quotas are consistently hit.
Ensure proper `with` statement usage for `ServiceBusClient`, `ServiceBusSender`, and `ServiceBusReceiver` to guarantee correct closure and resource release. Avoid holding references to messages or client objects after they have been settled or explicitly closed.
Verify the Service Bus connection string or the fully qualified namespace name for any typos. Ensure that the application's environment has proper DNS resolution capabilities and network connectivity to Azure Service Bus endpoints.
Verify the spelling of the Service Bus namespace hostname (e.g., `your-namespace.servicebus.windows.net`) in your connection string or the `fully_qualified_namespace` parameter. Ensure any environment variables providing the hostname are correct. Check the network environment for proper DNS configuration and connectivity to Azure endpoints.