/// <summary> /// Retrieves messages that match the criteria defined in the receivable. /// </summary> /// <param name="receivable">The receivable to use to retrieve messages.</param> /// <returns>A response containing any received messages.</returns> private ReceiveResponse GetMessages(Receivable receivable) { var messages = _messages .Where(message => message.Value.Expiry == null || message.Value.Expiry > DateTime.Now) .Take(receivable.MessagesToReceive) .Select(message => new ReceivedMessage { Id = message.Value.Id, Content = message.Value.Content, Deletable = new MemoryDeletable { Queue = receivable.Queue, MessageId = message.Value.Id.ToString() }, Queue = message.Value.Queue }); return(new ReceiveResponse { Messages = messages }); }
/// <inheritdoc /> public override async Task <ReceiveResponse> Receive( Receivable receivable, CancellationToken cancellationToken = default ) { if (receivable.MessagesToReceive > 10) { receivable.MessagesToReceive = 10; } var messages = await _client.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = _options.QueueUrl, WaitTimeSeconds = receivable.SecondsToWait ?? 0, MaxNumberOfMessages = receivable.MessagesToReceive }, cancellationToken).ConfigureAwait(false); var messagesToReturn = new List <ReceivedMessage>(); foreach (var message in messages.Messages) { var deletable = new SQSDeletable { Queue = receivable.Queue, ReceiptHandle = message.ReceiptHandle }; var deserialised = await DeserialiseAndHandle(message.Body, deletable, cancellationToken) .ConfigureAwait(false); if (deserialised != null) { messagesToReturn.Add(deserialised); } } return(new ReceiveResponse { Messages = messagesToReturn }); }
/// <summary> /// Mimics a long poll scenario available in other providers. /// </summary> /// <param name="receivable">The receivable to use to long poll and retrieve messages.</param> /// <returns>A response containing any received messages.</returns> private async Task <ReceiveResponse> LongPoll(Receivable receivable) { var messagesReceived = new List <ReceivedMessage>(); // ReSharper disable once PossibleInvalidOperationException var timeToWaitUntil = DateTime.Now.AddSeconds(receivable.SecondsToWait.Value); bool ShouldContinuePolling() => messagesReceived.Count < receivable.MessagesToReceive && DateTime.Now <= timeToWaitUntil; while (ShouldContinuePolling()) { messagesReceived.AddRange(GetMessages(receivable).Messages); if (ShouldContinuePolling()) { await Task.Delay(250); } } return(new ReceiveResponse { Messages = messagesReceived }); }
/// <inheritdoc /> public override async Task <ReceiveResponse> Receive( Receivable receivable, CancellationToken cancellationToken = default ) { if (receivable.SecondsToWait != null && !_options.IgnoreLongPollingException) { throw new FeatureNotYetSupportedException("AzureQueueStorageProvider: Long polling"); } var messages = await _azureQueueClient .ReceiveMessagesAsync(receivable.MessagesToReceive, cancellationToken : cancellationToken) .ConfigureAwait(false); var messagesToReturn = new List <ReceivedMessage>(); foreach (var message in messages.Value) { var deletable = new AzureQueueStorageDeletable { Queue = receivable.Queue, MessageId = message.MessageId, PopId = message.PopReceipt }; var handledMessage = await DeserialiseAndHandle(message.MessageText, deletable, cancellationToken) .ConfigureAwait(false); if (handledMessage != null) { messagesToReturn.Add(handledMessage); } } return(new ReceiveResponse { Messages = messagesToReturn }); }
/// <inheritdoc /> public async Task <ReceiveResponse> Receive(Receivable receivable, CancellationToken cancellationToken = default) { ReceivableValidator.Validate(receivable); return(await GetProvider(receivable.Queue).Receive(receivable, cancellationToken).ConfigureAwait(false)); }
/// <inheritdoc /> public override async Task <ReceiveResponse> Receive( Receivable receivable, CancellationToken cancellationToken = default ) => receivable.SecondsToWait != null ? await LongPoll(receivable) : GetMessages(receivable);