{"id":24510,"date":"2021-04-17T01:54:40","date_gmt":"2021-04-17T01:54:40","guid":{"rendered":"http:\/\/matthewjrodgers.com\/?p=24510"},"modified":"2021-04-17T01:54:41","modified_gmt":"2021-04-17T01:54:41","slug":"microsoft-azure-working-with-azure-queue-storage","status":"publish","type":"post","link":"https:\/\/matthewjrodgers.com\/?p=24510","title":{"rendered":"Microsoft Azure: Working with Azure Queue storage"},"content":{"rendered":"\n<p>Taking\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.microsoft.com\/en-us\/learn\/modules\/communicate-between-apps-with-azure-queue-storage\/\" target=\"_blank\">this<\/a>\u00a0Microsoft Learn module, I was able to create an app that interacts with Azure Queue storage. Azure Queue storage, as Microsoft describes, implements the publish-subscribe pattern in the cloud to allow communication between applications. Below I showcase parts of the app I created. The full code project can be found\u00a0<a href=\"https:\/\/github.com\/matthewjrodgers\/QueueApp-public-github-version\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>This function creates a queue in Azure Queue storage and then subsequently sends a message to that queue.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        \/\/ this sends a message to the queue\r\n        \/\/ best practice is only for the publisher to create the queue.\r\n        static async Task SendArticleAsync(string newsMessage)\r\n        {\r\n            \/\/ create a CloudQueue object that we can use to work with the queue.\r\n            CloudQueue queue = GetQueue();\r\n            \/\/ create the queue if necessary, or return false if the queue already exists.\r\n            \/\/ this ensures the queue is ready for use\r\n            bool createdQueue = await queue.CreateIfNotExistsAsync();\r\n            if (createdQueue)\r\n            {\r\n                Console.WriteLine(\"The queue of news articles was created.\");\r\n            }\r\n            \/\/ represents a message\r\n            CloudQueueMessage articleMessage = new CloudQueueMessage(newsMessage);\r\n            \/\/ use the CloudQueue object to send the message to the queue\r\n            await queue.AddMessageAsync(articleMessage);\r\n        }<\/code><\/pre>\n\n\n\n<p>This function reads the next message in the queue in Azure Queue storage, processes it, and the deletes it from the queue. As you can see, I don\u2019t actually process the text. I just capture it and return it. Obviously, in a production situation the next message in the queue could require data processing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/ read next message in queue, process it, and delete it from the queue\r\n        static async Task&lt;string> ReceiveArticleAsync()\r\n        {\r\n            \/\/ get our object we can use to create, delete, and check for an existing queue.\r\n            \/\/ this is called our queue reference\r\n            CloudQueue queue = GetQueue();\r\n            \/\/ check if the queue exists.\r\n            \/\/ if we attempt to retrieve a message from a non-existent queue, the API will throw an exception\r\n            bool exists = await queue.ExistsAsync();\r\n            if (exists)\r\n            {\r\n                \/\/ represents a message\r\n                \/\/ get the message. i.e. the next message in the queue\r\n                \/\/ the return value will be null if the queue is empty\r\n                CloudQueueMessage retrievedArticle = await queue.GetMessageAsync();\r\n                if (retrievedArticle != null)\r\n                {\r\n                    \/\/ this is where processing would be performed\r\n                    \/\/ this gets the contents of the message\r\n                    string newsMessage = retrievedArticle.AsString;\r\n                    \/\/ delete the message after processing completes\r\n                    await queue.DeleteMessageAsync(retrievedArticle);\r\n                    return newsMessage;\r\n                }\r\n\r\n            }\r\n            return \"&lt;queue empty or not created>\";\r\n        }<\/code><\/pre>\n\n\n\n<p>And that\u2019s it for this post! Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Taking\u00a0this\u00a0Microsoft Learn module, I was able to create an app that interacts with Azure Queue storage. Azure Queue storage, as Microsoft describes, implements the publish-subscribe pattern in the cloud to allow communication between applications. Below I showcase parts of the app I created. The full code project can be found\u00a0here. This function creates a queue&hellip; <a href=\"https:\/\/matthewjrodgers.com\/?p=24510\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Microsoft Azure: Working with Azure Queue storage<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[5,4],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=\/wp\/v2\/posts\/24510"}],"collection":[{"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24510"}],"version-history":[{"count":1,"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=\/wp\/v2\/posts\/24510\/revisions"}],"predecessor-version":[{"id":24511,"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=\/wp\/v2\/posts\/24510\/revisions\/24511"}],"wp:attachment":[{"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/matthewjrodgers.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}