async def test_async_session_schedule_message(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs): async with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, logging_enable=False) as sb_client: import uuid session_id = str(uuid.uuid4()) enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0) async with sb_client.get_queue_sender(servicebus_queue.name) as sender: content = str(uuid.uuid4()) message_id = uuid.uuid4() message = Message(content, session_id=session_id) message.properties.message_id = message_id message.scheduled_enqueue_time_utc = enqueue_time await sender.send_messages(message) messages = [] renewer = AutoLockRenew() async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver: renewer.register(receiver.session, timeout=140) messages.extend(await receiver.receive_messages(max_wait_time=120)) messages.extend(await receiver.receive_messages(max_wait_time=5)) if messages: data = str(messages[0]) assert data == content assert messages[0].properties.message_id == message_id assert messages[0].scheduled_enqueue_time_utc == enqueue_time assert messages[0].scheduled_enqueue_time_utc == messages[0].enqueued_time_utc.replace(microsecond=0) assert len(messages) == 1 else: raise Exception("Failed to receive schdeduled message.") await renewer.shutdown()
async def test_async_session_schedule_message(live_servicebus_config, session_queue): client = ServiceBusClient( service_namespace=live_servicebus_config['hostname'], shared_access_key_name=live_servicebus_config['key_name'], shared_access_key_value=live_servicebus_config['access_key'], debug=True) import uuid session_id = str(uuid.uuid4()) queue_client = client.get_queue(session_queue) enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0) async with queue_client.get_sender(session=session_id) as sender: content = str(uuid.uuid4()) message_id = uuid.uuid4() message = Message(content) message.properties.message_id = message_id message.schedule(enqueue_time) await sender.send(message) messages = [] renewer = AutoLockRenew() async with queue_client.get_receiver(session=session_id) as receiver: renewer.register(receiver, timeout=140) messages.extend(await receiver.fetch_next(timeout=120)) messages.extend(await receiver.fetch_next(timeout=5)) if messages: data = str(messages[0]) assert data == content assert messages[0].properties.message_id == message_id assert messages[0].scheduled_enqueue_time == enqueue_time assert messages[0].scheduled_enqueue_time == messages[0].enqueued_time.replace(microsecond=0) assert len(messages) == 1 else: raise Exception("Failed to receive schdeduled message.") await renewer.shutdown()
async def message_processing(servicebus_client, queue_name): while True: try: async with servicebus_client.get_queue_session_receiver(queue_name, idle_timeout=1) as receiver: renewer = AutoLockRenew() renewer.register(receiver.session) await receiver.session.set_session_state("OPEN") async for message in receiver: print("Message: {}".format(message)) print("Time to live: {}".format(message.header.time_to_live)) print("Sequence number: {}".format(message.sequence_number)) print("Enqueue Sequence number: {}".format(message.enqueue_sequence_number)) print("Partition ID: {}".format(message.partition_id)) 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_session_state("CLOSED") break await renewer.shutdown() except NoActiveSession: print("There are no non-empty sessions remaining; exiting. This may present as a UserError in the azure portal.") return
async def test_async_session_schedule_message(live_servicebus_config, session_queue): client = ServiceBusClient( service_namespace=live_servicebus_config['hostname'], shared_access_key_name=live_servicebus_config['key_name'], shared_access_key_value=live_servicebus_config['access_key'], debug=True) import uuid session_id = str(uuid.uuid4()) queue_client = client.get_queue(session_queue) enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0) async with queue_client.get_sender(session=session_id) as sender: content = str(uuid.uuid4()) message_id = uuid.uuid4() message = Message(content) message.properties.message_id = message_id message.schedule(enqueue_time) await sender.send(message) messages = [] renewer = AutoLockRenew() async with queue_client.get_receiver(session=session_id) as receiver: renewer.register(receiver, timeout=140) messages.extend(await receiver.fetch_next(timeout=120)) messages.extend(await receiver.fetch_next(timeout=5)) if messages: data = str(messages[0]) assert data == content assert messages[0].properties.message_id == message_id assert messages[0].scheduled_enqueue_time == enqueue_time assert messages[0].scheduled_enqueue_time == messages[0].enqueued_time.replace(microsecond=0) assert len(messages) == 1 else: raise Exception("Failed to receive schdeduled message.") await renewer.shutdown()
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 = [ Message("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 AutoLockRenew() as renewer" to automate shutdown. renewer = AutoLockRenew() async with servicebus_client.get_queue_receiver( queue_name=QUEUE_NAME, prefetch=10) as receiver: received_msgs = await receiver.receive_messages(max_batch_size=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 AutoLockRenew 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 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 = [ Message("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 = AutoLockRenew() async with servicebus_client.get_queue_session_receiver( queue_name=SESSION_QUEUE_NAME, session_id='SESSION', prefetch=10) as receiver: # automatically renew the lock on the session for 100 seconds renewer.register(receiver.session, timeout=100) print('Register session into AutoLockRenew.') received_msgs = await receiver.receive_messages(max_batch_size=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 test_async_session_cancel_scheduled_messages(live_servicebus_config, session_queue): client = ServiceBusClient( service_namespace=live_servicebus_config['hostname'], shared_access_key_name=live_servicebus_config['key_name'], shared_access_key_value=live_servicebus_config['access_key'], debug=True) session_id = str(uuid.uuid4()) queue_client = client.get_queue(session_queue) enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0) async with queue_client.get_sender(session=session_id) as sender: message_a = Message("Test scheduled message") message_b = Message("Test scheduled message") tokens = await sender.schedule(enqueue_time, message_a, message_b) assert len(tokens) == 2 await sender.cancel_scheduled_messages(*tokens) renewer = AutoLockRenew() messages = [] async with queue_client.get_receiver(session=session_id) as receiver: renewer.register(receiver, timeout=140) messages.extend(await receiver.fetch_next(timeout=120)) messages.extend(await receiver.fetch_next(timeout=5)) try: assert len(messages) == 0 except AssertionError: for m in messages: print(str(m)) await m.complete() raise await renewer.shutdown()
async def test_async_session_cancel_scheduled_messages(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs): async with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, logging_enable=False) as sb_client: session_id = str(uuid.uuid4()) enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0) async with sb_client.get_queue_sender(servicebus_queue.name) as sender: message_a = Message("Test scheduled message", session_id=session_id) message_b = Message("Test scheduled message", session_id=session_id) tokens = await sender.schedule_messages([message_a, message_b], enqueue_time) assert len(tokens) == 2 await sender.cancel_scheduled_messages(tokens) renewer = AutoLockRenew() messages = [] async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id) as receiver: renewer.register(receiver.session, timeout=140) messages.extend(await receiver.receive_messages(max_wait_time=120)) messages.extend(await receiver.receive_messages(max_wait_time=5)) try: assert len(messages) == 0 except AssertionError: for m in messages: print(str(m)) await m.complete() raise await renewer.close()
async def test_async_session_schedule_multiple_messages(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs): async with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, logging_enable=False) as sb_client: import uuid session_id = str(uuid.uuid4()) enqueue_time = (utc_now() + timedelta(minutes=2)).replace(microsecond=0) messages = [] async with sb_client.get_queue_sender(servicebus_queue.name) as sender: content = str(uuid.uuid4()) message_id_a = uuid.uuid4() message_a = Message(content, session_id=session_id) message_a.message_id = message_id_a message_id_b = uuid.uuid4() message_b = Message(content, session_id=session_id) message_b.message_id = message_id_b tokens = await sender.schedule_messages([message_a, message_b], enqueue_time) assert len(tokens) == 2 renewer = AutoLockRenew() async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, prefetch_count=20) as receiver: renewer.register(receiver.session, timeout=140) messages.extend(await receiver.receive_messages(max_wait_time=120)) messages.extend(await receiver.receive_messages(max_wait_time=5)) if messages: data = str(messages[0]) assert data == content assert messages[0].message_id in (message_id_a, message_id_b) assert messages[0].scheduled_enqueue_time_utc == enqueue_time assert messages[0].scheduled_enqueue_time_utc == messages[0].enqueued_time_utc.replace(microsecond=0) assert len(messages) == 2 else: raise Exception("Failed to receive schdeduled message.") await renewer.close()
async def test_async_session_cancel_scheduled_messages(live_servicebus_config, session_queue): client = ServiceBusClient( service_namespace=live_servicebus_config['hostname'], shared_access_key_name=live_servicebus_config['key_name'], shared_access_key_value=live_servicebus_config['access_key'], debug=True) session_id = str(uuid.uuid4()) queue_client = client.get_queue(session_queue) enqueue_time = (datetime.now() + timedelta(minutes=2)).replace(microsecond=0) async with queue_client.get_sender(session=session_id) as sender: message_a = Message("Test scheduled message") message_b = Message("Test scheduled message") tokens = await sender.schedule(enqueue_time, message_a, message_b) assert len(tokens) == 2 await sender.cancel_scheduled_messages(*tokens) renewer = AutoLockRenew() messages = [] async with queue_client.get_receiver(session=session_id) as receiver: renewer.register(receiver, timeout=140) messages.extend(await receiver.fetch_next(timeout=120)) messages.extend(await receiver.fetch_next(timeout=5)) try: assert len(messages) == 0 except AssertionError: for m in messages: print(str(m)) await m.complete() raise await renewer.shutdown()
async def test_async_session_by_conn_str_receive_handler_with_autolockrenew(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs): async with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, logging_enable=False) as sb_client: session_id = str(uuid.uuid4()) async with sb_client.get_queue_sender(servicebus_queue.name) as sender: for i in range(10): message = Message("{}".format(i), session_id=session_id) await sender.send_messages(message) results = [] async def lock_lost_callback(renewable, error): results.append(renewable) renewer = AutoLockRenew() messages = [] async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5, receive_mode=ReceiveMode.PeekLock, prefetch_count=20) as session: renewer.register(session.session, timeout=60) print("Registered lock renew thread", session.session.locked_until_utc, utc_now()) with pytest.raises(SessionLockExpired): async for message in session: if not messages: await asyncio.sleep(45) print("First sleep {}".format(session.session.locked_until_utc - utc_now())) assert not session.session._lock_expired with pytest.raises(TypeError): message._lock_expired assert message.locked_until_utc is None with pytest.raises(TypeError): await message.renew_lock() assert message.lock_token is not None await message.complete() messages.append(message) elif len(messages) == 1: assert not results await asyncio.sleep(45) print("Second sleep {}".format(session.session.locked_until_utc - utc_now())) assert session.session._lock_expired assert isinstance(session.session.auto_renew_error, AutoLockRenewTimeout) try: await message.complete() raise AssertionError("Didn't raise SessionLockExpired") except SessionLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) messages.append(message) # While we're testing autolockrenew and sessions, let's make sure we don't call the lock-lost callback when a session exits. renewer._renew_period = 1 session = None async with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, max_wait_time=5, receive_mode=ReceiveMode.PeekLock, prefetch_count=10) as receiver: session = receiver.session renewer.register(session, timeout=5, on_lock_renew_failure=lock_lost_callback) await asyncio.sleep(max(0,(session.locked_until_utc - utc_now()).total_seconds()+1)) # If this pattern repeats make sleep_until_expired_async assert not results await renewer.close() assert len(messages) == 2
async def test_async_session_by_conn_str_receive_handler_with_autolockrenew( self, servicebus_namespace_connection_string, servicebus_queue, **kwargs): async with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, logging_enable=False) as sb_client: session_id = str(uuid.uuid4()) async with sb_client.get_queue_sender( servicebus_queue.name) as sender: for i in range(10): message = Message("{}".format(i), session_id=session_id) await sender.send(message) renewer = AutoLockRenew() messages = [] async with sb_client.get_queue_session_receiver( servicebus_queue.name, session_id=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=20) as session: renewer.register(session.session, timeout=60) print("Registered lock renew thread", session.session.locked_until_utc, utc_now()) with pytest.raises(SessionLockExpired): async for message in session: if not messages: await asyncio.sleep(45) print("First sleep {}".format( session.session.locked_until_utc - utc_now())) assert not session.session.expired with pytest.raises(TypeError): message.expired assert message.locked_until_utc is None with pytest.raises(TypeError): await message.renew_lock() assert message.lock_token is not None await message.complete() messages.append(message) elif len(messages) == 1: await asyncio.sleep(45) print("Second sleep {}".format( session.session.locked_until_utc - utc_now())) assert session.session.expired assert isinstance(session.session.auto_renew_error, AutoLockRenewTimeout) try: await message.complete() raise AssertionError( "Didn't raise SessionLockExpired") except SessionLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) messages.append(message) await renewer.shutdown() assert len(messages) == 2
async def test_async_session_by_conn_str_receive_handler_with_autolockrenew( live_servicebus_config, session_queue): session_id = str(uuid.uuid4()) queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=session_queue, debug=True) async with queue_client.get_sender(session=session_id) as sender: for i in range(10): message = Message("{}".format(i)) await sender.send(message) renewer = AutoLockRenew() messages = [] async with queue_client.get_receiver(session=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=20) as session: renewer.register(session, timeout=60) print("Registered lock renew thread", session.locked_until, datetime.now()) with pytest.raises(SessionLockExpired): async for message in session: if not messages: await asyncio.sleep(45) print("First sleep {}".format(session.locked_until - datetime.now())) assert not session.expired with pytest.raises(TypeError): message.expired assert message.locked_until is None with pytest.raises(TypeError): await message.renew_lock() assert message.lock_token is None await message.complete() messages.append(message) elif len(messages) == 1: await asyncio.sleep(45) print("Second sleep {}".format(session.locked_until - datetime.now())) assert session.expired assert isinstance(session.auto_renew_error, AutoLockRenewTimeout) try: await message.complete() raise AssertionError("Didn't raise SessionLockExpired") except SessionLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) messages.append(message) await renewer.shutdown() assert len(messages) == 2
async def test_async_queue_by_queue_client_conn_str_receive_handler_with_autolockrenew( live_servicebus_config, standard_queue): queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=standard_queue, debug=True) async with queue_client.get_sender() as sender: for i in range(10): message = Message("{}".format(i)) await sender.send(message) renewer = AutoLockRenew() messages = [] async with queue_client.get_receiver(idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as receiver: async for message in receiver: if not messages: messages.append(message) assert not message.expired renewer.register(message, timeout=60) print("Registered lock renew thread", message.locked_until, datetime.now()) await asyncio.sleep(50) print("Finished first sleep", message.locked_until) assert not message.expired await asyncio.sleep(25) print("Finished second sleep", message.locked_until, datetime.now()) assert message.expired try: await message.complete() raise AssertionError("Didn't raise MessageLockExpired") except MessageLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) else: if message.expired: print("Remaining messages", message.locked_until, datetime.now()) assert message.expired with pytest.raises(MessageLockExpired): await message.complete() else: assert message.header.delivery_count >= 1 print("Remaining messages", message.locked_until, datetime.now()) messages.append(message) await message.complete() await renewer.shutdown() assert len(messages) == 11
async def example_session_ops_async(): servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] queue_name = os.environ['SERVICE_BUS_QUEUE_NAME'] session_id = "<your session id>" async with ServiceBusClient.from_connection_string( conn_str=servicebus_connection_str) as servicebus_client: # [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_session_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 session_state = await session.set_session_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 session_state = await session.renew_lock() # [END session_renew_lock_async] # [START auto_lock_renew_session_async] from azure.servicebus.aio import AutoLockRenew lock_renewal = AutoLockRenew() 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(session, timeout=120) async for message in receiver: await process_message(message) await message.complete() # [END auto_lock_renew_session_async] break
async def test_async_session_by_conn_str_receive_handler_with_autolockrenew(live_servicebus_config, session_queue): session_id = str(uuid.uuid4()) queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=session_queue, debug=True) async with queue_client.get_sender(session=session_id) as sender: for i in range(10): message = Message("{}".format(i)) await sender.send(message) renewer = AutoLockRenew() messages = [] async with queue_client.get_receiver(session=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=20) as session: renewer.register(session, timeout=60) print("Registered lock renew thread", session.locked_until, datetime.now()) with pytest.raises(SessionLockExpired): async for message in session: if not messages: await asyncio.sleep(45) print("First sleep {}".format(session.locked_until - datetime.now())) assert not session.expired with pytest.raises(TypeError): message.expired assert message.locked_until is None with pytest.raises(TypeError): await message.renew_lock() assert message.lock_token is None await message.complete() messages.append(message) elif len(messages) == 1: await asyncio.sleep(45) print("Second sleep {}".format(session.locked_until - datetime.now())) assert session.expired assert isinstance(session.auto_renew_error, AutoLockRenewTimeout) try: await message.complete() raise AssertionError("Didn't raise SessionLockExpired") except SessionLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) messages.append(message) await renewer.shutdown() assert len(messages) == 2
async def test_async_session_schedule_multiple_messages( self, servicebus_namespace, servicebus_namespace_key_name, servicebus_namespace_primary_key, servicebus_queue, **kwargs): client = ServiceBusClient( service_namespace=servicebus_namespace.name, shared_access_key_name=servicebus_namespace_key_name, shared_access_key_value=servicebus_namespace_primary_key, debug=False) import uuid session_id = str(uuid.uuid4()) queue_client = client.get_queue(servicebus_queue.name) enqueue_time = (datetime.utcnow() + timedelta(minutes=2)).replace(microsecond=0) messages = [] async with queue_client.get_sender(session=session_id) as sender: content = str(uuid.uuid4()) message_id_a = uuid.uuid4() message_a = Message(content) message_a.properties.message_id = message_id_a message_id_b = uuid.uuid4() message_b = Message(content) message_b.properties.message_id = message_id_b tokens = await sender.schedule(enqueue_time, message_a, message_b) assert len(tokens) == 2 renewer = AutoLockRenew() async with queue_client.get_receiver(session=session_id, prefetch=20) as receiver: renewer.register(receiver, timeout=140) messages.extend(await receiver.fetch_next(timeout=120)) messages.extend(await receiver.fetch_next(timeout=5)) if messages: data = str(messages[0]) assert data == content assert messages[0].properties.message_id in (message_id_a, message_id_b) assert messages[0].scheduled_enqueue_time == enqueue_time assert messages[0].scheduled_enqueue_time == messages[ 0].enqueued_time.replace(microsecond=0) assert len(messages) == 2 else: raise Exception("Failed to receive schdeduled message.") await renewer.shutdown()
async def test_async_queue_by_queue_client_conn_str_receive_handler_with_autolockrenew(live_servicebus_config, standard_queue): queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=standard_queue, debug=True) async with queue_client.get_sender() as sender: for i in range(10): message = Message("{}".format(i)) await sender.send(message) renewer = AutoLockRenew() messages = [] async with queue_client.get_receiver(idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as receiver: async for message in receiver: if not messages: messages.append(message) assert not message.expired renewer.register(message, timeout=60) print("Registered lock renew thread", message.locked_until, datetime.now()) await asyncio.sleep(50) print("Finished first sleep", message.locked_until) assert not message.expired await asyncio.sleep(25) print("Finished second sleep", message.locked_until, datetime.now()) assert message.expired try: await message.complete() raise AssertionError("Didn't raise MessageLockExpired") except MessageLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) else: if message.expired: print("Remaining messages", message.locked_until, datetime.now()) assert message.expired with pytest.raises(MessageLockExpired): await message.complete() else: assert message.header.delivery_count >= 1 print("Remaining messages", message.locked_until, datetime.now()) messages.append(message) await message.complete() await renewer.shutdown() assert len(messages) == 11
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 Message # [START send_async] async with servicebus_sender: message = Message("Hello World") await servicebus_sender.send(message) # [END send_async] # [START create_batch_async] async with servicebus_sender: batch_message = await servicebus_sender.create_batch() batch_message.add(Message("Single message inside batch")) # [END create_batch_async] # [START peek_messages_async] async with servicebus_receiver: messages = await servicebus_receiver.peek() for message in messages: print(message) # [END peek_messages_async] # [START receive_async] async with servicebus_receiver: messages = await servicebus_receiver.receive(max_wait_time=5) for message in messages: print(message) await message.complete() # [END receive_async] # [START auto_lock_renew_message_async] from azure.servicebus.aio import AutoLockRenew lock_renewal = AutoLockRenew() 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 test_async_snippet_sessions(live_servicebus_config, session_queue): queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=session_queue) queue_client.get_properties() # [START open_close_session_sender_context] from azure.servicebus.aio import Message async with queue_client.get_sender(session="MySessionID") as sender: await sender.send(Message("First")) await sender.send(Message("Second")) # [END open_close_session_sender_context] # [START queue_session_sender_messages] async with queue_client.get_sender(session="MySessionID") as sender: sender.queue_message(Message("First")) sender.queue_message(Message("Second")) await sender.send_pending_messages() # [END queue_session_sender_messages] # [START schedule_session_messages] async with queue_client.get_sender(session="MySessionID") as sender: enqueue_time = datetime.now() + timedelta(minutes=10) await sender.schedule(enqueue_time, Message("First"), Message("Second")) # [END schedule_session_messages] # [START open_close_receiver_session_context] async with queue_client.get_receiver(session="MySessionID") as session: async for message in session: await process_message(message) # [END open_close_receiver_session_context] break # [START open_close_receiver_session_nextavailable] from azure.servicebus import NEXT_AVAILABLE, NoActiveSession try: async with queue_client.get_receiver(session=NEXT_AVAILABLE, idle_timeout=5) as receiver: async for message in receiver: await process_message(message) except NoActiveSession: pass # [END open_close_receiver_session_nextavailable] # [START set_session_state] async with queue_client.get_receiver(session="MySessionID", idle_timeout=5) as session: current_state = await session.get_session_state() if not current_state: await session.set_session_state("OPENED") # [END set_session_state] try: # [START receiver_peek_session_messages] async with queue_client.get_receiver(session=NEXT_AVAILABLE, idle_timeout=5) as receiver: pending_messages = await receiver.peek(count=5) # [END receiver_peek_session_messages] except NoActiveSession: pass await queue_client.send([ Message("a"), Message("b"), Message("c"), Message("d"), Message("e"), Message("f") ], session="MySessionID") try: # [START receiver_defer_session_messages] async with queue_client.get_receiver(session="MySessionID", idle_timeout=5) as receiver: sequence_numbers = [] async for message in receiver: sequence_numbers.append(message.sequence_number) await message.defer() break message = await receiver.receive_deferred_messages(sequence_numbers ) # [END receiver_defer_session_messages] except ServiceBusError: pass # [START receiver_renew_session_lock] async with queue_client.get_receiver(session="MySessionID", idle_timeout=5) as session: async for message in session: await process_message(message) await session.renew_lock() # [END receiver_renew_session_lock] break # [START auto_lock_renew_async_session] from azure.servicebus.aio import AutoLockRenew lock_renewal = AutoLockRenew() async with queue_client.get_receiver(session="MySessionID", idle_timeout=3) as session: lock_renewal.register(session) async for message in session: await process_message(message) await message.complete() # [END auto_lock_renew_async_session] break
async def test_async_snippet_queues(live_servicebus_config, standard_queue): # [START create_async_servicebus_client] import os from azure.servicebus.aio import ServiceBusClient, Message namespace = os.environ['SERVICE_BUS_HOSTNAME'] shared_access_policy = os.environ['SERVICE_BUS_SAS_POLICY'] shared_access_key = os.environ['SERVICE_BUS_SAS_KEY'] client = ServiceBusClient( service_namespace=namespace, shared_access_key_name=shared_access_policy, shared_access_key_value=shared_access_key) # [END create_async_servicebus_client] # [START create_async_servicebus_client_connstr] connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] client = ServiceBusClient.from_connection_string(connection_str) # [END create_async_servicebus_client_connstr] # [START get_async_queue_client] from azure.servicebus import ServiceBusResourceNotFound try: queue_client = client.get_queue("MyQueue") except ServiceBusResourceNotFound: pass # [END get_async_queue_client] try: # [START create_queue_client] import os from azure.servicebus.aio import QueueClient connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] queue_client = QueueClient.from_connection_string(connection_str, name="MyQueue") queue_properties = queue_client.get_properties() # [END create_queue_client] except ServiceBusResourceNotFound: pass queue_client = client.get_queue(standard_queue) # [START client_peek_messages] peeked_messages = await queue_client.peek(count=5) # [END client_peek_messages] await queue_client.send(Message("a")) try: # [START client_defer_messages] sequence_numbers = [] async with queue_client.get_receiver() as receiver: async for message in receiver: sequence_numbers.append(message.sequence_number) await message.defer() break deferred = await queue_client.receive_deferred_messages(sequence_numbers) # [END client_defer_messages] except ValueError: pass await queue_client.send(Message("a")) try: sequence_numbers = [] async with queue_client.get_receiver(idle_timeout=2) as receiver: async for message in receiver: sequence_numbers.append(message.sequence_number) await message.defer() break # [START client_settle_deferred_messages] deferred = await queue_client.receive_deferred_messages(sequence_numbers) await queue_client.settle_deferred_messages('completed', deferred) # [END client_settle_deferred_messages] except ValueError: pass # [START open_close_sender_directly] from azure.servicebus.aio import Message sender = queue_client.get_sender() try: await sender.open() await sender.send(Message("foobar")) finally: await sender.close() # [END open_close_sender_directly] # [START queue_client_send] from azure.servicebus.aio import Message message = Message("Hello World") await queue_client.send(message) # [END queue_client_send] # [START queue_client_send_multiple] from azure.servicebus.aio import Message messages = [Message("First"), Message("Second")] await queue_client.send(messages, message_timeout=30) # [END queue_client_send_multiple] # [START open_close_receiver_directly] receiver = queue_client.get_receiver() async for message in receiver: print(message) break await receiver.close() # [END open_close_receiver_directly] await queue_client.send(Message("a")) # [START open_close_receiver_context] async with queue_client.get_receiver() as receiver: async for message in receiver: await process_message(message) # [END open_close_receiver_context] break # [START open_close_sender_context] async with queue_client.get_sender() as sender: await sender.send(Message("First")) await sender.send(Message("Second")) # [END open_close_sender_context] # [START queue_sender_messages] async with queue_client.get_sender() as sender: sender.queue_message(Message("First")) sender.queue_message(Message("Second")) await sender.send_pending_messages() # [END queue_sender_messages] # [START schedule_messages] async with queue_client.get_sender() as sender: enqueue_time = datetime.now() + timedelta(minutes=10) await sender.schedule(enqueue_time, Message("First"), Message("Second")) # [END schedule_messages] # [START cancel_schedule_messages] async with queue_client.get_sender() as sender: enqueue_time = datetime.now() + timedelta(minutes=10) sequence_numbers = await sender.schedule(enqueue_time, Message("First"), Message("Second")) await sender.cancel_scheduled_messages(*sequence_numbers) # [END cancel_schedule_messages] # [START receiver_peek_messages] async with queue_client.get_receiver() as receiver: pending_messages = await receiver.peek(count=5) # [END receiver_peek_messages] try: await queue_client.send(Message("a")) # [START receiver_defer_messages] async with queue_client.get_receiver() as receiver: async for message in receiver: sequence_no = message.sequence_number await message.defer() break message = await receiver.receive_deferred_messages([sequence_no]) # [END receiver_defer_messages] except ServiceBusError: pass await queue_client.send(Message("a")) # [START receiver_deadletter_messages] async with queue_client.get_receiver(idle_timeout=5) as receiver: async for message in receiver: await message.dead_letter() async with queue_client.get_deadletter_receiver() as receiver: async for message in receiver: await message.complete() # [END receiver_deadletter_messages] break # [START receiver_fetch_batch] async with queue_client.get_receiver(idle_timeout=5, prefetch=100) as receiver: messages = await receiver.fetch_next(timeout=5) await asyncio.gather(*[m.complete() for m in messages]) # [END receiver_fetch_batch] # [START auto_lock_renew_async_message] from azure.servicebus.aio import AutoLockRenew lock_renewal = AutoLockRenew() async with queue_client.get_receiver(idle_timeout=3) as queue_receiver: async for message in queue_receiver: lock_renewal.register(message, timeout=60) await process_message(message) await message.complete()
async def test_async_snippet_sessions(live_servicebus_config, session_queue): queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=session_queue) queue_client.get_properties() # [START open_close_session_sender_context] from azure.servicebus.aio import Message async with queue_client.get_sender(session="MySessionID") as sender: await sender.send(Message("First")) await sender.send(Message("Second")) # [END open_close_session_sender_context] # [START queue_session_sender_messages] async with queue_client.get_sender(session="MySessionID") as sender: sender.queue_message(Message("First")) sender.queue_message(Message("Second")) await sender.send_pending_messages() # [END queue_session_sender_messages] # [START schedule_session_messages] async with queue_client.get_sender(session="MySessionID") as sender: enqueue_time = datetime.now() + timedelta(minutes=10) await sender.schedule(enqueue_time, Message("First"), Message("Second")) # [END schedule_session_messages] # [START open_close_receiver_session_context] async with queue_client.get_receiver(session="MySessionID") as session: async for message in session: await process_message(message) # [END open_close_receiver_session_context] break # [START open_close_receiver_session_nextavailable] from azure.servicebus import NEXT_AVAILABLE, NoActiveSession try: async with queue_client.get_receiver(session=NEXT_AVAILABLE, idle_timeout=5) as receiver: async for message in receiver: await process_message(message) except NoActiveSession: pass # [END open_close_receiver_session_nextavailable] # [START set_session_state] async with queue_client.get_receiver(session="MySessionID", idle_timeout=5) as session: current_state = await session.get_session_state() if not current_state: await session.set_session_state("OPENED") # [END set_session_state] try: # [START receiver_peek_session_messages] async with queue_client.get_receiver(session=NEXT_AVAILABLE, idle_timeout=5) as receiver: pending_messages = await receiver.peek(count=5) # [END receiver_peek_session_messages] except NoActiveSession: pass await queue_client.send([Message("a"), Message("b"), Message("c"), Message("d"), Message("e"), Message("f")], session="MySessionID") try: # [START receiver_defer_session_messages] async with queue_client.get_receiver(session="MySessionID", idle_timeout=5) as receiver: sequence_numbers = [] async for message in receiver: sequence_numbers.append(message.sequence_number) await message.defer() break message = await receiver.receive_deferred_messages(sequence_numbers) # [END receiver_defer_session_messages] except ServiceBusError: pass # [START receiver_renew_session_lock] async with queue_client.get_receiver(session="MySessionID", idle_timeout=5) as session: async for message in session: await process_message(message) await session.renew_lock() # [END receiver_renew_session_lock] break # [START auto_lock_renew_async_session] from azure.servicebus.aio import AutoLockRenew lock_renewal = AutoLockRenew() async with queue_client.get_receiver(session="MySessionID", idle_timeout=3) as session: lock_renewal.register(session) async for message in session: await process_message(message) await message.complete() # [END auto_lock_renew_async_session] break
async def test_async_snippet_queues(live_servicebus_config, standard_queue): # [START create_async_servicebus_client] import os from azure.servicebus.aio import ServiceBusClient, Message namespace = os.environ['SERVICE_BUS_HOSTNAME'] shared_access_policy = os.environ['SERVICE_BUS_SAS_POLICY'] shared_access_key = os.environ['SERVICE_BUS_SAS_KEY'] client = ServiceBusClient(service_namespace=namespace, shared_access_key_name=shared_access_policy, shared_access_key_value=shared_access_key) # [END create_async_servicebus_client] # [START create_async_servicebus_client_connstr] connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] client = ServiceBusClient.from_connection_string(connection_str) # [END create_async_servicebus_client_connstr] # [START get_async_queue_client] from azure.servicebus import ServiceBusResourceNotFound try: queue_client = client.get_queue("MyQueue") except ServiceBusResourceNotFound: pass # [END get_async_queue_client] try: # [START create_queue_client] import os from azure.servicebus.aio import QueueClient connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] queue_client = QueueClient.from_connection_string(connection_str, name="MyQueue") queue_properties = queue_client.get_properties() # [END create_queue_client] except ServiceBusResourceNotFound: pass queue_client = client.get_queue(standard_queue) # [START client_peek_messages] peeked_messages = await queue_client.peek(count=5) # [END client_peek_messages] await queue_client.send(Message("a")) try: # [START client_defer_messages] sequence_numbers = [] async with queue_client.get_receiver() as receiver: async for message in receiver: sequence_numbers.append(message.sequence_number) await message.defer() break deferred = await queue_client.receive_deferred_messages( sequence_numbers) # [END client_defer_messages] except ValueError: pass await queue_client.send(Message("a")) try: sequence_numbers = [] async with queue_client.get_receiver(idle_timeout=2) as receiver: async for message in receiver: sequence_numbers.append(message.sequence_number) await message.defer() break # [START client_settle_deferred_messages] deferred = await queue_client.receive_deferred_messages( sequence_numbers) await queue_client.settle_deferred_messages('completed', deferred) # [END client_settle_deferred_messages] except ValueError: pass # [START open_close_sender_directly] from azure.servicebus.aio import Message sender = queue_client.get_sender() try: await sender.open() await sender.send(Message("foobar")) finally: await sender.close() # [END open_close_sender_directly] # [START queue_client_send] from azure.servicebus.aio import Message message = Message("Hello World") await queue_client.send(message) # [END queue_client_send] # [START queue_client_send_multiple] from azure.servicebus.aio import Message messages = [Message("First"), Message("Second")] await queue_client.send(messages, message_timeout=30) # [END queue_client_send_multiple] # [START open_close_receiver_directly] receiver = queue_client.get_receiver() async for message in receiver: print(message) break await receiver.close() # [END open_close_receiver_directly] await queue_client.send(Message("a")) # [START open_close_receiver_context] async with queue_client.get_receiver() as receiver: async for message in receiver: await process_message(message) # [END open_close_receiver_context] break # [START open_close_sender_context] async with queue_client.get_sender() as sender: await sender.send(Message("First")) await sender.send(Message("Second")) # [END open_close_sender_context] # [START queue_sender_messages] async with queue_client.get_sender() as sender: sender.queue_message(Message("First")) sender.queue_message(Message("Second")) await sender.send_pending_messages() # [END queue_sender_messages] # [START schedule_messages] async with queue_client.get_sender() as sender: enqueue_time = datetime.now() + timedelta(minutes=10) await sender.schedule(enqueue_time, Message("First"), Message("Second")) # [END schedule_messages] # [START cancel_schedule_messages] async with queue_client.get_sender() as sender: enqueue_time = datetime.now() + timedelta(minutes=10) sequence_numbers = await sender.schedule(enqueue_time, Message("First"), Message("Second")) await sender.cancel_scheduled_messages(*sequence_numbers) # [END cancel_schedule_messages] # [START receiver_peek_messages] async with queue_client.get_receiver() as receiver: pending_messages = await receiver.peek(count=5) # [END receiver_peek_messages] try: await queue_client.send(Message("a")) # [START receiver_defer_messages] async with queue_client.get_receiver() as receiver: async for message in receiver: sequence_no = message.sequence_number await message.defer() break message = await receiver.receive_deferred_messages([sequence_no]) # [END receiver_defer_messages] except ServiceBusError: pass await queue_client.send(Message("a")) # [START receiver_deadletter_messages] async with queue_client.get_receiver(idle_timeout=5) as receiver: async for message in receiver: await message.dead_letter() async with queue_client.get_deadletter_receiver() as receiver: async for message in receiver: await message.complete() # [END receiver_deadletter_messages] break # [START receiver_fetch_batch] async with queue_client.get_receiver(idle_timeout=5, prefetch=100) as receiver: messages = await receiver.fetch_next(timeout=5) await asyncio.gather(*[m.complete() for m in messages]) # [END receiver_fetch_batch] # [START auto_lock_renew_async_message] from azure.servicebus.aio import AutoLockRenew lock_renewal = AutoLockRenew() async with queue_client.get_receiver(idle_timeout=3) as queue_receiver: async for message in queue_receiver: lock_renewal.register(message, timeout=60) await process_message(message) await message.complete()