def message_processing(sb_client, queue_name, messages): while True: try: with sb_client.get_queue_session_receiver( queue_name, idle_timeout=1) as receiver: renewer = AutoLockRenew() renewer.register(receiver.session) receiver.session.set_session_state("OPEN") for message in receiver: messages.append(message) 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)) message.complete() if str(message) == 'shutdown': receiver.session.set_session_state("CLOSED") renewer.shutdown() except NoActiveSession: print( "There are no non-empty sessions remaining; exiting. This may present as a UserError in the azure portal." ) return
def renew_lock_on_message_received_from_non_sessionful_entity(): servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR) with servicebus_client: with servicebus_client.get_queue_sender(queue_name=QUEUE_NAME) as sender: msgs_to_send = [Message("message: {}".format(i)) for i in range(10)] 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() with servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, prefetch=10) as receiver: received_msgs = 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.') time.sleep(100) # message handling for long period (E.g. application logic) for msg in received_msgs: msg.complete() # Settling the message deregisters it from the AutoLockRenewer print('Complete messages.') renewer.close()
def renew_lock_on_session_of_the_sessionful_entity(): servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR) with servicebus_client: 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)] sender.send_messages(msgs_to_send) print('Send messages to sessionful queue.') renewer = AutoLockRenew() 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 = receiver.receive_messages(max_batch_size=10, max_wait_time=5) time.sleep(100) # message handling for long period (E.g. application logic) for msg in received_msgs: msg.complete() print('Complete messages.') renewer.close()
def test_session_by_conn_str_receive_handler_with_autolockrenew( self, servicebus_namespace_connection_string, servicebus_queue, **kwargs): session_id = str(uuid.uuid4()) queue_client = QueueClient.from_connection_string( servicebus_namespace_connection_string, name=servicebus_queue.name, debug=False) with queue_client.get_sender(session=session_id) as sender: for i in range(10): message = Message("{}".format(i)) sender.send(message) renewer = AutoLockRenew() messages = [] with queue_client.get_receiver(session=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as session: renewer.register(session, timeout=60) print("Registered lock renew thread", session.locked_until, datetime.now()) with pytest.raises(SessionLockExpired): for message in session: if not messages: print("Starting first sleep") time.sleep(40) 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): message.renew_lock() assert message.lock_token is None message.complete() messages.append(message) elif len(messages) == 1: print("Starting second sleep") time.sleep(40) print("Second sleep {}".format(session.locked_until - datetime.now())) assert session.expired assert isinstance(session.auto_renew_error, AutoLockRenewTimeout) try: message.complete() raise AssertionError( "Didn't raise SessionLockExpired") except SessionLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) messages.append(message) renewer.shutdown() assert len(messages) == 2
def receive_process_and_complete_message(client, queue_name): queue_client = client.get_queue(queue_name) lock_renewal = AutoLockRenew(max_workers=4) lock_renewal.renew_period = 120 with queue_client.get_receiver() as queue_receiver: for message in queue_receiver: print("Received message: ", message) lock_renewal.register(message, timeout=10800) process_message(message) print("Completing message") message.complete() break
def test_pqueue_by_queue_client_conn_str_receive_handler_with_autolockrenew( live_servicebus_config, partitioned_queue): queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=partitioned_queue, debug=False) with queue_client.get_sender() as sender: for i in range(10): message = Message("{}".format(i)) sender.send(message) renewer = AutoLockRenew() messages = [] with queue_client.get_receiver(idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as receiver: 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()) time.sleep(50) print("Finished first sleep", message.locked_until) assert not message.expired time.sleep(25) print("Finished second sleep", message.locked_until, datetime.now()) assert message.expired try: 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): message.complete() else: assert message.header.delivery_count >= 1 print("Remaining messages", message.locked_until, datetime.now()) messages.append(message) message.complete() renewer.shutdown() assert len(messages) == 11
def example_session_ops_sync(): servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] queue_name = os.environ['SERVICE_BUS_QUEUE_NAME'] session_id = "<your session id>" with ServiceBusClient.from_connection_string( conn_str=servicebus_connection_str) as servicebus_client: # [START get_session_sync] with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session # [END get_session_sync] # [START get_session_state_sync] with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session session_state = session.get_session_state() # [END get_session_state_sync] # [START set_session_state_sync] with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session session_state = session.set_session_state("START") # [END set_session_state_sync] # [START session_renew_lock_sync] with servicebus_client.get_queue_receiver( queue_name=queue_name, session_id=session_id) as receiver: session = receiver.session session_state = session.renew_lock() # [END session_renew_lock_sync] # [START auto_lock_renew_session_sync] from azure.servicebus import AutoLockRenew lock_renewal = AutoLockRenew(max_workers=4) 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) for message in receiver: process_message(message) message.complete() # [END auto_lock_renew_session_sync] break
def test_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) with queue_client.get_sender(session=session_id) as sender: for i in range(10): message = Message("{}".format(i)) sender.send(message) renewer = AutoLockRenew() messages = [] with queue_client.get_receiver(session=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as session: renewer.register(session, timeout=60) print("Registered lock renew thread", session.locked_until, datetime.now()) with pytest.raises(SessionLockExpired): for message in session: if not messages: print("Starting first sleep") time.sleep(40) 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): message.renew_lock() assert message.lock_token is None message.complete() messages.append(message) elif len(messages) == 1: print("Starting second sleep") time.sleep(40) print("Second sleep {}".format(session.locked_until - datetime.now())) assert session.expired assert isinstance(session.auto_renew_error, AutoLockRenewTimeout) try: message.complete() raise AssertionError("Didn't raise SessionLockExpired") except SessionLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) messages.append(message) renewer.shutdown() assert len(messages) == 2
def test_session_by_conn_str_receive_handler_with_autolockrenew(self, servicebus_namespace_connection_string, servicebus_queue, **kwargs): session_id = str(uuid.uuid4()) with ServiceBusClient.from_connection_string( servicebus_namespace_connection_string, logging_enable=False) as sb_client: with sb_client.get_queue_sender(servicebus_queue.name) as sender: for i in range(10): message = Message("{}".format(i), session_id=session_id) sender.send(message) renewer = AutoLockRenew() messages = [] with sb_client.get_queue_session_receiver(servicebus_queue.name, session_id=session_id, idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as receiver: renewer.register(receiver.session, timeout=60) print("Registered lock renew thread", receiver.session._locked_until_utc, utc_now()) with pytest.raises(SessionLockExpired): for message in receiver: if not messages: print("Starting first sleep") time.sleep(40) print("First sleep {}".format(receiver.session._locked_until_utc - utc_now())) assert not receiver.session.expired with pytest.raises(TypeError): message.expired assert message.locked_until_utc is None with pytest.raises(TypeError): message.renew_lock() assert message.lock_token is not None message.complete() messages.append(message) elif len(messages) == 1: print("Starting second sleep") time.sleep(40) # ensure renewer expires print("Second sleep {}".format(receiver.session._locked_until_utc - utc_now())) sleep_until_expired(receiver.session) # and then ensure it didn't slip a renew under the wire. assert receiver.session.expired assert isinstance(receiver.session.auto_renew_error, AutoLockRenewTimeout) try: message.complete() raise AssertionError("Didn't raise SessionLockExpired") except SessionLockExpired as e: assert isinstance(e.inner_exception, AutoLockRenewTimeout) messages.append(message) renewer.shutdown() assert len(messages) == 2
def test_pqueue_by_queue_client_conn_str_receive_handler_with_autolockrenew(live_servicebus_config, partitioned_queue): queue_client = QueueClient.from_connection_string( live_servicebus_config['conn_str'], name=partitioned_queue, debug=True) with queue_client.get_sender() as sender: for i in range(10): message = Message("{}".format(i)) sender.send(message) renewer = AutoLockRenew() messages = [] with queue_client.get_receiver(idle_timeout=5, mode=ReceiveSettleMode.PeekLock, prefetch=10) as receiver: 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()) time.sleep(50) print("Finished first sleep", message.locked_until) assert not message.expired time.sleep(25) print("Finished second sleep", message.locked_until, datetime.now()) assert message.expired try: 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): message.complete() else: assert message.header.delivery_count >= 1 print("Remaining messages", message.locked_until, datetime.now()) messages.append(message) message.complete() renewer.shutdown() assert len(messages) == 11
def example_send_and_receive_sync(): servicebus_sender = example_create_servicebus_sender_sync() servicebus_receiver = example_create_servicebus_receiver_sync() from azure.servicebus import Message # [START send_sync] with servicebus_sender: message = Message("Hello World") servicebus_sender.send(message) # [END send_sync] # [START create_batch_sync] with servicebus_sender: batch_message = servicebus_sender.create_batch() batch_message.add(Message("Single message inside batch")) # [END create_batch_sync] # [START send_complex_message] message = Message("Hello World!!") message.session_id = "MySessionID" message.partition_key = "UsingSpecificPartition" message.user_properties = {'data': 'custom_data'} message.time_to_live = datetime.timedelta(seconds=30) # [END send_complex_message] # [START peek_messages_sync] with servicebus_receiver: messages = servicebus_receiver.peek() for message in messages: print(message) # [END peek_messages_sync] # [START auto_lock_renew_message_sync] from azure.servicebus import AutoLockRenew lock_renewal = AutoLockRenew(max_workers=4) with servicebus_receiver: for message in servicebus_receiver: # Auto renew message for 1 minute. lock_renewal.register(message, timeout=60) process_message(message) message.complete() # [END auto_lock_renew_message_sync] break # [START receive_sync] with servicebus_receiver: messages = servicebus_receiver.receive(max_wait_time=5) for message in messages: print(message) message.complete() # [END receive_sync] # [START receive_complex_message] messages = servicebus_receiver.receive(max_wait_time=5) for message in messages: print("Receiving: {}".format(message)) print("Time to live: {}".format(message.time_to_live)) print("Sequence number: {}".format(message.sequence_number)) print("Enqueue Sequence numger: {}".format( message.enqueue_sequence_number)) print("Partition ID: {}".format(message.partition_id)) print("Partition Key: {}".format(message.partition_key)) print("User Properties: {}".format(message.user_properties)) print("Annotations: {}".format(message.annotations)) print("Delivery count: {}".format(message.header.delivery_count)) print("Message ID: {}".format(message.properties.message_id)) print("Locked until: {}".format(message.locked_until_utc)) print("Lock Token: {}".format(message.lock_token)) print("Enqueued time: {}".format(message.enqueued_time_utc))
def test_example_send_receive_service_bus(live_servicebus_config, standard_queue, session_queue): import os import datetime from azure.servicebus import ServiceBusClient, ServiceBusResourceNotFound from azure.servicebus import Message client = create_servicebus_client() try: # [START create_queue_client_directly] import os from azure.servicebus 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_directly] except ServiceBusResourceNotFound: pass try: # [START create_topic_client_directly] import os from azure.servicebus import TopicClient connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] topic_client = TopicClient.from_connection_string(connection_str, name="MyTopic") properties = topic_client.get_properties() # [END create_topic_client_directly] except ServiceBusResourceNotFound: pass try: # [START create_sub_client_directly] import os from azure.servicebus import SubscriptionClient connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] subscription_client = SubscriptionClient.from_connection_string( connection_str, name="MySub", topic="MyTopic") properties = subscription_client.get_properties() # [END create_sub_client_directly] except ServiceBusResourceNotFound: pass queue_client = client.get_queue(standard_queue) session_client = client.get_queue(session_queue) # [START get_sender] with queue_client.get_sender() as queue_sender: queue_sender.send(Message("First")) queue_sender.send(Message("Second")) # [END get_sender] # [START send_message_service_bus_multiple] from azure.servicebus import Message message1 = Message("Hello World!") message2 = Message("How are you?") queue_client.send([message1, message2]) # [END send_message_service_bus_multiple] # [START send_complex_message] message = Message("Hello World!") message.session_id = "MySessionID" message.partition_key = "UsingSpecificPartition" message.user_properties = {'data': 'custom_data'} message.time_to_live = datetime.timedelta(seconds=30) queue_client.send(message) # [END send_complex_message] # [START send_batch_message] from azure.servicebus import BatchMessage def batched_data(): for i in range(100): yield "Batched Message no. {}".format(i) message = BatchMessage(batched_data()) results = queue_client.send(message) # [END send_batch_message] # [START send_message_service_bus] from azure.servicebus import Message message = Message("Hello World!") queue_client.send(message) # [END send_message_service_bus] # [START get_receiver] with queue_client.get_receiver() as queue_receiver: messages = queue_receiver.fetch_next(timeout=3) # [END get_receiver] # [START peek_messages_service_bus] # Specify the number of messages to peek at. pending_messages = queue_client.peek(count=5) # [END peek_messages_service_bus] # [START auto_lock_renew_message] from azure.servicebus import AutoLockRenew lock_renewal = AutoLockRenew(max_workers=4) with queue_client.get_receiver(idle_timeout=3) as queue_receiver: for message in queue_receiver: # Auto renew message for 1 minute. lock_renewal.register(message, timeout=60) process_message(message) message.complete() # [END auto_lock_renew_message] # [START auto_lock_renew_session] from azure.servicebus import AutoLockRenew lock_renewal = AutoLockRenew(max_workers=4) with session_client.get_receiver(session="MySessionID", idle_timeout=3) as session: # Auto renew session lock for 2 minutes lock_renewal.register(session, timeout=120) for message in session: process_message(message) message.complete() # [END auto_lock_renew_session] # [START list_sessions_service_bus] session_ids = session_client.list_sessions() # List sessions updated after specific time import datetime yesterday = datetime.datetime.today() - datetime.timedelta(days=1) session_ids = session_client.list_sessions(updated_since=yesterday) # [END list_sessions_service_bus] try: # [START receive_deferred_messages_service_bus] seq_numbers = [] with queue_client.get_receiver(idle_timeout=3) as queue_receiver: for message in queue_receiver: seq_numbers.append(message.sequence_number) message.defer() # Receive deferred messages - provide sequence numbers of # messages which were deferred. deferred = queue_client.receive_deferred_messages(sequence_numbers=seq_numbers) # [END receive_deferred_messages_service_bus] except ValueError: pass deferred = [] try: # [START settle_deferred_messages_service_bus] queue_client.settle_deferred_messages('completed', deferred) # [END settle_deferred_messages_service_bus] except ValueError: pass # [START get_dead_letter_receiver] # Get dead lettered messages with queue_client.get_deadletter_receiver(idle_timeout=1) as dead_letter_receiver: # Receive dead lettered message continuously for message in dead_letter_receiver: print(message) message.complete()