DeleteMessageBatch() public method

Deletes up to ten messages from the specified queue. This is a batch version of DeleteMessage . The result of the action on each message is reported individually in the response.

Because the batch request can result in a combination of successful and unsuccessful actions, you should check for batch errors even when the call returns an HTTP status code of 200.

Some actions take lists of parameters. These lists are specified using the param.n notation. Values of n are integers starting from 1. For example, a parameter list with two elements looks like this:

&Attribute.1=this

&Attribute.2=that

/// Two or more batch entries in the request have the same Id. /// /// The batch request doesn't contain any entries. /// /// The Id of a batch entry in a batch request doesn't abide by the specification. /// /// The batch request contains more entries than permissible. ///
public DeleteMessageBatch ( DeleteMessageBatchRequest request ) : DeleteMessageBatchResponse
request Amazon.SQS.Model.DeleteMessageBatchRequest Container for the necessary parameters to execute the DeleteMessageBatch service method.
return Amazon.SQS.Model.DeleteMessageBatchResponse
Ejemplo n.º 1
0
        /// <summary>
        /// Deletes all messages from the input queue
        /// </summary>
        public void Purge()
        {
            if (_inputQueueAddress == 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);
                }

            }





        }
Ejemplo n.º 2
0
 /// <summary>
 /// Task for deleting messages from SQS
 /// </summary>
 /// <param name="state"></param>
 private void DeleteTask(CancellationToken ct, List<Message> messages, Tuple<int, int> range)
 {
     try
     {
         using (AmazonSQSClient client = new AmazonSQSClient(
                 this.AWSAccessKey, this.AWSSecretAccessKey,
                 new AmazonSQSConfig()
                     {
                         ServiceURL = this.AWSSQSServiceUrl,
                         MaxErrorRetry = 10
                     }))
         {
             DeleteMessageBatchRequest deleteRequest = new DeleteMessageBatchRequest();
             deleteRequest.QueueUrl = this.QueueUrl;
             for (int i = range.Item1; i < range.Item2; i++)
             {
                 deleteRequest.Entries.Add(new DeleteMessageBatchRequestEntry()
                 {
                     Id = messages[i].MessageId,
                     ReceiptHandle = messages[i].ReceiptHandle
                 });
                 // send 10x or the remainder of the set
                 if ((deleteRequest.Entries.Count == 10) |
                     (i == range.Item2 - 1))
                 {
                     if (ct.IsCancellationRequested)
                     {
                         break;
                     }
                     client.DeleteMessageBatch(deleteRequest);
                     deleteRequest.Entries.Clear();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         EventLog.WriteEntry(this.GetType().Name,
             ex.Message + "\n" + ex.StackTrace,
             EventLogEntryType.Error);
     }
 }
Ejemplo n.º 3
0
        private void execute()
        {
            FolderVaultMapping mapping = Context.Mapping;
            ReceiveMessageRequest req = new ReceiveMessageRequest() { QueueUrl = mapping.NotificationQueueURL, MaxNumberOfMessages = 10 };
            List<QueueMessage> messages = new List<QueueMessage>();
            DeleteMessageBatchRequest deleter;
            ReceiveMessageResult result;
            List<Message> resultMessages;
            HashSet<string> exceptions = new HashSet<string>();
            long id = 0;

            using (AmazonSQSClient client = new AmazonSQSClient(mapping.AccessKey, mapping.SecretKey, mapping.Endpoint))
            {
                while (KeepRunning())
                {
                    try
                    {
                        messages.Clear();
                        result = client.ReceiveMessage(req).ReceiveMessageResult;
                        resultMessages = new List<Message>(result.Message);

                        for(int i = 0; i < resultMessages.Count; i++)
                        {
                            if (exceptions.Contains(resultMessages[i].MessageId))
                            {
                                resultMessages.RemoveAt(i);
                                i--;
                            }
                        }

                        if (resultMessages.Count > 0)
                        {
                            foreach (Message m in resultMessages)
                            {
                                try
                                {
                                    messages.Add(new QueueMessage(m.ReceiptHandle, m.MessageId, m.Body));
                                }
                                catch (InvalidDataException ex)
                                {
                                    Console.Error.WriteLine("Received an error parsing " + m.MessageId);
                                    Console.Error.WriteLine(ex);
                                    exceptions.Add(m.MessageId);
                                }
                            }

                            try
                            {
                                deleter = new DeleteMessageBatchRequest() { QueueUrl = mapping.NotificationQueueURL };

                                foreach (QueueMessage message in messages)
                                {
                                    try
                                    {
                                        Console.WriteLine("Handling a " + message.Action + " message with ID " + message.MessageId);
                                        if (message.Action == "ResultNotice" || handleMessage(message))
                                        {
                                            deleter.Entries.Add(new DeleteMessageBatchRequestEntry() { Id = "Delete" + (id++), ReceiptHandle = message.ReceiptHandle });
                                        }
                                        else
                                        {
                                            Console.WriteLine("    Unknown action");
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.Error.WriteLine(ex);
                                        exceptions.Add(message.MessageId);
                                    }
                                }

                                if (deleter.Entries.Count > 0)
                                {
                                    client.DeleteMessageBatch(deleter);
                                }
                            }
                            catch(Exception ex)
                            {
                                Console.Error.WriteLine(ex);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < Settings.Default.PollInterval.TotalSeconds && KeepRunning(); i++)
                            {
                                Thread.Sleep(1000);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine(ex);
                    }
                }
            }

            activePollers.Remove(this);
        }
Ejemplo n.º 4
0
        /// <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}");
            }
        }
Ejemplo n.º 5
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();
    }