You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Interaction with Service Bus starts with an instance of the `ServiceBusClient` class. You either need a **connection string with SAS key**, or a **namespace** and one of its **account keys** to instantiate the client object.
45
45
46
-
#### Get credentials
46
+
#### Create client from connection string
47
47
48
-
Use the [Azure CLI][azure_cli] snippet below to populate an environment variable with the service bus connection string (you can also find these values in the [Azure portal][azure_portal]. The snippet is formatted for the Bash shell.
48
+
- Get credentials: Use the [Azure CLI][azure_cli] snippet below to populate an environment variable with the service bus connection string (you can also find these values in the [Azure Portal][azure_portal] by following the step-by-step guide to [Get a service bus connection string][get_servicebus_conn_str]). The snippet is formatted for the Bash shell.
with ServiceBusClient(FULLY_QUALIFIED_NAMESPACE, credential):
80
+
...
81
+
```
82
+
83
+
- This constructor takes the fully qualified namespace of your Service Bus instance and a credential that implements the
84
+
[TokenCredential][token_credential_interface]
85
+
protocol. There are implementations of the `TokenCredential` protocol available in the
86
+
[azure-identity package][pypi_azure_identity]. The fully qualified namespace is of the format `<yournamespace.servicebus.windows.net>`.
87
+
- When using Azure Active Directory, your principal must be assigned a role which allows access to Service Bus, such as the
88
+
Azure Service Bus Data Owner role. For more information about using Azure Active Directory authorization with Service Bus,
89
+
please refer to [the associated documentation][servicebus_aad_authentication].
90
+
71
91
Note: client can be initialized without a context manager, but must be manually closed via client.close() to not leak resources.
72
92
73
93
## Key concepts
@@ -88,22 +108,22 @@ To interact with these resources, one should be familiar with the following SDK
88
108
89
109
*[Sender](./azure/servicebus/_servicebus_sender.py): To send messages to a Queue or Topic, one would use the corresponding `get_queue_sender` or `get_topic_sender` method off of a `ServiceBusClient` instance as seen [here](./samples/sync_samples/send_queue.py).
90
110
91
-
*[Receiver](./azure/servicebus/_servicebus_receiver.py): To receive messages from a Queue or Subscription, one would use the corrosponding`get_queue_receiver` or `get_subscription_receiver` method off of a `ServiceBusClient` instance as seen [here](./samples/sync_samples/receive_queue.py).
111
+
*[Receiver](./azure/servicebus/_servicebus_receiver.py): To receive messages from a Queue or Subscription, one would use the corresponding`get_queue_receiver` or `get_subscription_receiver` method off of a `ServiceBusClient` instance as seen [here](./samples/sync_samples/receive_queue.py).
92
112
93
113
*[Message](./azure/servicebus/_common/message.py): When sending, this is the type you will construct to contain your payload. When receiving, this is where you will access the payload and control how the message is "settled" (completed, dead-lettered, etc); these functions are only available on a received message.
94
114
95
115
## Examples
96
116
97
117
The following sections provide several code snippets covering some of the most common Service Bus tasks, including:
98
118
99
-
*[Send a message to a queue](#send-to-a-queue)
100
-
*[Receive a message from a queue](#receive-from-a-queue)
101
-
*[Defer a message on receipt](#defer-a-message)
119
+
*[Send a message to a queue](#send-a-message-to-a-queue)
120
+
*[Receive a message from a queue](#receive-a-message-from-a-queue)
121
+
*[Defer a message on receipt](#defer-a-message-on-receipt)
102
122
103
123
To perform management tasks such as creating and deleting queues/topics/subscriptions, please utilize the azure-mgmt-servicebus library, available [here][servicebus_management_repository].
104
124
105
125
106
-
### Send to a queue
126
+
### Send a message to a queue
107
127
108
128
This example sends a message to a queue that is assumed to already exist, created via the Azure portal or az commands.
109
129
@@ -121,7 +141,7 @@ with ServiceBusClient.from_connection_string(connstr) as client:
121
141
sender.send(message)
122
142
```
123
143
124
-
### Receive from a queue
144
+
### Receive a message from a queue
125
145
126
146
To receive from a queue, you can either perform a one-off receive via "receiver.receive()" or receive persistently as follows:
127
147
@@ -139,7 +159,7 @@ with ServiceBusClient.from_connection_string(connstr) as client:
139
159
msg.complete()
140
160
```
141
161
142
-
### Defer a message
162
+
### Defer a message on receipt
143
163
144
164
When receiving from a queue, you have multiple actions you can take on the messages you receive. Where the prior example completes a message,
145
165
permanently removing it from the queue and marking as complete, this example demonstrates how to defer the message, sending it back to the queue
@@ -150,6 +170,7 @@ from azure.servicebus import ServiceBusClient
150
170
151
171
import os
152
172
connstr = os.environ['SERVICE_BUS_CONN_STR']
173
+
queue_name = os.environ['SERVICE_BUS_QUEUE_NAME']
153
174
154
175
with ServiceBusClient.from_connection_string(connstr) as client:
155
176
with client.get_queue_receiver(queue_name) as receiver:
0 commit comments