def test_subscription_by_subscription_client_conn_str_send_fail( live_servicebus_config, standard_subscription): topic_name, subscription_name = standard_subscription sub_client = SubscriptionClient.from_connection_string( live_servicebus_config['conn_str'], subscription_name, topic=topic_name, debug=True) with pytest.raises(AttributeError): sub_client.get_sender()
def test_subscription_by_subscription_client_conn_str_send_fail( self, servicebus_namespace_connection_string, servicebus_topic, servicebus_subscription, **kwargs): sub_client = SubscriptionClient.from_connection_string( servicebus_namespace_connection_string, servicebus_subscription.name, topic=servicebus_topic.name, debug=False) with pytest.raises(AttributeError): sub_client.get_sender()
def test_subscription_by_subscription_client_conn_str_receive_basic( live_servicebus_config, standard_subscription): topic_name, subscription_name = standard_subscription topic_client = TopicClient.from_connection_string( live_servicebus_config['conn_str'], name=topic_name, debug=True) with topic_client.get_sender() as sender: message = Message(b"Sample topic message") sender.send(message) sub_client = SubscriptionClient.from_connection_string( live_servicebus_config['conn_str'], subscription_name, topic=topic_name, debug=True) with sub_client.get_receiver(idle_timeout=5) as receiver: count = 0 for message in receiver: count += 1 message.complete() assert count == 1
def test_subscription_by_subscription_client_conn_str_receive_basic( self, servicebus_namespace_connection_string, servicebus_topic, servicebus_subscription, **kwargs): topic_client = TopicClient.from_connection_string( servicebus_namespace_connection_string, name=servicebus_topic.name, debug=False) with topic_client.get_sender() as sender: message = Message(b"Sample topic message") sender.send(message) sub_client = SubscriptionClient.from_connection_string( servicebus_namespace_connection_string, servicebus_subscription.name, topic=servicebus_topic.name, debug=False) with sub_client.get_receiver(idle_timeout=5) as receiver: count = 0 for message in receiver: count += 1 message.complete() assert count == 1
if not azure_logger.handlers: azure_logger.setLevel(level) handler = logging.StreamHandler(stream=sys.stdout) handler.setFormatter( logging.Formatter( '%(asctime)s %(name)-12s %(levelname)-8s %(message)s')) azure_logger.addHandler(handler) uamqp_logger = logging.getLogger("uamqp") if not uamqp_logger.handlers: uamqp_logger.setLevel(logging.INFO) uamqp_logger.addHandler(handler) return azure_logger logger = get_logger(logging.DEBUG) connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] if __name__ == '__main__': sub_client = SubscriptionClient.from_connection_string( connection_str, name="pytopic/Subscriptions/pysub", debug=True) with sub_client.get_receiver() as receiver: batch = receiver.fetch_next(timeout=10) while batch: print("Received {} messages".format(len(batch))) for message in batch: message.complete() batch = receiver.fetch_next(timeout=10)
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()
# Licensed under the MIT License. See License.txt in the project root for # license information. # ------------------------------------------------------------------------- import sys import os from azure.servicebus import SubscriptionClient CONNECTION_STR = os.environ['SERVICE_BUS_CONNECTION_STR'] TOPIC_NAME = os.environ["SERVICE_BUS_TOPIC_NAME"] SUBSCRIPTION_NAME = os.environ["SERVICE_BUS_SUBSCRIPTION_NAME"] if __name__ == '__main__': sub_client = SubscriptionClient.from_connection_string( CONNECTION_STR, name=SUBSCRIPTION_NAME, topic=TOPIC_NAME, debug=False) print("Got subscription clinet for " + TOPIC_NAME + " using the " + SUBSCRIPTION_NAME + " subscription") with sub_client.get_receiver() as receiver: # using no value for the timeout, allow for an active receiver batch = receiver.fetch_next() while batch: print("Received {} messages".format(len(batch))) for message in batch: message.complete() print(message) batch = receiver.fetch_next()
def get_logger(level): azure_logger = logging.getLogger("azure") if not azure_logger.handlers: azure_logger.setLevel(level) handler = logging.StreamHandler(stream=sys.stdout) handler.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')) azure_logger.addHandler(handler) uamqp_logger = logging.getLogger("uamqp") if not uamqp_logger.handlers: uamqp_logger.setLevel(logging.INFO) uamqp_logger.addHandler(handler) return azure_logger logger = get_logger(logging.DEBUG) connection_str = os.environ['SERVICE_BUS_CONNECTION_STR'] if __name__ == '__main__': sub_client = SubscriptionClient.from_connection_string( connection_str, name="pytopic/Subscriptions/pysub", debug=True) with sub_client.get_receiver() as receiver: batch = receiver.fetch_next(timeout=10) while batch: print("Received {} messages".format(len(batch))) for message in batch: message.complete() batch = receiver.fetch_next(timeout=10)
def receive_messages(): try: try: conn_key = request.args['connection_key'] conn_str = CONNECTION_LIST[conn_key]['conn_str'] sub_name = request.args[ 'sub_name'] if request.endpoint == 'SUBSCRIPTION_MSG_PUBLISHER_ENDPOINT' else None topic_name = request.args.get('topic_name') queue_name = request.args.get('queue_name') session_id = request.args.get('session_id') idle_timeout = int( request.args.get('idle_timeout', DEFAULT_IDLE_TIMEOUT)) prefetch = int(request.args.get('prefetch', DEFAULT_PREFETCH)) except (BadRequestKeyError, ValueError) as er: logger.info('request args %s' % request.args) return respond(400, 'invalid parameter values') client = None if request.endpoint == 'SUBSCRIPTION_MSG_PUBLISHER_ENDPOINT': client = SubscriptionClient.from_connection_string( conn_str, name=sub_name, topic=topic_name) elif request.endpoint == 'QUEUE_MSG_PUBLISHER_ENDPOINT': client = QueueClient.from_connection_string(conn_str, name=queue_name) response_body = bytearray() response_body += b'[' is_first = True while True: try: if session_id == '*': session_id = NEXT_AVAILABLE with client.get_receiver(session=session_id, idle_timeout=idle_timeout, prefetch=prefetch) as msg_receiver: msg_batch = msg_receiver.fetch_next( max_batch_size=prefetch, timeout=1) while msg_batch: for msg in msg_batch: msg_body = bytearray() for bytes in msg.body: msg_body += bytes try: if json.loads(msg_body): if is_first: is_first = False else: response_body += b',' response_body += msg_body msg.complete() except json.JSONDecodeError as er: logger.warning( 'not deleting fetched messages from service bus due to failed JSON serialization' ) msg_batch = msg_receiver.fetch_next( max_batch_size=None, timeout=1) if session_id != '*': break except NoActiveSession as err: break response_body += b']' return Response(response=response_body, mimetype='application/json') except Exception as er: logger.exception(er) return respond(500, str(er))