{"id":5402,"date":"2017-09-25T21:06:39","date_gmt":"2017-09-25T21:06:39","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5402"},"modified":"2017-09-25T21:06:39","modified_gmt":"2017-09-25T21:06:39","slug":"using-azure-queues","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-azure-queues\/","title":{"rendered":"Using Azure Queues"},"content":{"rendered":"<p>Azure Queue sits inside your Azure storage and allows messages to flow through a queue system (similar in some ways, but not as fully featured as MSMQ, TIBCO etc.).<\/p>\n<p><strong>Creating a Queue<\/strong><\/p>\n<p>Obviously we can create a queue using the Azure Portal or Azure Storage Explorer, but let&#8217;s create queue via code, using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar storageAccount = CloudStorageAccount.Parse(\r\n   CloudConfigurationManager.GetSetting(\r\n      &quot;StorageConnectionString&quot;));\r\n\r\nvar client = storageAccount.CreateCloudQueueClient();\r\nvar queue = client.GetQueueReference(&quot;my-queue&quot;);\r\nqueue.CreateIfNotExists();\r\n<\/pre>\n<p><strong>Sending a message to our Queue<\/strong><\/p>\n<p>Adding a message to the queue is as simple as, the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar storageAccount = CloudStorageAccount.Parse(\r\n   CloudConfigurationManager.GetSetting(\r\n      &quot;StorageConnectionString&quot;));\r\n\r\nvar client = storageAccount.CreateCloudQueueClient();\r\nvar queue = client.GetQueueReference(&quot;my-queue&quot;);\r\n\r\nvar message = new CloudQueueMessage(&quot;Hello World&quot;);\r\nqueue.AddMessage(message);\r\n<\/pre>\n<p><strong>Peeking at a Queue<\/strong><\/p>\n<p>We can peek at a message on the queue, which basically means we can look at the message without affect it, using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar storageAccount = CloudStorageAccount.Parse(\r\n   CloudConfigurationManager.GetSetting(\r\n      &quot;StorageConnectionString&quot;));\r\n\r\nvar client = storageAccount.CreateCloudQueueClient();\r\nvar queue = client.GetQueueReference(&quot;my-queue&quot;);\r\n\r\nvar message = queue.PeekMessage();\r\nConsole.WriteLine(message.AsString);\r\n<\/pre>\n<p>As the name suggests, in this instance we&#8217;re peeking at the current or at least, most recent message. But we can also use the <em>PeekMessages<\/em> method to enumerate over a number of messages on the queue. <\/p>\n<p>Here&#8217;s an example of peeking at 32 messages (it appears 32 is the maximum number of messages we&#8217;re allowed to peek, currently anything above this causes a bad request exception)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nforeach (var message in queue.PeekMessages(32))\r\n{\r\n   Console.WriteLine(message.AsString);\r\n}\r\n<\/pre>\n<p><strong>Getting messages<\/strong><\/p>\n<p>Unlike, for example TIBCO RV, Azure Queue&#8217;s do not have subscribers and therefore do not push messages to subscribers (like and event might). Once a message is de-queued it will be marked as invisible (see <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/microsoft.windowsazure.storage.queue.cloudqueue.getmessage?view=azurestorage-8.1.3\" rel=\"noopener\" target=\"_blank\">CloudQueue.GetMessage Method<\/a>. <\/p>\n<p>To de-queue a message we use <em>GetMessage<\/em> on a Queue. As one might expect, once the message is marked as invisible, subsequent calls to GetMessage will not return the hidden message until the visibility timeout is reached and the message will then reappear and be available again from subsequent GetMessage calls.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar storageAccount = CloudStorageAccount.Parse(\r\n   CloudConfigurationManager.GetSetting(\r\n      &quot;StorageConnectionString&quot;));\r\n\r\nvar client = storageAccount.CreateCloudQueueClient();\r\nvar queue = client.GetQueueReference(&quot;my-queue&quot;);\r\n\r\nvar message = queue.GetMessage();\r\n\r\nConsole.WriteLine(message.AsString);\r\n<\/pre>\n<p>Now if you change the <em>var message = queue.GetMessage();<\/em> line to the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar message = queue.GetMessage(TimeSpan.FromSeconds(10));\r\n<\/pre>\n<p>and then within the Azure Portal or Azure Storage explorer refresh immediately after it&#8217;s de-queued the message will disappear but then refreshing again after 10 seconds and the message will reappear in the queue with it&#8217;s dequeue count incremented.<\/p>\n<p>Like PeekMessages, we can call <em>GetMessages<\/em> to get a batch of messages (between 1 and 32 messages).<\/p>\n<p><strong>Deleting a message<\/strong><\/p>\n<p>To remove a message altogether use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nqueue.DeleteMessage(message);\r\n<\/pre>\n<p>This would usually be called after <em>GetMessage<\/em> is called, but obviously this is dependent upon your requirements. It might be called after a certain dequeuer count or simply after every GetMessage call, but remember if you do not delete the message it will reappear on your queue until an it&#8217;s maximum <em>time to live<\/em> ends, as supplied via the <em>AddMessage<\/em> method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure Queue sits inside your Azure storage and allows messages to flow through a queue system (similar in some ways, but not as fully featured as MSMQ, TIBCO etc.). Creating a Queue Obviously we can create a queue using the Azure Portal or Azure Storage Explorer, but let&#8217;s create queue via code, using the following [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[96],"tags":[],"class_list":["post-5402","post","type-post","status-publish","format-standard","hentry","category-azure-2"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5402"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5402\/revisions"}],"predecessor-version":[{"id":5435,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5402\/revisions\/5435"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}