Ejemplo n.º 1
0
    def test_subscription_by_sas_token_credential_conn_str_send_basic(
            self, servicebus_namespace, servicebus_namespace_key_name,
            servicebus_namespace_primary_key, servicebus_topic,
            servicebus_subscription, **kwargs):
        fully_qualified_namespace = servicebus_namespace.name + '.servicebus.windows.net'
        with ServiceBusClient(
                fully_qualified_namespace=fully_qualified_namespace,
                credential=ServiceBusSharedKeyCredential(
                    policy=servicebus_namespace_key_name,
                    key=servicebus_namespace_primary_key),
                logging_enable=False) as sb_client:

            with sb_client.get_topic_sender(
                    topic_name=servicebus_topic.name) as sender:
                message = ServiceBusMessage(b"Sample topic message")
                sender.send_messages(message)

            with sb_client.get_subscription_receiver(
                    topic_name=servicebus_topic.name,
                    subscription_name=servicebus_subscription.name,
                    max_wait_time=5) as receiver:
                count = 0
                for message in receiver:
                    count += 1
                    message.complete()
            assert count == 1
Ejemplo n.º 2
0
    def test_subscription_by_subscription_client_conn_str_receive_basic(
            self, servicebus_namespace_connection_string, servicebus_topic,
            servicebus_subscription, **kwargs):

        with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:
            with sb_client.get_topic_sender(
                    topic_name=servicebus_topic.name) as sender:
                message = ServiceBusMessage(b"Sample topic message")
                sender.send_messages(message)

            with sb_client.get_subscription_receiver(
                    topic_name=servicebus_topic.name,
                    subscription_name=servicebus_subscription.name,
                    max_wait_time=5) as receiver:
                count = 0
                for message in receiver:
                    count += 1
                    message.complete()
            assert count == 1
Ejemplo n.º 3
0
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]

    # [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(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_messages(max_wait_time=5)
        for message in messages:
            print(str(message))
            message.complete()
    # [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("Properties: {}".format(message.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]
        messages = servicebus_receiver.receive_messages(max_wait_time=5)
        for message in messages:
            message.abandon()
        # [END abandon_message]

    # [START receive_forever]
    with servicebus_receiver:
        for message in servicebus_receiver.get_streaming_message_iter():
            print(str(message))
            message.complete()
Ejemplo n.º 4
0
    def test_subscription_by_servicebus_client_receive_batch_with_deadletter(
            self, servicebus_namespace_connection_string, servicebus_topic,
            servicebus_subscription, **kwargs):

        with ServiceBusClient.from_connection_string(
                servicebus_namespace_connection_string,
                logging_enable=False) as sb_client:

            with sb_client.get_subscription_receiver(
                    topic_name=servicebus_topic.name,
                    subscription_name=servicebus_subscription.name,
                    max_wait_time=5,
                    receive_mode=ReceiveMode.PeekLock,
                    prefetch_count=10) as receiver:

                with sb_client.get_topic_sender(
                        servicebus_topic.name) as sender:
                    for i in range(10):
                        message = ServiceBusMessage(
                            "Dead lettered message no. {}".format(i))
                        sender.send_messages(message)

                count = 0
                messages = receiver.receive_messages()
                while messages:
                    for message in messages:
                        print_message(_logger, message)
                        count += 1
                        message.dead_letter(
                            reason="Testing reason",
                            error_description="Testing description")
                    messages = receiver.receive_messages()

            assert count == 10

            with sb_client.get_subscription_receiver(
                    topic_name=servicebus_topic.name,
                    subscription_name=servicebus_subscription.name,
                    max_wait_time=5,
                    receive_mode=ReceiveMode.PeekLock) as receiver:
                count = 0
                for message in receiver:
                    print_message(_logger, message)
                    message.complete()
                    count += 1
            assert count == 0

            with sb_client.get_subscription_receiver(
                    topic_name=servicebus_topic.name,
                    subscription_name=servicebus_subscription.name,
                    sub_queue=SubQueue.DeadLetter,
                    max_wait_time=5,
                    receive_mode=ReceiveMode.PeekLock) as dl_receiver:
                count = 0
                for message in dl_receiver:
                    message.complete()
                    count += 1
                    assert message.dead_letter_reason == 'Testing reason'
                    assert message.dead_letter_error_description == 'Testing description'
                    assert message.properties[
                        b'DeadLetterReason'] == b'Testing reason'
                    assert message.properties[
                        b'DeadLetterErrorDescription'] == b'Testing description'
                assert count == 10