Esempio n. 1
0
        async Task SendOutgoingMessages(ConcurrentQueue <OutgoingMessage> outgoingMessages, ITransactionContext context)
        {
            if (!outgoingMessages.Any())
            {
                return;
            }

            var client = GetClientFromTransactionContext(context);

            var messagesByDestination = outgoingMessages
                                        .GroupBy(m => m.DestinationAddress)
                                        .ToList();

            await Task.WhenAll(
                messagesByDestination
                .Select(async batch =>
            {
                var entries = batch
                              .Select(message =>
                {
                    var transportMessage = message.TransportMessage;
                    var headers          = transportMessage.Headers;
                    var messageId        = headers[Headers.MessageId];

                    var sqsMessage   = new AmazonSqsTransportMessage(transportMessage.Headers, GetBody(transportMessage.Body));
                    var delaySeconds = GetDelaySeconds(headers);

                    var entry = new SendMessageBatchRequestEntry(messageId, _serializer.Serialize(sqsMessage));

                    if (delaySeconds != null)
                    {
                        entry.DelaySeconds = delaySeconds.Value;
                    }

                    return(entry);
                })
                              .ToList();

                var destinationUrl = GetDestinationQueueUrlByName(batch.Key, context);
                var request        = new SendMessageBatchRequest(destinationUrl, entries);
                var response       = await client.SendMessageBatchAsync(request);

                if (response.Failed.Any())
                {
                    var failed = response.Failed.Select(f => new AmazonSQSException($"Failed {f.Message} with Id={f.Id}, Code={f.Code}, SenderFault={f.SenderFault}"));

                    throw new AggregateException(failed);
                }
            })
                );
        }
Esempio n. 2
0
 public string Serialize(AmazonSqsTransportMessage message)
 {
     return(JsonConvert.SerializeObject(message));
 }