def on_receive(self, state, received_message, receiver): '''Called on every successful receive''' renewer = AutoLockRenewer() renewer.register(receiver, received_message, max_lock_renewal_duration=300) time.sleep(300)
def message_processing(sb_client, queue_name, messages): while True: try: with sb_client.get_queue_receiver( queue_name, max_wait_time=1, session_id=NEXT_AVAILABLE_SESSION) as receiver: renewer = AutoLockRenewer() renewer.register(receiver.session) receiver.session.set_state("OPEN") for message in receiver: messages.append(message) 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)) message.complete() if str(message) == 'shutdown': receiver.session.set_state("CLOSED") renewer.close() 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_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 = AutoLockRenewer() with servicebus_client.get_queue_session_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 = receiver.receive_messages(max_message_count=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 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 AutoLockRenewer() as renewer" to automate shutdown. renewer = AutoLockRenewer() with servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, prefetch_count=10) as receiver: received_msgs = 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.') 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 example_session_ops_sync(): servicebus_connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] queue_name = os.environ['SERVICE_BUS_SESSION_QUEUE_NAME'] session_id = "<your session id>" with ServiceBusClient.from_connection_string( conn_str=servicebus_connection_str) as servicebus_client: with servicebus_client.get_queue_sender( queue_name=queue_name) as sender: sender.send_messages( ServiceBusMessage('msg', session_id=session_id)) # [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_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.set_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.renew_lock() # [END session_renew_lock_sync] # [START auto_lock_renew_session_sync] from azure.servicebus import AutoLockRenewer lock_renewal = AutoLockRenewer(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(receiver, session, max_lock_renewal_duration=120) for message in receiver: process_message(message) receiver.complete_message(message) # [END auto_lock_renew_session_sync] break
def on_receive(self, state, received_message, receiver): '''Called on every successful receive''' renewer = AutoLockRenewer() def on_fail(renewable, error): print("FAILED AUTOLOCKRENEW: " + str(error)) renewer.register(receiver, receiver.session, max_lock_renewal_duration=600, on_lock_renew_failure=on_fail)
def receive_process_and_complete_message(client, queue_name): queue_client = client.get_queue(queue_name) lock_renewal = AutoLockRenewer(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 renew_lock_with_lock_renewal_failure_callback(): 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: sender.send_messages(ServiceBusMessage("message")) with AutoLockRenewer() as renewer: # For this sample we're going to set the renewal recurrence of the autolockrenewer to greater than the # service side message lock duration, to demonstrate failure. Normally, this should not be adjusted. renewer._sleep_time = 40 with servicebus_client.get_queue_receiver( queue_name=QUEUE_NAME, prefetch_count=10) as receiver: def on_lock_renew_failure_callback(renewable, error): # If auto-lock-renewal fails, this function will be called. # If failure is due to an error, the second argument will be populated, otherwise # it will default to `None`. # This callback can be an ideal location to log the failure, or take action to safely # handle any processing on the message or session that was in progress. print("Intentionally failed to renew lock on {} due to {}". format(renewable, error)) received_msgs = receiver.receive_messages(max_message_count=1, max_wait_time=5) for msg in received_msgs: # automatically renew the lock on each message for 120 seconds renewer.register( receiver, msg, max_lock_renewal_duration=90, on_lock_renew_failure=on_lock_renew_failure_callback) print('Register messages into AutoLockRenewer done.') # Cause the messages and autorenewal to time out. # Other reasons for renew failure could include a network or service outage. time.sleep(80) try: for msg in received_msgs: receiver.complete_message(msg) except ServiceBusError as e: print( 'Messages cannot be settled if they have timed out. (This is expected)' ) print('Lock renew failure demonstration complete.')
def example_send_and_receive_sync(): servicebus_sender = example_create_servicebus_sender_sync() servicebus_receiver = example_create_servicebus_receiver_sync() from azure.servicebus import ServiceBusMessage # [START send_sync] with servicebus_sender: message = ServiceBusMessage("Hello World") servicebus_sender.send_messages(message) # [END send_sync] servicebus_sender.send_messages([ServiceBusMessage("Hello World")] * 5) # [START create_batch_sync] with servicebus_sender: batch_message = servicebus_sender.create_message_batch() batch_message.add_message( ServiceBusMessage("Single message inside batch")) # [END create_batch_sync] # [START send_complex_message] message = ServiceBusMessage("Hello World!!", session_id="MySessionID", partition_key="UsingSpecificPartition", user_properties={'data': 'custom_data'}, time_to_live=datetime.timedelta(seconds=30), label='MyLabel') # [END send_complex_message] # [START peek_messages_sync] with servicebus_receiver: messages = servicebus_receiver.peek_messages() for message in messages: print(str(message)) # [END peek_messages_sync] # [START auto_lock_renew_message_sync] from azure.servicebus import AutoLockRenewer lock_renewal = AutoLockRenewer(max_workers=4) with servicebus_receiver: for message in servicebus_receiver: # Auto renew message for 1 minute. lock_renewal.register(servicebus_receiver, message, max_lock_renewal_duration=60) process_message(message) servicebus_receiver.complete_message(message) # [END auto_lock_renew_message_sync] break # [START receive_sync] with servicebus_receiver: messages = servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: print(str(message)) servicebus_receiver.complete_message(message) # [END receive_sync] # [START receive_complex_message] messages = servicebus_receiver.receive_messages(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("Enqueued Sequence numger: {}".format( message.enqueued_sequence_number)) print("Partition Key: {}".format(message.partition_key)) print("Application Properties: {}".format( message.application_properties)) print("Delivery count: {}".format(message.delivery_count)) print("Message ID: {}".format(message.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)) # [END receive_complex_message] # [START abandon_message_sync] messages = servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: servicebus_receiver.abandon_message(message) # [END abandon_message_sync] # [START complete_message_sync] messages = servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: servicebus_receiver.complete_message(message) # [END complete_message_sync] # [START defer_message_sync] messages = servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: servicebus_receiver.defer_message(message) # [END defer_message_sync] # [START dead_letter_message_sync] messages = servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: servicebus_receiver.dead_letter_message(message) # [END dead_letter_message_sync] # [START renew_message_lock_sync] messages = servicebus_receiver.receive_messages(max_wait_time=5) for message in messages: servicebus_receiver.renew_message_lock(message) # [END renew_message_lock_sync] # [START receive_forever] with servicebus_receiver: for message in servicebus_receiver.get_streaming_message_iter(): print(str(message)) servicebus_receiver.complete_message(message) # [END receive_forever] break