Example #1
0
        public bool DeleteMessages(List <Message> messages)
        {
            var queueUrl = GetQueueUrl(MailerQueueName);
            var entries  = new List <DeleteMessageBatchRequestEntry>();

            foreach (var message in messages)
            {
                entries.Add(new DeleteMessageBatchRequestEntry
                {
                    Id            = message.Id.ToString(),
                    ReceiptHandle = message.ReceiptHandle,
                });
            }

            var deleteMessageBatchRequest = new DeleteMessageBatchRequest
            {
                Entries  = entries,
                QueueUrl = queueUrl
            };

            var deleteMessageBatchResponse = _sqsClient.DeleteMessageBatch(deleteMessageBatchRequest);

            // TODO: Handle failures.

            return(deleteMessageBatchResponse.HttpStatusCode.Equals(HttpStatusCode.OK));
        }
        /// <summary>
        /// Deletes all messages from the input queue
        /// </summary>
        public void Purge()
        {
            if (Address == null)
            {
                return;
            }

            _log.Info("Purging {0} (by receiving all messages from the queue)", Address);

            try
            {
                using (var client = new AmazonSQSClient(_accessKeyId, _secretAccessKey, _amazonSqsConfig))
                {
                    var stopwatch = Stopwatch.StartNew();

                    while (true)
                    {
                        var response = client.ReceiveMessage(new ReceiveMessageRequest(_queueUrl)
                        {
                            MaxNumberOfMessages = 10
                        });

                        if (!response.Messages.Any())
                        {
                            break;
                        }

                        var deleteResponse = client.DeleteMessageBatch(_queueUrl, response.Messages
                                                                       .Select(m => new DeleteMessageBatchRequestEntry(m.MessageId, m.ReceiptHandle))
                                                                       .ToList());

                        if (deleteResponse.Failed.Any())
                        {
                            var errors = string.Join(Environment.NewLine,
                                                     deleteResponse.Failed.Select(f => $"{f.Message} ({f.Id})"));

                            throw new RebusApplicationException(
                                      $@"Error {deleteResponse.HttpStatusCode} while purging: 
{errors}");
                        }
                    }

                    _log.Info($"Purging {Address} took {stopwatch.Elapsed.TotalSeconds:0.0} s");
                }
            }
            catch (AmazonSQSException exception) when(exception.StatusCode == HttpStatusCode.BadRequest)
            {
                if (exception.Message.Contains("queue does not exist"))
                {
                    return;
                }

                throw;
            }
            catch (Exception exception)
            {
                throw new RebusApplicationException(exception, $"Error while purging {Address}");
            }
        }
Example #3
0
        private IEnumerable <T> YieldFromQueue()
        {
            var request = new ReceiveMessageRequest()
                          .WithMaxNumberOfMessages(10)
                          .WithQueueUrl(QueueUrl);

            ReceiveMessageResponse response = null;

            try
            {
                response = _client.ReceiveMessage(request);
            }
            catch
            {
            }
            if (response == null || !response.IsSetReceiveMessageResult())
            {
                yield break;
            }

            var messages = response.ReceiveMessageResult.Message;
            var toDelete = new List <DeleteMessageBatchRequestEntry>();

            foreach (var message in messages)
            {
                T deserialized = null;
                try
                {
                    var stream = new MemoryStream(Encoding.UTF8.GetBytes(message.Body));
                    deserialized = _serializer.DeserializeFromStream <T>(stream);
                    toDelete.Add(new DeleteMessageBatchRequestEntry()
                                 .WithId(message.MessageId)
                                 .WithReceiptHandle(message.ReceiptHandle)
                                 );
                }
                catch
                {
                }
                yield return(deserialized);
            }

            if (toDelete.Count == 0)
            {
                yield break;
            }

            var batch = new DeleteMessageBatchRequest()
                        .WithEntries(toDelete.ToArray())
                        .WithQueueUrl(QueueUrl);

            _client.DeleteMessageBatch(batch);
        }
        public void DeleteFromQueue(List <Order> orders)
        {
            foreach (var orderBatches in orders.Partition(MaxPerRequest))
            {
                var entries = new List <DeleteMessageBatchRequestEntry>();
                foreach (var order in orderBatches)
                {
                    entries.Add(new DeleteMessageBatchRequestEntry {
                        Id = Guid.NewGuid().ToString(), ReceiptHandle = order.ReceiptHandle
                    });
                }

                var dmr = new DeleteMessageBatchRequest();
                dmr.Entries  = entries;
                dmr.QueueUrl = _queueUrl;
                _sqsClient.DeleteMessageBatch(dmr);
            }
        }
Example #5
0
        /// <summary>
        /// Delete messages from named queue.
        /// </summary>
        /// <param name="queueName">Queue name.</param>
        /// <param name="messages">Enumeration of messages to delete.</param>
        /// <returns>Enumeration of messages that failed to delete.</returns>
        public IEnumerable <SqsMessageId> DeleteMessages(SqsQueueName queueName, IEnumerable <SqsMessage> messages)
        {
            if (messages.Count() > SqsUtils.MAX_NUMBER_OF_BATCH_DELETE_MESSAGES)
            {
                throw new ArgumentException(string.Format("messageReceipts is larger than {0}, which is the maximum", SqsUtils.MAX_NUMBER_OF_BATCH_DELETE_MESSAGES));
            }
            var deleteEntries = messages.Distinct(message => message.MessageId.Value).Select(message => new DeleteMessageBatchRequestEntry {
                Id = message.MessageId.Value, ReceiptHandle = message.MessageReceipt.Value
            }).ToList();
            var response = Invoke(() =>
                                  _client.DeleteMessageBatch(new DeleteMessageBatchRequest {
                QueueUrl = GetQueueUrl(queueName.Value),
                Entries  = deleteEntries
            }),
                                  queueName,
                                  BATCH_DELETING_MESSAGES);

            AssertSuccessfulStatusCode(response.HttpStatusCode, queueName, BATCH_DELETING_MESSAGES);
            return(response.Failed.Select(failed => new SqsMessageId(failed.Id)).ToArray());
        }
Example #6
0
        /// <summary>
        /// Deletes all messages from the input queue
        /// </summary>
        public void Purge()
        {
            if (Address == null)
            {
                return;
            }

            using (var client = new AmazonSQSClient(_accessKeyId, _secretAccessKey, RegionEndpoint.EUCentral1))
            {
                try
                {
                    var response = client.ReceiveMessage(new ReceiveMessageRequest(_queueUrl)
                    {
                        MaxNumberOfMessages = 10
                    });

                    while (response.Messages.Any())
                    {
                        var deleteResponse = client.DeleteMessageBatch(_queueUrl, response.Messages
                                                                       .Select(m => new DeleteMessageBatchRequestEntry(m.MessageId, m.ReceiptHandle))
                                                                       .ToList());

                        if (!deleteResponse.Failed.Any())
                        {
                            response = client.ReceiveMessage(new ReceiveMessageRequest(_queueUrl)
                            {
                                MaxNumberOfMessages = 10
                            });
                        }
                        else
                        {
                            throw new Exception(deleteResponse.HttpStatusCode.ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("error in purge: " + ex.Message);
                }
            }
        }
Example #7
0
        public DeleteMessagesResponseModel <string> DeleteMessages(IEnumerable <string> receiptHandles)
        {
            List <DeleteMessageBatchRequestEntry> deleteMessageBatchEntries = new List <DeleteMessageBatchRequestEntry>();
            int i = 0;

            foreach (string receiptHandle in receiptHandles)
            {
                i++;
                deleteMessageBatchEntries.Add(new DeleteMessageBatchRequestEntry()
                {
                    Id = i.ToString(), ReceiptHandle = receiptHandle
                });
            }

            DeleteMessageBatchRequest deleteMessageBatchRequest = new DeleteMessageBatchRequest(this.queueUrl, deleteMessageBatchEntries);

            DeleteMessageBatchResponse deleteMessageBatchResponse = sqsClient.DeleteMessageBatch(deleteMessageBatchRequest);

            DeleteMessagesResponseModel <string> deleteMessagesResponseModel = ConstructDeleteBatchResponseModel(deleteMessageBatchEntries, deleteMessageBatchResponse);

            return(deleteMessagesResponseModel);
        }
Example #8
0
        public static void SQSDeleteMessageBatch()
        {
            #region SQSDeleteMessageBatch
            var client = new AmazonSQSClient();

            var request = new ReceiveMessageRequest
            {
                AttributeNames = new List <string>()
                {
                    "All"
                },
                MaxNumberOfMessages = 5,
                QueueUrl            = "https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyTestQueue",
                VisibilityTimeout   = (int)TimeSpan.FromMinutes(10).TotalSeconds,
                WaitTimeSeconds     = (int)TimeSpan.FromSeconds(5).TotalSeconds
            };

            var response     = client.ReceiveMessage(request);
            var batchEntries = new List <DeleteMessageBatchRequestEntry>();

            if (response.Messages.Count > 0)
            {
                foreach (var message in response.Messages)
                {
                    var batchEntry = new DeleteMessageBatchRequestEntry
                    {
                        Id            = message.MessageId,
                        ReceiptHandle = message.ReceiptHandle
                    };

                    batchEntries.Add(batchEntry);
                }

                var delRequest = new DeleteMessageBatchRequest
                {
                    Entries  = batchEntries,
                    QueueUrl = "https://sqs.us-east-1.amazonaws.com/80398EXAMPLE/MyTestQueue"
                };

                var delResponse = client.DeleteMessageBatch(delRequest);

                if (delResponse.Failed.Count > 0)
                {
                    Console.WriteLine("Failed deletions:");

                    foreach (var failure in delResponse.Failed)
                    {
                        Console.WriteLine("  For ID '" + failure.Id + "': ");
                        Console.WriteLine("    Code = " + failure.Code);
                        Console.WriteLine("    Message = " + failure.Message);
                        Console.WriteLine("    Sender's fault? = " + failure.SenderFault);
                    }
                }

                if (delResponse.Successful.Count > 0)
                {
                    Console.WriteLine("Successful deletions:");

                    foreach (var success in delResponse.Successful)
                    {
                        Console.WriteLine("  ID '" + success.Id + "'");
                    }
                }
            }
            else
            {
                Console.WriteLine("No messages to delete.");
            }
            #endregion

            Console.ReadLine();
        }