public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context) { DeleteMessageBatchResponse response = new DeleteMessageBatchResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.IsStartElement) { if (context.TestExpression("DeleteMessageBatchResult", 2)) { UnmarshallResult(context, response); continue; } if (context.TestExpression("ResponseMetadata", 2)) { response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context); } } } return(response); }
private static void UnmarshallResult(XmlUnmarshallerContext context, DeleteMessageBatchResponse 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("DeleteMessageBatchResultEntry", targetDepth)) { var unmarshaller = DeleteMessageBatchResultEntryUnmarshaller.Instance; var item = unmarshaller.Unmarshall(context); response.Successful.Add(item); continue; } } } return; }
public override T Unmarshall <T>(IResponseContext context) { try { var response = new DeleteMessageBatchResponse(); ResultUnmarshall(context, response); var xmlRootNode = GetXmlElement(context.ContentStream); if (TryGetXmlElements(xmlRootNode, "DeleteMessageBatchResult/BatchResultErrorEntry", out var batchErrorList)) { BatchResultErrorEntryUnmarshaller(batchErrorList, response.BatchResultErrorEntry); } if (TryGetXmlElements(xmlRootNode, "DeleteMessageBatchResult/DeleteMessageBatchResultEntry", out var batchResultList)) { DeleteMessageBatchResultEntryUnmarshaller(batchResultList, response.DeleteMessageBatchResultEntry); } response.ResponseMetadata.RequestId = xmlRootNode.SelectSingleNode("ResponseMetadata/RequestId")?.InnerText; return(response as T); } catch (Exception ex) { throw ErrorUnmarshall(ex.Message, ex, context.StatusCode); } }
private static async Task <List <Message> > ReadAmazonSqsEvent(string queueUrl, int waitTimeoutSeconds) { using (AmazonSQSClient sqsClient = new AmazonSQSClient()) { ReceiveMessageResponse receiveMessageResponse = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest(queueUrl) { WaitTimeSeconds = waitTimeoutSeconds, MessageAttributeNames = new List <string> { "All" } }); List <Message> messages = receiveMessageResponse.Messages.Select(GetMessage).ToList(); if (receiveMessageResponse.Messages.Any()) { List <DeleteMessageBatchRequestEntry> deleteMessageBatchRequestEntries = receiveMessageResponse .Messages .Select(_ => new DeleteMessageBatchRequestEntry(_.MessageId, _.ReceiptHandle)).ToList(); DeleteMessageBatchRequest deleteMessageBatchRequest = new DeleteMessageBatchRequest(queueUrl, deleteMessageBatchRequestEntries); DeleteMessageBatchResponse response = await sqsClient.DeleteMessageBatchAsync(deleteMessageBatchRequest); } return(messages); } }
private static void BatchRequestCompletedDiagnostics(DeleteMessageBatchResponse response, Stopwatch stopwatch) { if (_diagnostics.IsEnabled(DiagnosticEvents.DeleteBatchRequestComplete)) { _diagnostics.Write(DiagnosticEvents.DeleteBatchRequestComplete, new EndDeletionBatchPayload(response, stopwatch.ElapsedMilliseconds)); } }
public SpyAmazonSQS( SendMessageResponse sendResponse = null , ReceiveMessageResponse receiveResponse = null , DeleteMessageBatchResponse deleteResponse = null ) { _sendResponse = sendResponse; _receiveResponse = receiveResponse; _deleteResponse = deleteResponse; }
public List <T> ConsumeMessages <T>(string queueUrl, bool deleteMessages = false, int?maxNumberOfMessages = null) { ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest { QueueUrl = queueUrl }; if (maxNumberOfMessages.HasValue) { receiveMessageRequest.MaxNumberOfMessages = maxNumberOfMessages.Value; } ReceiveMessageResponse receiveMessageResponse = m_amazonSqsClient.ReceiveMessage(receiveMessageRequest); var messagesAndMd5 = receiveMessageResponse.Messages .Select(s => new { s.MessageId, s.Body, IsValidMD5 = Md5Utility.CompareMd5Sqs(s.Body, s.MD5OfBody), s.ReceiptHandle }) .ToList(); if (messagesAndMd5.Any(a => !a.IsValidMD5)) { LoggingRepository.Log(LoggingCategory.RepricingScript, "MD5 on IMwsSubscriptionServiceApi was not valid."); } // Filter out messages with corrupted Body (Md5 didn't match). var messagesAndMd5Filtered = messagesAndMd5.Where(w => w.IsValidMD5) .ToDictionary(k => k.MessageId, v => v); if (deleteMessages && messagesAndMd5Filtered.Any()) { List <DeleteMessageBatchRequestEntry> deleteMessageBatchRequestEntries = messagesAndMd5Filtered .Select(s => s.Value) .Select(s => new DeleteMessageBatchRequestEntry { Id = s.MessageId, ReceiptHandle = s.ReceiptHandle }) .ToList(); DeleteMessageBatchResponse deleteMessageBatchResponse = m_amazonSqsClient.DeleteMessageBatch(new DeleteMessageBatchRequest { Entries = deleteMessageBatchRequestEntries, QueueUrl = queueUrl }); // Don't return messages that we weren't able to delete. if (deleteMessageBatchResponse.Failed.Any()) { foreach (BatchResultErrorEntry batchResultErrorEntry in deleteMessageBatchResponse.Failed) { if (messagesAndMd5Filtered.ContainsKey(batchResultErrorEntry.Id)) { messagesAndMd5Filtered.Remove(batchResultErrorEntry.Id); } } } } return(messagesAndMd5Filtered.Select(s => s.Value.Body.FromXml <T>()).ToList()); }
private void GenerateErrorsAndLog(DeleteMessageBatchResponse result) { var failedMessages = String.Join("\n", result.Failed.Select(f => String.Format("Code:{0}, Id:{1}, Error:{2}", f.Code, f.Id, f.Message))); var errorMessage = "There were 1 or more errors when sending messages on commit." + failedMessages; if (result.Successful.Any()) { errorMessage += "\n These message went through the loophole:\n" + String.Join(", ", result.Successful.Select(s => s.Id)); } _log.Warn("Not all completed messages is removed from the queue: {0} \n{1} failed.\n{2}", _inputQueueAddress, result.Failed.Count, errorMessage); }
private static async Task PurgeAmazonSqsQueue(string queueUrl) { using (AmazonSQSClient sqsClient = new AmazonSQSClient()) { ReceiveMessageResponse receiveMessageResponse; do { receiveMessageResponse = await sqsClient.ReceiveMessageAsync( new ReceiveMessageRequest(queueUrl) { MaxNumberOfMessages = 10 }); if (receiveMessageResponse.Messages.Any()) { DeleteMessageBatchResponse deleteMessageResponse = await sqsClient.DeleteMessageBatchAsync(new DeleteMessageBatchRequest(queueUrl, receiveMessageResponse.Messages.Select(_ => new DeleteMessageBatchRequestEntry(_.MessageId, _.ReceiptHandle)).ToList())); } } while (receiveMessageResponse.Messages.Any()); } }
public override List <SqsDeleteMessageResponse> Map(DeleteMessageBatchResponse messageResponse) { Source = messageResponse; Destination = new List <SqsDeleteMessageResponse>(); foreach (var message in messageResponse.Successful) { Destination.Add(new SqsDeleteMessageResponse { MessageId = message.Id, HttpStatusCode = HttpStatusCode.OK }); } foreach (var message in messageResponse.Successful) { Destination.Add(new SqsDeleteMessageResponse { MessageId = message.Id, HttpStatusCode = HttpStatusCode.BadRequest }); } return(Destination); }
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); }
private static void UnmarshallResult(XmlUnmarshallerContext context, DeleteMessageBatchResponse 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("DeleteMessageBatchResultEntry", targetDepth)) { response.Successful.Add(DeleteMessageBatchResultEntryUnmarshaller.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 void Parse() { string xmlText = @" <DeleteMessageBatchResponse xmlns=""http://queue.amazonaws.com/doc/2012-11-05/""> <DeleteMessageBatchResult> <DeleteMessageBatchResultEntry> <Id>msg1</Id> </DeleteMessageBatchResultEntry> <DeleteMessageBatchResultEntry> <Id>msg2</Id> </DeleteMessageBatchResultEntry> </DeleteMessageBatchResult> <ResponseMetadata> <RequestId>d6f86b7a-74d1-4439-b43f-196a1e29cd85</RequestId> </ResponseMetadata> </DeleteMessageBatchResponse>".Trim(); var result = DeleteMessageBatchResponse.Parse(xmlText).DeleteMessageBatchResult.Items; Assert.Equal(2, result.Length); Assert.Equal("msg1", result[0].Id); Assert.Equal("msg2", result[1].Id); }
public DeleteMessageBatchResponse DeleteMessageBatch(DeleteMessageBatchRequest request) { if (request.Entries == null || request.Entries.Count <= 0) { throw new EmptyBatchRequestException("No entires in request"); } if (request.Entries.Count > SqsQueueDefinition.MaxBatchDeleteItems) { throw new TooManyEntriesInBatchRequestException( $"Count of [{request.Entries.Count}] exceeds limit of [{SqsQueueDefinition.MaxBatchDeleteItems}]"); } var q = GetQueue(request.QueueUrl); var response = new DeleteMessageBatchResponse { Failed = new List <BatchResultErrorEntry>(), Successful = new List <DeleteMessageBatchResultEntry>() }; var entryIds = new HashSet <string>(); foreach (var entry in request.Entries) { var success = false; BatchResultErrorEntry batchError = null; try { if (entryIds.Contains(entry.Id)) { throw new BatchEntryIdsNotDistinctException($"Duplicate Id of [{entry.Id}]"); } entryIds.Add(entry.Id); success = q.DeleteMessage(new DeleteMessageRequest { QueueUrl = request.QueueUrl, ReceiptHandle = entry.ReceiptHandle }); } catch (ReceiptHandleIsInvalidException rhex) { batchError = new BatchResultErrorEntry { Id = entry.Id, Message = rhex.Message, Code = rhex.ErrorCode }; } catch (MessageNotInflightException mfex) { batchError = new BatchResultErrorEntry { Id = entry.Id, Message = mfex.Message, Code = mfex.ErrorCode }; } if (success) { response.Successful.Add(new DeleteMessageBatchResultEntry { Id = entry.Id }); } else { var entryToQueue = batchError ?? new BatchResultErrorEntry { Id = entry.Id, Message = "FakeDeleteError", Code = "456" }; response.Failed.Add(entryToQueue); } } return(response); }
internal EndDeletionBatchPayload(DeleteMessageBatchResponse response, long millisecondsTaken) { DeleteMessageBatchResponse = response; MillisecondsTaken = millisecondsTaken; }
private DeleteMessagesResponseModel <string> ConstructDeleteBatchResponseModel(List <DeleteMessageBatchRequestEntry> deleteMessageBatchEntries, DeleteMessageBatchResponse deleteMessageBatchResponse) { DeleteMessagesResponseModel <string> deleteMessageBatchResponseModel = new DeleteMessagesResponseModel <string>(); deleteMessageBatchResponseModel.Successful .AddRange(deleteMessageBatchEntries.Where(entry => deleteMessageBatchResponse.Successful .Select(re => re.Id).Contains(entry.Id)).Select(entry => entry.ReceiptHandle)); deleteMessageBatchResponseModel.Failed .AddRange(deleteMessageBatchEntries.Where(entry => deleteMessageBatchResponse.Failed .Select(re => re.Id).Contains(entry.Id)).Select(entry => entry.ReceiptHandle)); return(deleteMessageBatchResponseModel); }