public async Task CloudQueueGetMessagesAsync() { CloudQueueClient client = GenerateCloudQueueClient(); string name = GenerateNewQueueName(); CloudQueue queue = client.GetQueueReference(name); await queue.CreateAsync(); int messageCount = 30; List <CloudQueueMessage> emptyMessages = (await queue.GetMessagesAsync(messageCount)).ToList(); Assert.AreEqual(0, emptyMessages.Count); List <string> messageContentList = new List <string>(); for (int i = 0; i < messageCount; i++) { string messageContent = i.ToString(); CloudQueueMessage message = new CloudQueueMessage(messageContent); await queue.AddMessageAsync(message); messageContentList.Add(messageContent); } List <CloudQueueMessage> receivedMessages = (await queue.GetMessagesAsync(messageCount)).ToList(); Assert.AreEqual(messageCount, receivedMessages.Count); for (int i = 0; i < messageCount; i++) { Assert.IsTrue(messageContentList.Contains(receivedMessages[i].AsString)); } await queue.DeleteAsync(); }
protected override async Task StartAsync() { InfoLogging(string.Format("{0} - Processing", QueueName)); _queue = await _clientFactory.CreateStorageQueueClientAsync(QueueName).ConfigureAwait(false); var stopWatch = new Stopwatch(); while (!Token.IsCancellationRequested) { await _queue.FetchAttributesAsync().ConfigureAwait(false); if (_queue.ApproximateMessageCount.GetValueOrDefault() <= 0) { await Task.Delay(LoopWaitTime).ConfigureAwait(false); continue; } var messages = await _queue .GetMessagesAsync(MessageCount, TimeSpan.FromSeconds(10), null, null, Token) .ConfigureAwait(false); var cloudQueueMessages = messages as IList<CloudQueueMessage> ?? messages.ToList(); if (!cloudQueueMessages.Any()) continue; var correlationId = Guid.NewGuid().ToString(); DebugLogging(string.Format("{0} - Received {1} new messages", QueueName, cloudQueueMessages.Count), correlationId); stopWatch.Restart(); await Do(cloudQueueMessages).ConfigureAwait(false); stopWatch.Stop(); var timeSpan = stopWatch.Elapsed; DebugLogging(string.Format("{0} - Processed messages", QueueName), correlationId, timeSpan.TotalSeconds); } }
/// <summary> /// Demonstrate adding a number of messages, checking the message count and batch retrieval of messages. During retrieval we /// also set the visibility timeout to 5 minutes. Visibility timeout is the amount of time message is not visible to other /// clients after a GetMessageOperation assuming DeleteMessage is not called. /// </summary> /// <param name="queue">The sample queue</param> private static async Task ProcessBatchOfMessagesAsync(CloudQueue queue) { // Enqueue 20 messages by which to demonstrate batch retrieval Console.WriteLine("7. Enqueue 20 messages."); for (int i = 0; i < 20; i++) { await queue.AddMessageAsync(new CloudQueueMessage(string.Format("{0} - {1}", i, "Hello World"))); } // The FetchAttributes method asks the Queue service to retrieve the queue attributes, including an approximation of message count Console.WriteLine("8. Get the queue length"); queue.FetchAttributes(); int? cachedMessageCount = queue.ApproximateMessageCount; Console.WriteLine("Number of messages in queue: {0}", cachedMessageCount); // Dequeue a batch of 21 messages (up to 32) and set visibility timeout to 5 minutes. Note we are dequeuing 21 messages because the earlier // UpdateEnqueuedMessage method left a message on the queue hence we are retrieving that as well. Console.WriteLine("9. Dequeue 21 messages, allowing 5 minutes for the clients to process."); foreach (CloudQueueMessage msg in await queue.GetMessagesAsync(21, TimeSpan.FromMinutes(5), null, null)) { Console.WriteLine("Processing & deleting message with content: {0}", msg.AsString); // Process all messages in less than 5 minutes, deleting each message after processing. await queue.DeleteMessageAsync(msg); } }
/// <inheritdoc /> public async Task<IEnumerable<CloudQueueMessage>> GetMessagesAsync(CloudQueue queue, int messageCount, TimeSpan timeout) { if (queue == null) { throw new ArgumentNullException(nameof(queue)); } try { IEnumerable<CloudQueueMessage> messages = await queue.GetMessagesAsync(messageCount, timeout, options: null, operationContext: null); return messages; } catch (Exception ex) { string errorMessage = GetStorageErrorMessage(ex); int statusCode = GetStorageStatusCode(ex); string msg = string.Format(CultureInfo.CurrentCulture, AzureStorageResources.StorageManager_OperationFailed, statusCode, errorMessage); _logger.Error(msg, ex); return Enumerable.Empty<CloudQueueMessage>(); } }
private static async Task ProcessBatchOfMessagesAsync(CloudQueue queue) { Console.WriteLine("7. Enqueue 20 messages."); for (int i = 0; i < 20; i++) { await queue.AddMessageAsync(new CloudQueueMessage(string.Format("{0} - {1}", i, "Hello World"))); } Console.WriteLine("8. Get the queue length"); queue.FetchAttributes(); int? cachedMessageCount = queue.ApproximateMessageCount; Console.WriteLine("Number of messages in queue: {0}", cachedMessageCount); Console.WriteLine("9. Dequeue 21 messages, allowing 5 minutes for the clients to process."); foreach (CloudQueueMessage msg in await queue.GetMessagesAsync(21, TimeSpan.FromMinutes(5), null, null)) { Console.WriteLine("Processing & deleting message with content: {0}", msg.AsString); await queue.DeleteMessageAsync(msg); } }