Example #1
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, SendMessageBatchResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("BatchResultErrorEntry", targetDepth))
                    {
                        var unmarshaller = BatchResultErrorEntryUnmarshaller.Instance;
                        var item         = unmarshaller.Unmarshall(context);
                        response.Failed.Add(item);
                        continue;
                    }
                    if (context.TestExpression("SendMessageBatchResultEntry", targetDepth))
                    {
                        var unmarshaller = SendMessageBatchResultEntryUnmarshaller.Instance;
                        var item         = unmarshaller.Unmarshall(context);
                        response.Successful.Add(item);
                        continue;
                    }
                }
            }

            return;
        }
        /// <summary>
        /// Validate the Message
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        public static void ValidateSendMessageBatch(SendMessageBatchRequest request, SendMessageBatchResponse response)
        {
            if (response != null && response.Successful != null && response.Successful.Count > 0)
            {
                Dictionary <string, SendMessageBatchRequestEntry> requestMessages = request.Entries.ToDictionary(entry => entry.Id, StringComparer.Ordinal);

                List <SendMessageBatchResultEntry> resultEntries = response.Successful;
                foreach (SendMessageBatchResultEntry entry in resultEntries)
                {
                    var    message = requestMessages[entry.Id];
                    string id      = entry.MessageId;

                    var body    = message.MessageBody;
                    var bodyMd5 = entry.MD5OfMessageBody;
                    ValidateMD5(body, id, bodyMd5);

                    var attributes    = message.MessageAttributes;
                    var attributesMd5 = entry.MD5OfMessageAttributes;
                    if (attributes != null && attributes.Count > 0 && !string.IsNullOrEmpty(attributesMd5))
                    {
                        ValidateMD5(attributes, id, attributesMd5);
                    }
                }
            }
        }
        /// <summary>
        /// Custom pipeline handler
        /// </summary>
        /// <param name="executionContext"></param>
        protected virtual void PostInvoke(IExecutionContext executionContext)
        {
            var request  = executionContext.RequestContext.Request;
            var response = executionContext.ResponseContext.Response;
            ReceiveMessageResponse receiveMessageResponse = response as ReceiveMessageResponse;

            if (receiveMessageResponse != null)
            {
                ValidateReceiveMessage(receiveMessageResponse);
            }

            SendMessageResponse sendMessageResponse = response as SendMessageResponse;
            SendMessageRequest  sendMessageRequest  = request.OriginalRequest as SendMessageRequest;

            if (sendMessageRequest != null && sendMessageResponse != null)
            {
                ValidateSendMessage(sendMessageRequest, sendMessageResponse);
            }

            SendMessageBatchRequest  sendMessageBatchRequest  = request.OriginalRequest as SendMessageBatchRequest;
            SendMessageBatchResponse sendMessageBatchResponse = response as SendMessageBatchResponse;

            if (sendMessageBatchRequest != null && sendMessageBatchResponse != null)
            {
                ValidateSendMessageBatch(sendMessageBatchRequest, sendMessageBatchResponse);
            }
        }
Example #4
0
        private Task <SendMessageBatchResponse> SendSQSBatchMessageWithBadResult(SendMessageBatchRequest sendMessageBatchRequest, CancellationToken cancellationToken = default)
        {
            var response = new SendMessageBatchResponse();

            response.HttpStatusCode = HttpStatusCode.BadRequest;
            return(Task.FromResult(response));
        }
Example #5
0
        /// <summary>
        /// Creates a BatchResponse from an AWS SendMessageBatchResponse type
        ///
        /// This method creates an array of SendResults that mimics the original order of messages sent in
        /// </summary>
        /// <param name="batchResponse"></param>
        /// <param name="messages"></param>
        /// <param name="messageBatch"></param>
        /// <returns></returns>
        public static BatchResponse FromAwsBatchResponse(SendMessageBatchResponse batchResponse, BatchMessageRequest[] messageBatch)
        {
            var result = new BatchResponse
            {
                SendResults = new SendResult[batchResponse.Successful.Count + batchResponse.Failed.Count]
            };

            for (var messageId = 0; messageId < messageBatch.Length; messageId++)
            {
                var successSendResult = batchResponse.Successful.Where(x => x.Id == messageId.ToString()).Select(x => new SendResult
                {
                    Success        = true,
                    MessageId      = x.MessageId,
                    MessageRequest = messageBatch[messageId],
                    Error          = null
                }).FirstOrDefault();

                var failedSendResult = batchResponse.Failed.Where(x => x.Id == messageId.ToString()).Select(x => new SendResult
                {
                    Success        = false,
                    MessageId      = null,
                    MessageRequest = messageBatch[messageId],
                    Error          = x.Message,
                }).FirstOrDefault();

                result.SendResults[messageId] = successSendResult?.MessageId != null ? successSendResult : failedSendResult;
            }

            result.Success = !batchResponse.Failed.Any();
            return(result);
        }
        protected override void ProcessResponseHandlers(AmazonWebServiceResponse response, IRequest request, IWebResponseData webResponseData)
        {
            base.ProcessResponseHandlers(response, request, webResponseData);

            ReceiveMessageResponse receiveMessageResponse = response as ReceiveMessageResponse;

            if (receiveMessageResponse != null)
            {
                ValidateReceiveMessage(receiveMessageResponse);
            }

            SendMessageResponse sendMessageResponse = response as SendMessageResponse;
            SendMessageRequest  sendMessageRequest  = request.OriginalRequest as SendMessageRequest;

            if (sendMessageRequest != null && sendMessageResponse != null)
            {
                ValidateSendMessage(sendMessageRequest, sendMessageResponse);
            }

            SendMessageBatchRequest  sendMessageBatchRequest  = request.OriginalRequest as SendMessageBatchRequest;
            SendMessageBatchResponse sendMessageBatchResponse = response as SendMessageBatchResponse;

            if (sendMessageBatchRequest != null && sendMessageBatchResponse != null)
            {
                ValidateSendMessageBatch(sendMessageBatchRequest, sendMessageBatchResponse);
            }
        }
Example #7
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            SendMessageBatchResponse response = new SendMessageBatchResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("SendMessageBatchResult", 2))
                    {
                        UnmarshallResult(context, response);
                        continue;
                    }

                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return(response);
        }
Example #8
0
        // snippet-end:[sqs.dotnetv3.SQSSendMessagesExample.SendMessage]


        // snippet-start:[sqs.dotnetv3.SQSSendMessagesExample.SendMessageBatch]
        /// <summary>
        /// Method to put a batch of messages on an SQS queue.
        /// Could be expanded to include message attributes, etc.,
        /// in the SendMessageBatchRequestEntry objects.
        /// </summary>
        /// <param name="sqsClient">The SQS client, created in Main.</param>
        /// <param name="qUrl">The URL of the queue, given as a command line argument.</param>
        /// <param name="messages">The messages to send to the queue.</param>
        private static async Task SendMessageBatch(
            IAmazonSQS sqsClient, string qUrl, List <SendMessageBatchRequestEntry> messages)
        {
            Console.WriteLine($"\nSending a batch of messages to queue\n  {qUrl}");
            SendMessageBatchResponse responseSendBatch =
                await sqsClient.SendMessageBatchAsync(qUrl, messages);

            // Could test responseSendBatch.Failed here
            foreach (SendMessageBatchResultEntry entry in responseSendBatch.Successful)
            {
                Console.WriteLine($"Message {entry.Id} successfully queued.");
            }
        }
        public async Task CanSendMessagesFifo()
        {
            var publisher = new PersonPublisher(sqsClient.Object, "sqs.com.fifo");
            var elvis     = new PersonMessage {
                Id = Guid.NewGuid(), Name = "Elvis"
            };
            var response = new SendMessageBatchResponse();

            sqsClient.Setup(x => x.SendMessageBatchAsync(It.IsNotNull <SendMessageBatchRequest>(), It.IsNotNull <CancellationToken>()))
            .ReturnsAsync(response);

            await publisher.SendMessagesAsync(new[] { elvis });
        }
        public async Task PutMessagesAsync(IReadOnlyCollection <QueueMessage> messages, CancellationToken cancellationToken = default)
        {
            if (messages == null)
            {
                return;
            }

            // SQS request size is limited
            foreach (IEnumerable <QueueMessage> chunk in messages.Chunk(MaxEntriesPerRequest))
            {
                var request = new SendMessageBatchRequest(
                    _queueUrl,
                    chunk.Select(Converter.ToSQSMessage).ToList());

                SendMessageBatchResponse r = await _client.SendMessageBatchAsync(request, cancellationToken);
            }
        }
Example #11
0
        /// <summary>
        /// Puts a batch of messages on the queue
        ///
        /// Supports a maximum of 10 messages.
        /// </summary>
        /// <param name="batchMessages">An array of messages to be put on the queue</param>
        /// <param name="queueName">The name of the queue</param>
        /// <returns></returns>
        public async Task <IBatchResponse> SendMessageBatchAsync(BatchMessageRequest[] batchMessages, string queueName)
        {
            if (batchMessages.Length > 10)
            {
                throw new ArgumentException($"AWS SQS supports a max message number of 10 messages. {batchMessages.Length} were received.", nameof(batchMessages));
            }

            var queueUrl = await GetQueueUrlAsync(queueName);

            var sendMessageBatchRequest = new SendMessageBatchRequest
            {
                Entries = batchMessages.Select((v, i) => new SendMessageBatchRequestEntry(i.ToString(), v.Message)
                {
                    MessageAttributes = v.MessageAttributes.ToDictionary(attribute => attribute.Key, attribute => new MessageAttributeValue {
                        StringValue = attribute.Value, DataType = "String"
                    }),
                    MessageGroupId = queueName.EndsWith(".fifo") ? queueUrl : null
                }).ToList(),
                QueueUrl = queueUrl
            };

            var retryCounter = 0;
            SendMessageBatchResponse awsBatchResponse = null;

            while (awsBatchResponse == null)
            {
                try
                {
                    awsBatchResponse = await _client.SendMessageBatchAsync(sendMessageBatchRequest);
                }
                catch (AmazonSQSException e)
                {
                    if (e.Message.EndsWith("Throttled") && retryCounter < 10)
                    {
                        retryCounter += 1;
                        await Task.Delay(retryCounter * 3);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(BatchResponse.FromAwsBatchResponse(awsBatchResponse, batchMessages));
        }
Example #12
0
        public static void ValidateSendMessageBatch(SendMessageBatchRequest request, SendMessageBatchResponse response)
        {
            if (response != null && response.SendMessageBatchResult != null &&
                response.SendMessageBatchResult.SendMessageBatchResultEntry != null && response.SendMessageBatchResult.SendMessageBatchResultEntry.Count > 0)
            {
                Dictionary <string, string> requestMessages = request.Entries.ToDictionary(entry => entry.Id, entry => entry.MessageBody, StringComparer.Ordinal);

                List <SendMessageBatchResultEntry> resultEntries = response.SendMessageBatchResult.SendMessageBatchResultEntry;
                foreach (SendMessageBatchResultEntry entry in resultEntries)
                {
                    string body = requestMessages[entry.Id];
                    string md5  = entry.MD5OfMessageBody;
                    string id   = entry.MessageId;

                    AmazonSQSUtil.ValidateMD5(body, id, md5);
                }
            }
        }
Example #13
0
        public SendMessageResponseModel <T> Save(IEnumerable <T> messages)
        {
            List <SendMessageBatchRequestEntry> sendMessageBatchRequestEntries = new List <SendMessageBatchRequestEntry>();
            int i = 0;

            foreach (T message in messages)
            {
                i++;
                SendMessageBatchRequestEntry sendMessageBatchRequestEntry = new SendMessageBatchRequestEntry(i.ToString(), JsonConvert.SerializeObject(message));
                sendMessageBatchRequestEntries.Add(sendMessageBatchRequestEntry);
            }

            SendMessageBatchRequest  sendMessageBatchRequest  = new SendMessageBatchRequest(this.queueUrl, sendMessageBatchRequestEntries);
            SendMessageBatchResponse sendMessageBatchResponse = this.sqsClient.SendMessageBatch(sendMessageBatchRequest);

            SendMessageResponseModel <T> sendMessageBatchResponseModel = ConstructSendResponseModel <T>(sendMessageBatchRequestEntries, sendMessageBatchResponse);

            return(sendMessageBatchResponseModel);
        }
Example #14
0
        private static async Task SendBatch(SendMessageBatchRequest sendMessageBatchRequest)
        {
            SendMessageBatchResponse response = await client.SendMessageBatchAsync(sendMessageBatchRequest);

            Console.WriteLine("Messages successfully sent:");
            foreach (var success in response.Successful)
            {
                Console.WriteLine("    Message id : {0}", success.MessageId);
                Console.WriteLine("    Message content MD5 : {0}", success.MD5OfMessageBody);
            }

            Console.WriteLine("Messages failed to send:");
            foreach (var failed in response.Failed)
            {
                Console.WriteLine("    Message id : {0}", failed.Id);
                Console.WriteLine("    Message content : {0}", failed.Message);
                Console.WriteLine("    Sender's fault? : {0}", failed.SenderFault);
            }
        }
        public override List <SqsSendMessageResponse> Map(SendMessageBatchResponse messageResponse)
        {
            Source      = messageResponse;
            Destination = new List <SqsSendMessageResponse>();

            foreach (var message in messageResponse.Successful)
            {
                Destination.Add(new SqsSendMessageResponse {
                    MessageId = message.MessageId, HttpStatusCode = HttpStatusCode.OK
                });
            }

            foreach (var message in messageResponse.Failed)
            {
                Destination.Add(new SqsSendMessageResponse {
                    MessageId = message.Id, HttpStatusCode = HttpStatusCode.BadRequest
                });
            }

            return(Destination);
        }
        public void SendMessageBatchResponseParse()
        {
            var xmlText = $@"
<SendMessageBatchResponse xmlns=""{SqsClient.NS}"">
	<SendMessageBatchResult>
		<SendMessageBatchResultEntry>
			<Id>test_msg_001</Id>
			<MessageId>0a5231c7-8bff-4955-be2e-8dc7c50a25fa</MessageId>
			<MD5OfMessageBody>0e024d309850c78cba5eabbeff7cae71</MD5OfMessageBody>
		</SendMessageBatchResultEntry>
		<SendMessageBatchResultEntry>
			<Id>test_msg_002</Id>
			<MessageId>15ee1ed3-87e7-40c1-bdaa-2e49968ea7e9</MessageId>
			<MD5OfMessageBody>7fb8146a82f95e0af155278f406862c2</MD5OfMessageBody>
		</SendMessageBatchResultEntry>
	</SendMessageBatchResult>

	<ResponseMetadata>
		<RequestId>ca1ad5d0-8271-408b-8d0f-1351bf547e74</RequestId>
	</ResponseMetadata>
</SendMessageBatchResponse>".Trim();

            var response = SendMessageBatchResponse.Parse(xmlText);

            var messages = response.SendMessageBatchResult.Items;

            Assert.Equal(2, messages.Length);

            var message_0 = messages[0];

            Assert.Equal("test_msg_001", message_0.Id);
            Assert.Equal("0a5231c7-8bff-4955-be2e-8dc7c50a25fa", message_0.MessageId);
            Assert.Equal("0e024d309850c78cba5eabbeff7cae71", message_0.MD5OfMessageBody);

            var message_1 = messages[1];

            Assert.Equal("test_msg_002", message_1.Id);
            Assert.Equal("15ee1ed3-87e7-40c1-bdaa-2e49968ea7e9", message_1.MessageId);
            Assert.Equal("7fb8146a82f95e0af155278f406862c2", message_1.MD5OfMessageBody);
        }
Example #17
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, SendMessageBatchResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.Read())
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("SendMessageBatchResultEntry", targetDepth))
                    {
                        response.Successful.Add(SendMessageBatchResultEntryUnmarshaller.GetInstance().Unmarshall(context));

                        continue;
                    }
                    if (context.TestExpression("BatchResultErrorEntry", targetDepth))
                    {
                        response.Failed.Add(BatchResultErrorEntryUnmarshaller.GetInstance().Unmarshall(context));

                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }



            return;
        }
        public override T Unmarshall <T>(IResponseContext context)
        {
            try
            {
                var response = new SendMessageBatchResponse();
                ResultUnmarshall(context, response);

                var xmlRootNode = GetXmlElement(context.ContentStream);

                if (TryGetXmlElements(xmlRootNode, "SendMessageBatchResult/BatchResultErrorEntry", out var batchErrorList))
                {
                    BatchResultErrorEntryUnmarshaller(batchErrorList, response.BatchResultErrorEntry);
                }

                if (TryGetXmlElements(xmlRootNode, "SendMessageBatchResult/SendMessageBatchResultEntry", out var batchResultList))
                {
                    foreach (XmlNode item in batchResultList)
                    {
                        var resultEntry = new SendMessageBatchResultEntry();
                        resultEntry.Id = item.SelectSingleNode("Id")?.InnerText;
                        resultEntry.MD5OfMessageAttributes = item.SelectSingleNode("MD5OfMessageAttributes")?.InnerText;
                        resultEntry.MD5OfMessageBody       = item.SelectSingleNode("MD5OfMessageBody")?.InnerText;
                        resultEntry.MessageId      = item.SelectSingleNode("MessageId")?.InnerText;
                        resultEntry.SequenceNumber = item.SelectSingleNode("SequenceNumber")?.InnerText;
                        response.SendMessageBatchResultEntry.Add(resultEntry);
                    }
                }
                response.ResponseMetadata.RequestId = xmlRootNode.SelectSingleNode("ResponseMetadata/RequestId")?.InnerText;

                return(response as T);
            }
            catch (Exception ex)
            {
                throw ErrorUnmarshall(ex.Message, ex, context.StatusCode);
            }
        }
Example #19
0
        async Task DeleteDelayedMessagesThatWereDeliveredSuccessfullyAsBatchesInBatches(BatchEntry <SqsReceivedDelayedMessage> batch, SendMessageBatchResponse result, int batchNumber, int totalBatches)
        {
            List <DeleteMessageBatchRequestEntry> deleteBatchRequestEntries = null;

            foreach (var successful in result.Successful)
            {
                deleteBatchRequestEntries = deleteBatchRequestEntries ?? new List <DeleteMessageBatchRequestEntry>(result.Successful.Count);
                var preparedMessage = batch.PreparedMessagesBydId[successful.Id];
                // need to reuse the previous batch entry ID so that we can map again in failure scenarios, this is fine given that IDs only need to be unique per request
                deleteBatchRequestEntries.Add(new DeleteMessageBatchRequestEntry(successful.Id, preparedMessage.ReceiptHandle));
            }

            if (deleteBatchRequestEntries != null)
            {
                if (Logger.IsDebugEnabled)
                {
                    var message = batch.PreparedMessagesBydId.Values.First();

                    Logger.Debug($"Deleting delayed message for batch '{batchNumber}/{totalBatches}' with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}' for destination {message.Destination}");
                }

                var deleteResult = await sqsClient.DeleteMessageBatchAsync(new DeleteMessageBatchRequest(delayedDeliveryQueueUrl, deleteBatchRequestEntries), CancellationToken.None)
                                   .ConfigureAwait(false);

                if (Logger.IsDebugEnabled)
                {
                    var message = batch.PreparedMessagesBydId.Values.First();

                    Logger.Debug($"Deleted delayed message for batch '{batchNumber}/{totalBatches}' with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}' for destination {message.Destination}");
                }

                List <Task> deleteTasks = null;
                foreach (var errorEntry in deleteResult.Failed)
                {
                    deleteTasks = deleteTasks ?? new List <Task>(deleteResult.Failed.Count);
                    var messageToDeleteWithAnotherAttempt = batch.PreparedMessagesBydId[errorEntry.Id];
                    Logger.Info($"Retrying message deletion with MessageId {messageToDeleteWithAnotherAttempt.MessageId} that failed in batch '{batchNumber}/{totalBatches}' due to '{errorEntry.Message}'.");
                    deleteTasks.Add(DeleteMessage(messageToDeleteWithAnotherAttempt));
                }

                if (deleteTasks != null)
                {
                    await Task.WhenAll(deleteTasks).ConfigureAwait(false);
                }
            }
        }
Example #20
0
        public SendMessageBatchResponse SendMessageBatch(SendMessageBatchRequest request)
        {
            if (request.Entries == null || request.Entries.Count <= 0)
            {
                throw new EmptyBatchRequestException("No entires in request");
            }

            if (request.Entries.Count > SqsQueueDefinition.MaxBatchSendItems)
            {
                throw new TooManyEntriesInBatchRequestException($"Count of [{request.Entries.Count}] exceeds limit of [{SqsQueueDefinition.MaxBatchSendItems}]");
            }

            var q = GetQueue(request.QueueUrl);

            var response = new SendMessageBatchResponse
            {
                Failed     = new List <BatchResultErrorEntry>(),
                Successful = new List <SendMessageBatchResultEntry>()
            };

            var entryIds = new HashSet <string>();

            foreach (var entry in request.Entries)
            {
                string id = null;
                BatchResultErrorEntry batchError = null;

                try
                {
                    if (entryIds.Contains(entry.Id))
                    {
                        throw new BatchEntryIdsNotDistinctException($"Duplicate Id of [{entry.Id}]");
                    }

                    entryIds.Add(entry.Id);

                    id = q.Send(new SendMessageRequest
                    {
                        QueueUrl          = q.QueueDefinition.QueueUrl,
                        MessageAttributes = entry.MessageAttributes,
                        MessageBody       = entry.MessageBody,
                    });
                }
                catch (ReceiptHandleIsInvalidException rhex)
                {
                    batchError = new BatchResultErrorEntry
                    {
                        Id      = entry.Id,
                        Message = rhex.Message,
                        Code    = rhex.ErrorCode
                    };
                }

                if (id == null)
                {
                    var entryToQueue = batchError ?? new BatchResultErrorEntry
                    {
                        Id      = entry.Id,
                        Message = "FakeSendError",
                        Code    = "789"
                    };

                    response.Failed.Add(entryToQueue);
                }
                else
                {
                    response.Successful.Add(new SendMessageBatchResultEntry
                    {
                        Id        = entry.Id,
                        MessageId = id
                    });
                }
            }

            return(response);
        }
Example #21
0
        private SendMessageResponseModel <T> ConstructSendResponseModel <T>(List <SendMessageBatchRequestEntry> sendMessageBatchRequestEntries, SendMessageBatchResponse sendMessageBatchResponse)
        {
            SendMessageResponseModel <T> sendMessageBatchResponseModel = new SendMessageResponseModel <T>();

            sendMessageBatchResponseModel.SendMessagesSuccessfully
            .AddRange(sendMessageBatchRequestEntries.Where(entry => sendMessageBatchResponse.Successful
                                                           .Select(re => re.Id).Contains(entry.Id)).Select(entry => JsonConvert.DeserializeObject <T>(entry.MessageBody)));

            sendMessageBatchResponseModel.UnprocessedMessages
            .AddRange(sendMessageBatchRequestEntries.Where(entry => sendMessageBatchResponse.Failed
                                                           .Select(re => re.Id).Contains(entry.Id)).Select(entry => JsonConvert.DeserializeObject <T>(entry.MessageBody)));
            return(sendMessageBatchResponseModel);
        }
Example #22
0
        public async Task PutMessagesAsync(IEnumerable <QueueMessage> messages, CancellationToken cancellationToken = default)
        {
            var sqs = messages.Select(ToSQSMessage).ToList();

            SendMessageBatchResponse r = await _client.SendMessageBatchAsync(_queueName, sqs, cancellationToken);
        }
Example #23
0
        async Task ChangeVisibilityOfDelayedMessagesThatFailedBatchDeliveryInBatches(BatchEntry <SqsReceivedDelayedMessage> batch, SendMessageBatchResponse result, int batchNumber, int totalBatches)
        {
            try
            {
                List <ChangeMessageVisibilityBatchRequestEntry> changeVisibilityBatchRequestEntries = null;
                foreach (var failed in result.Failed)
                {
                    changeVisibilityBatchRequestEntries = changeVisibilityBatchRequestEntries ?? new List <ChangeMessageVisibilityBatchRequestEntry>(result.Failed.Count);
                    var preparedMessage = batch.PreparedMessagesBydId[failed.Id];
                    // need to reuse the previous batch entry ID so that we can map again in failure scenarios, this is fine given that IDs only need to be unique per request
                    changeVisibilityBatchRequestEntries.Add(new ChangeMessageVisibilityBatchRequestEntry(failed.Id, preparedMessage.ReceiptHandle)
                    {
                        VisibilityTimeout = 0
                    });
                }

                if (changeVisibilityBatchRequestEntries != null)
                {
                    if (Logger.IsDebugEnabled)
                    {
                        var message = batch.PreparedMessagesBydId.Values.First();

                        Logger.Debug($"Changing delayed message visibility for batch '{batchNumber}/{totalBatches}' with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}' for destination {message.Destination}");
                    }

                    var changeVisibilityResult = await sqsClient.ChangeMessageVisibilityBatchAsync(new ChangeMessageVisibilityBatchRequest(delayedDeliveryQueueUrl, changeVisibilityBatchRequestEntries), CancellationToken.None)
                                                 .ConfigureAwait(false);

                    if (Logger.IsDebugEnabled)
                    {
                        var message = batch.PreparedMessagesBydId.Values.First();

                        Logger.Debug($"Changed delayed message visibility for batch '{batchNumber}/{totalBatches}' with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}' for destination {message.Destination}");
                    }

                    if (Logger.IsDebugEnabled && changeVisibilityResult.Failed.Count > 0)
                    {
                        var builder = new StringBuilder();
                        foreach (var failed in changeVisibilityResult.Failed)
                        {
                            builder.AppendLine($"{failed.Id}: {failed.Message} | {failed.Code} | {failed.SenderFault}");
                        }

                        Logger.Debug($"Changing visibility failed for {builder}");
                    }
                }
            }
            catch (Exception e)
            {
                var builder = new StringBuilder();
                foreach (var failed in result.Failed)
                {
                    builder.AppendLine($"{failed.Id}: {failed.Message} | {failed.Code} | {failed.SenderFault}");
                }

                Logger.Error($"Changing visibility failed for {builder}", e);
            }
        }