Пример #1
0
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()
Пример #2
0
def message_processing(sb_client, queue_name, messages):
    while True:
        try:
            with sb_client.get_queue_session_receiver(
                    queue_name, max_wait_time=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.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_session_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
Пример #3
0
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()