def main(events: List[func.EventHubEvent], doc: func.Out[func.Document]): #pylint: disable=E1136 sb_connection_str = os.environ['AzureServiceBusConnectionString'] sb_client = TopicClient.from_connection_string(conn_str=sb_connection_str, name='receipts') for event in events: payload = event.get_body().decode('utf-8') logging.info('Python EventHub trigger processed an event: %s', payload) doc.set(func.Document.from_json(payload)) order = json.loads(payload) if (order['header']['receiptUrl'] != None): totalCost = 0.0 for line_item in order['details']: totalCost = totalCost + float(line_item['totalCost']) x = { "totalItems": len(order['details']), "totalCost": totalCost, "salesNumber": order['header']['salesNumber'], "salesDate": order['header']['dateTime'], "storeLocation": order['header']['locationId'], "receiptUrl": order['header']['receiptUrl'] } msg = Message(json.dumps(x)) msg.user_properties = {'totalCost': x['totalCost']} sb_client.send(msg)
def test_topic_by_topic_client_conn_str_send_basic(live_servicebus_config, standard_topic): topic_client = TopicClient.from_connection_string(live_servicebus_config['conn_str'], name=standard_topic, debug=True) with topic_client.get_sender() as sender: message = Message(b"Sample topic message") sender.send(message) message = Message(b"Another sample topic message") topic_client.send(message)
def test_topic_by_topic_client_conn_str_receive_fail( self, servicebus_namespace_connection_string, servicebus_topic, **kwargs): topic_client = TopicClient.from_connection_string( servicebus_namespace_connection_string, name=servicebus_topic.name, debug=False) with pytest.raises(AttributeError): topic_client.get_receiver()
def test_topic_by_topic_client_conn_str_send_basic(live_servicebus_config, standard_topic): topic_client = TopicClient.from_connection_string( live_servicebus_config['conn_str'], name=standard_topic, debug=False) with topic_client.get_sender() as sender: message = Message(b"Sample topic message") sender.send(message) message = Message(b"Another sample topic message") topic_client.send(message)
def test_topic_by_topic_client_conn_str_send_basic( self, servicebus_namespace_connection_string, servicebus_topic, **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) message = Message(b"Another sample topic message") topic_client.send(message)
def send_message(): try: try: conn_key = request.args['connection_key'] conn_str = CONNECTION_LIST[conn_key]['conn_str'] topic_name = request.args.get('topic_name') queue_name = request.args.get('queue_name') session_id = request.args.get('session_id') except (BadRequestKeyError, ValueError) as er: return respond(400, 'invalid parameter values') client = None if request.endpoint == 'TOPIC_MSG_RECEIVER_ENDPOINT': client = TopicClient.from_connection_string(conn_str=conn_str, name=topic_name) elif request.endpoint == 'QUEUE_MSG_RECEIVER_ENDPOINT': client = QueueClient.from_connection_string(conn_str=conn_str, name=queue_name) else: return respond(404, 'not found') request_data = request.get_json() msg_list = None if isinstance(request_data, dict): msg_list = [Message(json.dumps(request_data))] elif isinstance(request_data, list): msg_list = [Message(json.dumps(msg)) for msg in request_data] response_data = [] if msg_list: send_result_list = client.send(messages=msg_list, message_timeout=0, session=session_id) for i, send_result in enumerate(send_result_list): response_data.append(send_result) if not send_result[0]: logger.warning( 'failed to send msg for message %s(index %d with message %s)' % (msg_list[i].body, i, send_result[1])) return respond(200, response_data) except Exception as er: logger.exception(er) return respond(500, str(er))
def main(event: func.EventHubEvent): cosmosdb_pos_masterKey = os.environ.get('cosmosdb_pos_masterKey') cosmosdb_pos_host = os.environ.get('cosmosdb_pos_host') cosmosdb_pos_databaseId = os.environ.get('cosmosdb_pos_databaseId') cosmosdb_pos_collectionId = os.environ.get('cosmosdb_pos_collectionId') servicebus_pos_conn_str = os.environ.get('servicebus_pos_conn_str') servicebus_pos_topic_name = os.environ.get('servicebus_pos_topic_name') dbLink = 'dbs/' + cosmosdb_pos_databaseId collLink = dbLink + '/colls/' + cosmosdb_pos_collectionId client = document_client.DocumentClient( cosmosdb_pos_host, {'masterKey': cosmosdb_pos_masterKey}) event_str = event.get_body().decode('utf-8') logging.info(event_str) event_json = json.loads(event_str) for e in event_json: e['id'] = e['header']['salesNumber'] client.UpsertDocument(collLink, e) totalCost = float(e["header"]["totalCost"]) receipt = { "totalItems": len(e["details"]), "totalCost": totalCost, "salesNumber": e["header"]["salesNumber"], "salesDate": e["header"]["dateTime"], "storeLocation": e["header"]["locationId"], "receiptUrl": e["header"]["receiptUrl"] } tc = TopicClient.from_connection_string(servicebus_pos_conn_str, servicebus_pos_topic_name) msg = Message(json.dumps(receipt).encode('utf8'), ContentType='application/json;charset=utf-8') msg.user_properties = {'totalCost': totalCost} tc.send(msg)
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 __init__(self): work_directory = os.environ['RAFT_WORK_DIRECTORY'] with open(os.path.join(work_directory, 'task-config.json'), 'r') as task_config: self.config = json.load(task_config) connection_str = os.environ['RAFT_SB_OUT_SAS'] self.topic_client = TopicClient.from_connection_string(connection_str) self.telemetry_client = TelemetryClient( instrumentation_key=os.environ['RAFT_APP_INSIGHTS_KEY']) self.job_id = os.environ['RAFT_JOB_ID'] self.container_name = os.environ['RAFT_CONTAINER_NAME'] self.telemetry_properties = { "jobId": self.job_id, "taskIndex": os.environ['RAFT_TASK_INDEX'], "containerName": self.container_name }
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
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()
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__': topic_client = TopicClient.from_connection_string(connection_str, name="pytopic", debug=True) with topic_client.get_sender() as sender: message = Message(b"sample topic message") sender.send(message)
from azure.servicebus import TopicClient from azure.servicebus import Message 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__': topic_client = TopicClient.from_connection_string(connection_str, name="pytopic", debug=True) with topic_client.get_sender() as sender: message = Message(b"sample topic message") sender.send(message)
def test_topic_by_topic_client_conn_str_receive_fail(live_servicebus_config, standard_topic): topic_client = TopicClient.from_connection_string( live_servicebus_config['conn_str'], name=standard_topic, debug=False) with pytest.raises(AttributeError): topic_client.get_receiver()
def test_topic_by_topic_client_conn_str_receive_fail(live_servicebus_config, standard_topic): topic_client = TopicClient.from_connection_string(live_servicebus_config['conn_str'], name=standard_topic, debug=True) with pytest.raises(AttributeError): topic_client.get_receiver()