{"id":39480,"date":"2020-06-03T06:00:12","date_gmt":"2020-06-03T13:00:12","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/premier-developer\/?p=39480"},"modified":"2020-05-16T13:33:28","modified_gmt":"2020-05-16T20:33:28","slug":"manage-service-bus-queue-messages-with-python","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/manage-service-bus-queue-messages-with-python\/","title":{"rendered":"Manage Service Bus Queue messages with Python"},"content":{"rendered":"<p>In this post, Big Data Consultant <a href=\"https:\/\/www.linkedin.com\/in\/rakhi-guha-3930aa65\">Rakhi Guha<\/a> shows how to get started with Python scripting to manage Service Bus Queue messages.<\/p>\n<hr \/>\n<p>This article provides a detailed idea on the following operation using python scripting platform.<\/p>\n<ol>\n<li>Send messages to Service Bus Queue<\/li>\n<li>Resubmit messages from Dead letter queue to Main queue<\/li>\n<li>Manage latest message instance by pushing existing message to Dead letter queue before submission.<\/li>\n<\/ol>\n<h4>Send Message to Service Bus Queue:<\/h4>\n<pre class=\"prettyprint\">from azure.servicebus import ServiceBusClient\r\nfrom azure.servicebus import QueueClient, Message\r\n# Create the QueueClient\r\nqueue_client = QueueClient.from_connection_string(\"&lt;Connection string&gt;\", \"&lt;QUEUE NAME&gt;\")\r\n# Send a test message to the queue\r\nmsg = Message(b'{\"MessageName\":\"DEMO\",\"MessageText\":\"TestMessage\"}')\r\nqueue_client.send(msg)<\/pre>\n<h4>Resubmit messages from Dead letter Queue to Main Queue:<\/h4>\n<pre class=\"prettyprint\">from azure.servicebus import ServiceBusClient\r\nfrom azure.servicebus import ServiceBusClient, Message\r\nimport json\r\nconnectionString = \"&lt;Connection String&gt;\"\r\nserviceBusClient = ServiceBusClient.from_connection_string(connectionString)\r\nqueueName = \"&lt;Queue Name&gt;\"\r\nqueueClient = serviceBusClient.get_queue(queueName)\r\nmessageToSend=[]\r\nwith queueClient.get_deadletter_receiver(prefetch=5) as queueReceiver:\r\n\tmessages = queueReceiver.fetch_next(timeout=100)\r\n\tfor message in messages:\r\n\t\tbody=next(message.body)\r\n\t\tmessageToSend.append(body)\t\r\n\t\tmessage.complete()\r\nwith queueClient.get_sender() as sender:\r\n\tfor message in messageToSend:\r\n\t\tnewmessage=Message(message)\r\n\t\tsender.send(newmessage)\r\n<\/pre>\n<h4>Manage latest message instance by pushing existing message to Dead letter Queue before submission:<\/h4>\n<pre class=\"prettyprint\">from azure.servicebus import ServiceBusClient\r\nfrom azure.servicebus import ServiceBusClient, Message\r\nimport json\r\nMessageToSend='{\"MessageName\":\"DEMO\",\"MessageText\":\"TestMessage1\"}'\r\nconnectionString = \"&lt;Connection String&gt;\"\r\nserviceBusClient = ServiceBusClient.from_connection_string(connectionString)\r\nqueueName = \"&lt;QUEUE NAME&gt;\"\r\nqueueClient = serviceBusClient.get_queue(queueName)\r\nMessageTosendJson=json.loads(MessageToSend)\r\nwith queueClient.get_receiver(prefetch=5) as queueReceiver:\r\n\tmessages = queueReceiver.fetch_next(timeout=100)\r\n\tfor message in messages:\r\n\t\texistingMessage=json.loads(str(message.body,'utf-8'))\r\n\t\tif MessageTosendJson[\"MessageName\"] == existingMessage[\"MessageName\"]:\tmessage.dead_letter()\r\n\r\nmsg = Message(MessageToSend)\r\nqueueClient.send(msg)<\/pre>\n<h4>Note:<\/h4>\n<ol>\n<li>azure.servicebus namespace contains the required code artifacts for all the operation and class instances to be required.<\/li>\n<li>In the above scripts, the place holder needs to be replaced with the appropriate value.<\/li>\n<li>Message format will be use case specific and based on the message format, the logic for message sanity check in script 3 will be changed.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In this post, Big Data Consultant Rakhi Guha shows how to get started with Python scripting to manage Service Bus Queue messages.<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[25,5940,1],"tags":[7717,1129],"class_list":["post-39480","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-development","category-permierdev","tag-azure-service-bus","tag-python"],"acf":[],"blog_post_summary":"<p>In this post, Big Data Consultant Rakhi Guha shows how to get started with Python scripting to manage Service Bus Queue messages.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/39480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=39480"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/39480\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=39480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=39480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=39480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}