@@ -119,12 +119,14 @@ This example sends single message and array of messages to a queue that is assum
119119
120120``` python
121121from azure.servicebus import ServiceBusClient, ServiceBusMessage
122+ from azure.identity import DefaultAzureCredential
122123
123124import os
124- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
125+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
125126queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
126127
127- with ServiceBusClient.from_connection_string(connstr) as client:
128+ credential = DefaultAzureCredential()
129+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
128130 with client.get_queue_sender(queue_name) as sender:
129131 # Sending a single message
130132 single_message = ServiceBusMessage(" Single message" )
@@ -147,12 +149,14 @@ To receive from a queue, you can either perform an ad-hoc receive via `receiver.
147149
148150``` python
149151from azure.servicebus import ServiceBusClient
152+ from azure.identity import DefaultAzureCredential
150153
151154import os
152- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
155+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
153156queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
154157
155- with ServiceBusClient.from_connection_string(connstr) as client:
158+ credential = DefaultAzureCredential()
159+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
156160 # max_wait_time specifies how long the receiver should wait with no incoming messages before stopping receipt.
157161 # Default is None; to receive forever.
158162 with client.get_queue_receiver(queue_name, max_wait_time = 30 ) as receiver:
@@ -172,12 +176,14 @@ with ServiceBusClient.from_connection_string(connstr) as client:
172176
173177``` python
174178from azure.servicebus import ServiceBusClient
179+ from azure.identity import DefaultAzureCredential
175180
176181import os
177- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
182+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
178183queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
179184
180- with ServiceBusClient.from_connection_string(connstr) as client:
185+ credential = DefaultAzureCredential()
186+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
181187 with client.get_queue_receiver(queue_name) as receiver:
182188 received_message_array = receiver.receive_messages(max_wait_time = 10 ) # try to receive a single message within 10 seconds
183189 if received_message_array:
@@ -201,13 +207,15 @@ Sessions provide first-in-first-out and single-receiver semantics on top of a qu
201207
202208``` python
203209from azure.servicebus import ServiceBusClient, ServiceBusMessage
210+ from azure.identity import DefaultAzureCredential
204211
205212import os
206- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
213+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
207214queue_name = os.environ[' SERVICE_BUS_SESSION_QUEUE_NAME' ]
208215session_id = os.environ[' SERVICE_BUS_SESSION_ID' ]
209216
210- with ServiceBusClient.from_connection_string(connstr) as client:
217+ credential = DefaultAzureCredential()
218+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
211219 with client.get_queue_sender(queue_name) as sender:
212220 sender.send_messages(ServiceBusMessage(" Session Enabled Message" , session_id = session_id))
213221
@@ -229,13 +237,16 @@ and of how these differ from queues.
229237
230238``` python
231239from azure.servicebus import ServiceBusClient, ServiceBusMessage
240+ from azure.identity import DefaultAzureCredential
241+
232242
233243import os
234- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
244+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
235245topic_name = os.environ[' SERVICE_BUS_TOPIC_NAME' ]
236246subscription_name = os.environ[' SERVICE_BUS_SUBSCRIPTION_NAME' ]
237247
238- with ServiceBusClient.from_connection_string(connstr) as client:
248+ credential = DefaultAzureCredential()
249+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
239250 with client.get_topic_sender(topic_name) as sender:
240251 sender.send_messages(ServiceBusMessage(" Data" ))
241252
@@ -264,12 +275,15 @@ Declares the message processing to be successfully completed, removing the messa
264275
265276``` python
266277from azure.servicebus import ServiceBusClient
278+ from azure.identity import DefaultAzureCredential
279+
267280
268281import os
269- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
282+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
270283queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
271284
272- with ServiceBusClient.from_connection_string(connstr) as client:
285+ credential = DefaultAzureCredential()
286+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
273287 with client.get_queue_receiver(queue_name) as receiver:
274288 for msg in receiver:
275289 print (str (msg))
@@ -282,12 +296,15 @@ Abandon processing of the message for the time being, returning the message imme
282296
283297``` python
284298from azure.servicebus import ServiceBusClient
299+ from azure.identity import DefaultAzureCredential
300+
285301
286302import os
287- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
303+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
288304queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
289305
290- with ServiceBusClient.from_connection_string(connstr) as client:
306+ credential = DefaultAzureCredential()
307+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
291308 with client.get_queue_receiver(queue_name) as receiver:
292309 for msg in receiver:
293310 print (str (msg))
@@ -300,12 +317,15 @@ Transfer the message from the primary queue into a special "dead-letter sub-queu
300317
301318``` python
302319from azure.servicebus import ServiceBusClient
320+ from azure.identity import DefaultAzureCredential
321+
303322
304323import os
305- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
324+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
306325queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
307326
308- with ServiceBusClient.from_connection_string(connstr) as client:
327+ credential = DefaultAzureCredential()
328+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
309329 with client.get_queue_receiver(queue_name) as receiver:
310330 for msg in receiver:
311331 print (str (msg))
@@ -319,12 +339,15 @@ by setting it aside such that it must be received by sequence number in a call t
319339
320340``` python
321341from azure.servicebus import ServiceBusClient
342+ from azure.identity import DefaultAzureCredential
343+
322344
323345import os
324- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
346+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
325347queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
326348
327- with ServiceBusClient.from_connection_string(connstr) as client:
349+ credential = DefaultAzureCredential()
350+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
328351 with client.get_queue_receiver(queue_name) as receiver:
329352 for msg in receiver:
330353 print (str (msg))
@@ -342,14 +365,17 @@ It should be used as follows:
342365
343366``` python
344367from azure.servicebus import ServiceBusClient, AutoLockRenewer
368+ from azure.identity import DefaultAzureCredential
369+
345370
346371import os
347- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
372+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
348373queue_name = os.environ[' SERVICE_BUS_QUEUE_NAME' ]
349374
350375# Can also be called via "with AutoLockRenewer() as renewer" to automate closing.
351376renewer = AutoLockRenewer()
352- with ServiceBusClient.from_connection_string(connstr) as client:
377+ credential = DefaultAzureCredential()
378+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
353379 with client.get_queue_receiver(queue_name) as receiver:
354380 for msg in receiver.receive_messages():
355381 renewer.register(receiver, msg, max_lock_renewal_duration = 60 )
@@ -362,15 +388,18 @@ renewer.close()
362388
363389``` python
364390from azure.servicebus import ServiceBusClient, AutoLockRenewer
391+ from azure.identity import DefaultAzureCredential
392+
365393
366394import os
367- connstr = os.environ[' SERVICE_BUS_CONNECTION_STR ' ]
395+ fully_qualified_namespace = os.environ[' SERVICEBUS_FULLY_QUALIFIED_NAMESPACE ' ]
368396session_queue_name = os.environ[' SERVICE_BUS_SESSION_QUEUE_NAME' ]
369397session_id = os.environ[' SERVICE_BUS_SESSION_ID' ]
370398
371399# Can also be called via "with AutoLockRenewer() as renewer" to automate closing.
372400renewer = AutoLockRenewer()
373- with ServiceBusClient.from_connection_string(connstr) as client:
401+ credential = DefaultAzureCredential()
402+ with ServiceBusClient(fully_qualified_namespace, credential) as client:
374403 with client.get_queue_receiver(session_queue_name, session_id = session_id) as receiver:
375404 renewer.register(receiver, receiver.session, max_lock_renewal_duration = 300 ) # Duration for how long to maintain the lock for, in seconds.
376405
0 commit comments