async def message_processing(servicebus_client, queue_name): while True: try: async with servicebus_client.get_queue_receiver( queue_name, max_wait_time=1, session_id=NEXT_AVAILABLE_SESSION) as receiver: renewer = AutoLockRenewer() renewer.register(receiver, receiver.session) await receiver.session.set_state("OPEN") async for message in receiver: print("Message: {}".format(message)) print("Time to live: {}".format(message.time_to_live)) print("Sequence number: {}".format( message.sequence_number)) print("Enqueue Sequence number: {}".format( message.enqueued_sequence_number)) print("Partition Key: {}".format(message.partition_key)) print("Locked until: {}".format(message.locked_until_utc)) print("Lock Token: {}".format(message.lock_token)) print("Enqueued time: {}".format( message.enqueued_time_utc)) await receiver.complete_message(message) if str(message) == 'shutdown': await receiver.session.set_state("CLOSED") break await renewer.close() except OperationTimeoutError: print( "If timeout occurs during connecting to a session," "It indicates that there might be no non-empty sessions remaining; exiting." "This may present as a UserError in the azure portal metric.") return
async def message_processing(servicebus_client, queue_name): while True: try: async with servicebus_client.get_queue_session_receiver( queue_name, max_wait_time=1) as receiver: renewer = AutoLockRenewer() renewer.register(receiver.session) await receiver.session.set_state("OPEN") async for message in receiver: print("Message: {}".format(message)) print("Time to live: {}".format(message.time_to_live)) print("Sequence number: {}".format( message.sequence_number)) print("Enqueue Sequence number: {}".format( message.enqueued_sequence_number)) print("Partition Key: {}".format(message.partition_key)) print("Locked until: {}".format(message.locked_until_utc)) print("Lock Token: {}".format(message.lock_token)) print("Enqueued time: {}".format( message.enqueued_time_utc)) await message.complete() if str(message) == 'shutdown': await receiver.session.set_state("CLOSED") break await renewer.close() except NoActiveSession: print( "There are no non-empty sessions remaining; exiting. This may present as a UserError in the azure portal." ) return
async def renew_lock_on_session_of_the_sessionful_entity(): servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR) async with servicebus_client: async with servicebus_client.get_queue_sender(queue_name=SESSION_QUEUE_NAME) as sender: msgs_to_send = [ServiceBusMessage("session message: {}".format(i), session_id='SESSION') for i in range(10)] await sender.send_messages(msgs_to_send) print('Send messages to sessionful queue.') renewer = AutoLockRenewer() async with servicebus_client.get_queue_receiver( queue_name=SESSION_QUEUE_NAME, session_id='SESSION', prefetch_count=10 ) as receiver: # automatically renew the lock on the session for 100 seconds renewer.register(receiver.session, timeout=100) print('Register session into AutoLockRenewer.') received_msgs = await receiver.receive_messages(max_message_count=10, max_wait_time=5) await asyncio.sleep(100) # message handling for long period (E.g. application logic) for msg in received_msgs: await msg.complete() print('Complete messages.')
async def renew_lock_on_message_received_from_non_sessionful_entity(): servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR) async with servicebus_client: async with servicebus_client.get_queue_sender(queue_name=QUEUE_NAME) as sender: msgs_to_send = [ServiceBusMessage("session message: {}".format(i)) for i in range(10)] await sender.send_messages(msgs_to_send) print('Send messages to non-sessionful queue.') # Can also be called via "with AutoLockRenewer() as renewer" to automate shutdown. renewer = AutoLockRenewer() async with servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, prefetch_count=10) as receiver: received_msgs = await receiver.receive_messages(max_message_count=10, max_wait_time=5) for msg in received_msgs: # automatically renew the lock on each message for 100 seconds renewer.register(msg, timeout=100) print('Register messages into AutoLockRenewer done.') await asyncio.sleep(100) # message handling for long period (E.g. application logic) for msg in received_msgs: await msg.complete() print('Complete messages.') await renewer.close()
async def example_session_ops_async(): servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] queue_name = os.environ['SERVICE_BUS_SESSION_QUEUE_NAME'] session_id = "<your session id>" async with ServiceBusClient.from_connection_string( conn_str=servicebus_connection_str) as servicebus_client: async with servicebus_client.get_queue_sender( queue_name=queue_name) as sender: await sender.send_messages( ServiceBusMessage('msg', session_id=session_id)) # [START get_session_async] async with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session # [END get_session_async] # [START get_session_state_async] async with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session session_state = await session.get_state() # [END get_session_state_async] # [START set_session_state_async] async with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session await session.set_state("START") # [END set_session_state_async] # [START session_renew_lock_async] async with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session await session.renew_lock() # [END session_renew_lock_async] # [START auto_lock_renew_session_async] from azure.servicebus.aio import AutoLockRenewer lock_renewal = AutoLockRenewer() async with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session # Auto renew session lock for 2 minutes lock_renewal.register(receiver, session, max_lock_renewal_duration=120) async for message in receiver: await process_message(message) await receiver.complete_message(message) # [END auto_lock_renew_session_async] break
async def example_send_and_receive_async(): servicebus_sender = await example_create_servicebus_sender_async() servicebus_receiver = await example_create_servicebus_receiver_async() from azure.servicebus import ServiceBusMessage # [START send_async] async with servicebus_sender: message = ServiceBusMessage("Hello World") await servicebus_sender.send_messages(message) # [END send_async] # [START create_batch_async] async with servicebus_sender: batch_message = await servicebus_sender.create_message_batch() batch_message.add_message( ServiceBusMessage("Single message inside batch")) # [END create_batch_async] # [START peek_messages_async] async with servicebus_receiver: messages = await servicebus_receiver.peek_messages() for message in messages: print(str(message)) # [END peek_messages_async] # [START receive_async] async with servicebus_receiver: messages = await servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: print(str(message)) await message.complete() # [END receive_async] # [START receive_forever_async] async with servicebus_receiver: async for message in servicebus_receiver.get_streaming_message_iter(): print(str(message)) await message.complete() # [END receive_forever_async] # [START auto_lock_renew_message_async] from azure.servicebus.aio import AutoLockRenewer lock_renewal = AutoLockRenewer() async with servicebus_receiver: async for message in servicebus_receiver: lock_renewal.register(message, timeout=60) await process_message(message) await message.complete()
async def example_send_and_receive_async(): servicebus_sender = await example_create_servicebus_sender_async() servicebus_receiver = await example_create_servicebus_receiver_async() from azure.servicebus import ServiceBusMessage # [START send_async] async with servicebus_sender: message = ServiceBusMessage("Hello World") await servicebus_sender.send_messages(message) # [END send_async] await servicebus_sender.send_messages( [ServiceBusMessage("Hello World")] * 5) # [START create_batch_async] async with servicebus_sender: batch_message = await servicebus_sender.create_message_batch() batch_message.add_message( ServiceBusMessage("Single message inside batch")) # [END create_batch_async] # [START peek_messages_async] async with servicebus_receiver: messages = await servicebus_receiver.peek_messages() for message in messages: print(str(message)) # [END peek_messages_async] # [START receive_async] async with servicebus_receiver: messages = await servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: print(str(message)) await servicebus_receiver.complete_message(message) # [END receive_async] # [START receive_forever_async] async with servicebus_receiver: async for message in servicebus_receiver.get_streaming_message_iter(): print(str(message)) await servicebus_receiver.complete_message(message) # [END receive_forever_async] break # [START abandon_message_async] messages = await servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: await servicebus_receiver.abandon_message(message) # [END abandon_message_async] # [START complete_message_async] messages = await servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: await servicebus_receiver.complete_message(message) # [END complete_message_async] # [START defer_message_async] messages = await servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: await servicebus_receiver.defer_message(message) # [END defer_message_async] # [START dead_letter_message_async] messages = await servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: await servicebus_receiver.dead_letter_message(message) # [END dead_letter_message_async] # [START renew_message_lock_async] messages = await servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: await servicebus_receiver.renew_message_lock(message) # [END renew_message_lock_async] # [START auto_lock_renew_message_async] from azure.servicebus.aio import AutoLockRenewer lock_renewal = AutoLockRenewer() async with servicebus_receiver: async for message in servicebus_receiver: lock_renewal.register(servicebus_receiver, message, max_lock_renewal_duration=60) await process_message(message) await servicebus_receiver.complete_message(message) # [END auto_lock_renew_message_async] break await lock_renewal.close()