private static void UnmarshallResult(XmlUnmarshallerContext context, ReceiveMessageResponse 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("Message", targetDepth))
                    {
                        var unmarshaller = MessageUnmarshaller.Instance;
                        var item = unmarshaller.Unmarshall(context);
                        response.Messages.Add(item);
                        continue;
                    }
                } 
           }

            return;
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context,ReceiveMessageResponse 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("Message", targetDepth))
                    {
                        response.Messages.Add(MessageUnmarshaller.GetInstance().Unmarshall(context));
                            
                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }
                            


            return;
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>  
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            ReceiveMessageResponse response = new ReceiveMessageResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;
            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {                    
                    if(context.TestExpression("ReceiveMessageResult", 2))
                    {
                        UnmarshallResult(context, response);                        
                        continue;
                    }
                    
                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return response;
        }
            public CheckForMessagesShould()
            {
                expectedQueueUrl = "QueueUrl";
                queueInfo        = new QueueInfo(typeof(TestMessage), expectedQueueUrl);
                var queues = new Dictionary <string, QueueInfo>();

                queues.Add("", queueInfo);

                var messageBusMock = new Mock <IMessageBus>();

                sqsServiceMock = new Mock <ISqsService>();
                var messageDeserializerMock = new Mock <IMessageDeserializer>();

                handlerMock = new Mock <IHandlerAsync <TestMessage> >();

                messageBusMock.Setup(x => x.Queues).Returns(queues);
                messageBusMock.Setup(x => x.GetHandler <TestMessage>()).Returns(handlerMock.Object);

                expectedMessage = new TestMessage();
                var response = new Amazon.SQS.Model.ReceiveMessageResponse();

                busMessage = new Amazon.SQS.Model.Message();
                busMessage.ReceiptHandle = "Recipt001";
                response.Messages.Add(busMessage);
                sqsServiceMock.Setup(x => x.ReceiveMessageAsync(expectedQueueUrl)).Returns(Task.FromResult(response));
                messageDeserializerMock.Setup(x => x.Deserialize <TestMessage>(busMessage)).Returns(expectedMessage);

                sut = new MessageBusListener(messageBusMock.Object, sqsServiceMock.Object, messageDeserializerMock.Object);
            }
        protected internal virtual List<CombinedMarketDataRequest> Convert(ReceiveMessageResponse response)
        {
            var list = new List<CombinedMarketDataRequest>();
            if ((response != null)
                && (response.IsSetReceiveMessageResult()))
            {
                var result = response.ReceiveMessageResult;
                if (result.IsSetMessage())
                {
                    var queueMessages = result.Message;
                    CombinedMarketDataRequest marketDataRequest = null;
                    foreach (var qm in queueMessages)
                    {
                        try
                        {
                            marketDataRequest = this.Convert(qm);
                            if (marketDataRequest != null)
                            {
                                list.Add(marketDataRequest);
                            }
                        }
                        catch (PossiblyPoisonMessageException ppme)
                        {
                            System.Diagnostics.Debug.WriteLine(ppme.ToString());
                            Log.LogException(ppme);
                        }

                    }
                }
            }

            return list;
        }
 private static void ValidateReceiveMessage(ReceiveMessageResponse response)
 {
     if (response != null && response.Messages != null && response.Messages.Count > 0)
     {
         foreach (Message message in response.Messages)
         {
             ValidateMD5(message);
         }
     }
 }
        public override List<Advertisement> checkMessages()
        {
            List<Advertisement> AdvertisementList = new List<Advertisement>();

            Advertisement newAd = new Advertisement(0,"0","0");

            int currentFetchCounter = 0;

            do
            {
                string fullMessage = string.Empty;
                string receiptHandle;

                string selectedQueue = QueueURLString;

                ReceiveMessageResponse queueReceiveMessageResponse = new ReceiveMessageResponse();

                queueReceiveMessageResponse = objClient.ReceiveMessage(new ReceiveMessageRequest()
                {
                    QueueUrl = selectedQueue,
                    MaxNumberOfMessages = 10,
                    VisibilityTimeout = 5

                });

                ReceiveMessageResult objReceiveMessageResult = new ReceiveMessageResult();

                objReceiveMessageResult = queueReceiveMessageResponse.ReceiveMessageResult;

                List<Amazon.SQS.Model.Message> messagesList = new List<Amazon.SQS.Model.Message>();

                if(objReceiveMessageResult.IsSetMessage())
                     messagesList = objReceiveMessageResult.Message;

                foreach (Amazon.SQS.Model.Message objMessage in messagesList)
                {
                    fullMessage += objMessage.Body;
                    receiptHandle = objMessage.ReceiptHandle;

                    newAd = createAdvertisement(fullMessage);

                    if(!AdvertisementList.Contains(newAd))
                        AdvertisementList.Add(newAd);
                }

                currentFetchCounter++;

            } while (currentFetchCounter<numberOfMsgToFetch);

            return AdvertisementList;
        }
Exemple #8
0
        public static void ProcessQueuedBounce(ReceiveMessageResponse response)
        {
            int messages = response.ReceiveMessageResult.Message.Count;

            if (messages > 0)
            {
                foreach (var m in response.ReceiveMessageResult.Message)
                {
                    // First, convert the Amazon SNS message into a JSON object.
                    if (m.Body.IndexOf("notificationType") >= 0)
                    {
                        var notification = Newtonsoft.Json.JsonConvert.DeserializeObject<AmazonSesBounceNotification>(m.Body);
                        if (notification.Bounce != null)
                        {
                            // Now access the Amazon SES bounce notification.
                            //var bounce = Newtonsoft.Json.JsonConvert.DeserializeObject<AmazonSesBounceNotification>(notification.Message);

                            switch (notification.Bounce.BounceType)
                            {
                                case "Transient":
                                    // Per our sample organizational policy, we will remove all recipients
                                    // that generate an AttachmentRejected bounce from our mailing list.
                                    // Other bounces will be reviewed manually.
                                    switch (notification.Bounce.BounceSubType)
                                    {
                                        case "AttachmentRejected":
                                            foreach (var recipient in notification.Bounce.BouncedRecipients)
                                            {
                                                //RemoveFromMailingList(recipient.EmailAddress);
                                            }
                                            break;
                                        default:
                                            //ManuallyReviewBounce(bounce);
                                            break;
                                    }
                                    break;
                                default:
                                    // Remove all recipients that generated a permanent bounce
                                    // or an unknown bounce.
                                    foreach (var recipient in notification.Bounce.BouncedRecipients)
                                    {
                                        //RemoveFromMailingList(recipient.EmailAddress);
                                    }
                                    break;
                            }
                        }
                    }
                }
            }
        }
        protected override void Given()
        {
            base.Given();

            // we expect to get max 10 messages per batch
            // except on single-core machines when we top out at ParallelHandlerExecutionPerCore=8
            expectedMaxMessageCount = Math.Min(MessageConstants.MaxAmazonMessageCap, 
                Environment.ProcessorCount * MessageConstants.ParallelHandlerExecutionPerCore);

            var response1 = GenerateResponseMessage(SubjectOfMessageAfterStop, Guid.NewGuid());
            var response2 = new ReceiveMessageResponse
            {
                Messages = new List<Message>()
            };

            Sqs.ReceiveMessageAsync(
                Arg.Any<ReceiveMessageRequest>(),
                Arg.Any<CancellationToken>())
            .Returns(
                _ => Task.FromResult(response1),
                _ => Task.FromResult(response2));
        }
 public static void ValidateReceiveMessage(ReceiveMessageResponse response)
 {
     if (response != null && response.ReceiveMessageResult != null &&
         response.ReceiveMessageResult.Message != null && response.ReceiveMessageResult.Message.Count > 0)
     {
         foreach (Message message in response.ReceiveMessageResult.Message)
         {
             AmazonSQSUtil.ValidateMD5(message);
         }
     }
 }
        private Message BufferResponse(ReceiveMessageResponse response)
        {
            if (response == null || response.Messages == null)
                return null;

            Message toReturn = null;

            foreach (var message in response.Messages.Where(m => m != null))
            {
                if (toReturn == null)
                {
                    toReturn = message;
                    continue;
                }

                receiveBuffer.Enqueue(message);
            }

            return toReturn;
        }
Exemple #12
0
        /// <summary>
        /// The method loads a one or more messages from the queue
        /// </summary>
        public bool DeQueueMessages()
        {
            ClearErrorInfo();

            bool result = false;
            try
            {
                rcvMessageResponse = queue.ReceiveMessage(rcvMessageRequest);
                result = true;
            }
            catch (Exception ex)
            {
                ErrorCode = e_Exception;
                ErrorMessage = ex.Message;
            }
            return result;
        }
        public ReceiveMessageResponse ReceiveMessage(ReceiveMessageRequest request)
        {
            if (request.MaxNumberOfMessages > SqsQueueDefinition.MaxBatchReceiveItems)
            {
                throw new TooManyEntriesInBatchRequestException("Count of [{0}] exceeds limit of [{1]}".Fmt(request.MaxNumberOfMessages, SqsQueueDefinition.MaxBatchReceiveItems));
            }

            var q = GetQueue(request.QueueUrl);

            var response = new ReceiveMessageResponse
            {
                Messages = q.Receive(request)
                .Select(qi => new Message {
                    Body = qi.Body,
                    Attributes = qi.Attributes,
                    MessageId = qi.MessageId,
                    ReceiptHandle = qi.ReceiptHandle
                })
                .ToList()
            };

            return response;
        }