예제 #1
0
        public void Send <T>(T message, string destination) where T : MessageBase
        {
            var envelope = new MessageEnvelope
            {
                Message        = JsonConvert.SerializeObject(message),
                MessageType    = message.GetType().AssemblyQualifiedName,
                OrganisationId = message.OrganisationId.IsNullOrEmpty() ? Organisation.NullOrganisationId : message.OrganisationId,
                QueueUrl       = destination,
                GeneratedOnUtc = DateTime.UtcNow
            };

            try
            {
                _amazonSQS.SendMessage(new SendMessageRequest
                {
                    QueueUrl    = destination,
                    MessageBody = JsonConvert.SerializeObject(envelope),
                });
            }
            catch (AmazonSQSException e)
            {
                //if we are attempting to send a message to a receive queue which does not exist for an organisation
                //craete the queue and try to send the message again
                if (e.Message.ToLowerInvariant().Contains("the specified queue does not exist"))
                {
                    _createSQSQueueCommand.Invoke(new CreateSQSQueueRequest
                    {
                        QueueName = destination.GetQueueName()
                    });

                    _amazonSQS.SendMessage(new SendMessageRequest
                    {
                        QueueUrl    = destination,
                        MessageBody = JsonConvert.SerializeObject(envelope),
                    });
                }
            }
        }
예제 #2
0
        private void ReceiveFromQueue()
        {
            int emptyReceiptCount = 0;

            while (_serviceRunning)
            {
                try
                {
                    var request = new ReceiveMessageRequest
                    {
                        QueueUrl            = _queueUrl,
                        MaxNumberOfMessages = _serviceConfiguration.MaxNumberOfMessagesPerReceive,
                        WaitTimeSeconds     = 20
                    };

                    Trace("Attmepting to receive message from queue '{0}'", _queueUrl);

                    ReceiveMessageResponse response;

                    try
                    {
                        response = _amazonSQS.ReceiveMessage(request);
                    }
                    catch (AmazonSQSException e)
                    {
                        //create the queue if the exception indicates the queue does not exist
                        if (e.Message.ToLowerInvariant().Contains("the specified queue does not exist"))
                        {
                            _createSQSQueueCommand.Invoke(new CreateSQSQueueRequest
                            {
                                QueueName = _queueUrl.GetQueueName()
                            });
                        }

                        emptyReceiptCount = 9;                         //equiv to 3 mins pause, the maximum we wait
                        Thread.Sleep(_requestThrottler.GetDelayMilliseconds(emptyReceiptCount));
                        continue;
                    }

                    if (!response.IsSetReceiveMessageResult() || response.ReceiveMessageResult.Message.Count == 0)
                    {
                        emptyReceiptCount++;
                        Trace("No message returned from queue '{0}', zero message count:={1}", _queueUrl, emptyReceiptCount);

                        //sleep for delay as specified by throttler (unless instructed to poll now)
                        int       delay       = _requestThrottler.GetDelayMilliseconds(emptyReceiptCount);
                        const int sleepPeriod = 100;
                        int       sleepCount  = 0;
                        while (sleepPeriod * ++sleepCount < delay)
                        {
                            if (_pollNow)
                            {
                                emptyReceiptCount = 0;
                                _pollNow          = false;
                                break;
                            }
                            Thread.Sleep(sleepPeriod);
                        }
                        continue;
                    }

                    //reset the zero message count
                    emptyReceiptCount = 0;

                    Trace("Received {0} messages from queue '{1}'", response.ReceiveMessageResult.Message.Count, _queueUrl);

                    foreach (var message in response.ReceiveMessageResult.Message)
                    {
                        var envelope = GetEnvelope(message);

                        Trace("Receiving message for organisation:={0}", envelope.OrganisationId);

                        try
                        {
                            ObjectFactory.GetObject <IMessageProcessor>().Process(_serviceConfiguration, envelope);

                            _amazonSQS.DeleteMessage(new DeleteMessageRequest
                            {
                                QueueUrl      = _queueUrl,
                                ReceiptHandle = envelope.ReceiptHandle
                            });
                        }
                        catch (Exception e)
                        {
                            var de = new ErrorditeDeleteSQSMessageException("Failed to delete message with receipt handle:={0}".FormatWith(envelope.ReceiptHandle), true, e);

                            if (e is ThreadAbortException)
                            {
                                continue;
                            }

                            Error(de);
                            ErrorditeClient.ReportException(de);
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is ThreadAbortException)
                    {
                        continue;
                    }

                    Error(e);
                    ErrorditeClient.ReportException(e);
                }
            }
        }