예제 #1
0
 private static void MonitorQueue(Config config, AmazonSQSClient client)
 {
     foreach (var @group in config.Groups)
     {
         foreach (var queue in group.Queues)
         {
             try
             {
                 var url        = client.GetQueueUrl(queue);
                 var attributes = client.GetQueueAttributes(
                     new GetQueueAttributesRequest(url.QueueUrl,
                                                   new List <string> {
                     "ApproximateNumberOfMessages"
                 }));
                 if (attributes.ApproximateNumberOfMessages >= group.Threshold)
                 {
                     Console.ForegroundColor = ConsoleColor.Yellow;
                     Console.WriteLine($"{queue} has {attributes.ApproximateNumberOfMessages} messages");
                 }
             }
             catch (Exception e)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine($"Monitor exeption: {e.Message}");
                 Console.ResetColor();
             }
         }
     }
 }
        private void GetMessages()
        {
            var queueUrlRequest       = new GetQueueUrlRequest("Messenger.fifo");
            var queueUrl              = _sqsClient.GetQueueUrl(queueUrlRequest).QueueUrl;
            var receiveMessageRequest = new ReceiveMessageRequest {
                QueueUrl = queueUrl
            };

            while ()
            {
                Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                var receiveMessageResponse = _sqsClient.ReceiveMessage(receiveMessageRequest);
                foreach (var message in receiveMessageResponse.Messages)
                {
                    var deserializedMessage = JsonConvert.DeserializeObject <MessageModel>(message.Body);
                    if (deserializedMessage.ChatId.Equals(_parentViewModel.ChatId, StringComparison.InvariantCulture) && !deserializedMessage.Author.Equals(_parentViewModel.Author))
                    {
                        NewMessageReceived?.Invoke(deserializedMessage);
                        var messageRecieptHandle = receiveMessageResponse.Messages[0].ReceiptHandle;
                        var deleteRequest        = new DeleteMessageRequest
                        {
                            QueueUrl      = queueUrl,
                            ReceiptHandle = messageRecieptHandle
                        };
                        _sqsClient.DeleteMessage(deleteRequest);
                    }
                }
            }
        }
        private string GetQueueUrl()
        {
            GetQueueUrlResponse queueUrlResult = sqsClient.GetQueueUrl(QueueName);
            string queueUrl = queueUrlResult.QueueUrl;

            return(queueUrl);
        }
예제 #4
0
        private AmazonSQSClient InitializeQueue()
        {
            var client = new AmazonSQSClient(Utility.GetRegionEndpoint());

            ListQueuesRequest listQueuesRequest = new ListQueuesRequest
            {
                QueueNamePrefix = QueueName
            };
            var  listQueuesResponse = client.ListQueues(listQueuesRequest);
            bool found = listQueuesResponse.ListQueuesResult.QueueUrls.Any(s => s == QueueName);

            if (found == false)
            {
                var createQueueResponse = client.CreateQueue(new CreateQueueRequest
                {
                    QueueName = QueueName
                });
                QueueUrl = createQueueResponse.CreateQueueResult.QueueUrl;
            }
            else
            {
                QueueUrl = client.GetQueueUrl(
                    new GetQueueUrlRequest
                {
                    QueueName = _queueName
                }
                    ).GetQueueUrlResult.QueueUrl;
            }
            return(client);
        }
        public async Task <CloudPlatformResponse> DequeueMessagesAsync(CloudPlatformRequest request)
        {
            var cResponse = new CloudPlatformResponse();

            try
            {
                ReceiveMessageResponse response;
                using (var client = new AmazonSQSClient())
                {
                    var queueUrl = client.GetQueueUrl(request.Source).QueueUrl;
                    response = await client.ReceiveMessageAsync(queueUrl);

                    if (response.HttpStatusCode == HttpStatusCode.OK)
                    {
                        cResponse.Data = response.Messages;
                        foreach (var message in response.Messages)
                        {
                            await client.DeleteMessageAsync(new DeleteMessageRequest(queueUrl, message.ReceiptHandle));
                        }
                    }
                    else
                    {
                        cResponse.ErrorDetails.Add("unknown", 1);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(cResponse);
        }
예제 #6
0
        public MessageQueueDetail Build <T>(string uri)
        {
            var listResponse = _client.ListQueues(new ListQueuesRequest().WithQueueNamePrefix(uri));
            var exists       = listResponse.ListQueuesResult.QueueUrl.Any();

            if (exists)
            {
                var queue    = _client.GetQueueUrl(new GetQueueUrlRequest().WithQueueName(uri));
                var request  = new GetQueueAttributesRequest().WithQueueUrl(queue.GetQueueUrlResult.QueueUrl);
                var response = _client.GetQueueAttributes(request);

                return(new MessageQueueDetail
                {
                    MessageCount = response.GetQueueAttributesResult.ApproximateNumberOfMessages,
                    Uri = queue.GetQueueUrlResult.QueueUrl,
                    Exists = true
                });
            }
            else
            {
                return(new MessageQueueDetail
                {
                    MessageCount = null,
                    Uri = uri,
                    Exists = false
                });
            }
        }
예제 #7
0
        private static void GetQueueStatus()
        {
            var response = _client.GetQueueUrl(Data.Queue.QueueName);

            _queueUrl = string.IsNullOrEmpty(response.QueueUrl)
                ? CreateQueue
                : _queueUrl = response.QueueUrl;
        }
예제 #8
0
        /**
         * Retrieve the URL of the SQS queue by using the AmazonSQSClient
         * object and queueName constant for SQS queue name
         *
         * @return    URL
         */
        private static string GetURL()
        {
            // TODO 8: Replace the solution with your own code
            Debug.WriteLine(String.Format("RUNNING JAYARAJ'S CODE: {0}! \n", "GetURL"));
            GetQueueUrlResponse queueUrlResponse = sqsClient.GetQueueUrl(QueueName);

            return(queueUrlResponse.QueueUrl);
        }
예제 #9
0
        public static string GetURL(AmazonSQSClient sqsClient, string QueueName)
        {
            Debug.WriteLine(String.Format("RUNNING SOLUTION CODE: {0}! Follow the steps in the lab guide to replace this method with your own implementation.\n", "GetURL"));
            GetQueueUrlResponse queueUrlResponse = sqsClient.GetQueueUrl(QueueName);
            string queueUrl = queueUrlResponse.QueueUrl;

            return(queueUrl);
        }
예제 #10
0
        private static void GetQueueUrl(AmazonSQSClient sqsClient)
        {
            var getQueueUrlRequest = new GetQueueUrlRequest
            {
                QueueName = SingleQueue
            };

            var response1 = sqsClient.GetQueueUrl(getQueueUrlRequest);

            Console.WriteLine($"GetQueueUrl(GetQueueUrlRequest) HTTP status code: {response1.HttpStatusCode}");
            _singleQueueUrl = response1.QueueUrl;

            var response2 = sqsClient.GetQueueUrl(BatchedQueue);

            Console.WriteLine($"GetQueueUrl(string) HTTP status code: {response2.HttpStatusCode}");
            _batchedQueueUrl = response2.QueueUrl;
        }
        public AmazonMessageQueueProvider(MainWindowViewModel parentViewModel, CancellationToken ct)
        {
            _parentViewModel = parentViewModel;
            _sqsClient       = new AmazonSQSClient();
            var queueUrlRequest = new GetQueueUrlRequest("Messenger.fifo");

            _queueUrl = _sqsClient.GetQueueUrl(queueUrlRequest).QueueUrl;

            Task.Factory.StartNew(GetMessages, ct);
        }
예제 #12
0
        public void SubmitJobAsTestModel(string testModelAsString)
        {
            AmazonSQSClient amazonSqsClient = new AmazonSQSClient(ConfigurationManager.AppSettings["accessKeyID"],ConfigurationManager.AppSettings["secretAccessKeyID"], RegionEndpoint.EUWest1);

            var queue = amazonSqsClient.GetQueueUrl("EgAutomatedTests");

            var mod = JsonConvert.DeserializeObject<TestModel>(testModelAsString);

            amazonSqsClient.SendMessage(queue.QueueUrl, JsonConvert.SerializeObject(mod));
        }
예제 #13
0
        public string GetQueueUrl(string queueName)
        {
            var request = new GetQueueUrlRequest
            {
                QueueName = MailerQueueName,
                QueueOwnerAWSAccountId = QueueOwnerAccountId
            };

            var getQueueUrlResponse = _sqsClient.GetQueueUrl(request);

            return(getQueueUrlResponse.HttpStatusCode.Equals(HttpStatusCode.OK) ? getQueueUrlResponse.QueueUrl : string.Empty);
        }
예제 #14
0
        public void GetQueueUrlExample()
        {
            AmazonSQSClient client = new AmazonSQSClient();

            var request = new GetQueueUrlRequest
            {
                QueueName = "SQS_QUEUE_NAME"
            };

            GetQueueUrlResponse response = client.GetQueueUrl(request);

            Console.WriteLine("The SQS queue's URL is {1}", response.QueueUrl);
        }
예제 #15
0
        public static void ProcessConfigMessages()
        {
            Log.Info("Start processing queue messages");
            var configQueueName = "config-queue";

            if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ConfigQueueName"]))
            {
                configQueueName = ConfigurationManager.AppSettings["ConfigQueueName"];
            }
            var configQueueUrl = sqsClient.GetQueueUrl(configQueueName).QueueUrl;
            var timestamp      = DateTime.UtcNow;

            ProcessConfigMessagesBatch(configQueueUrl, timestamp);
            Log.Info("End processing queue messages");
        }
예제 #16
0
        public static void SQSGetQueueUrl()
        {
            #region SQSGetQueueUrl
            var client = new AmazonSQSClient();

            var request = new GetQueueUrlRequest
            {
                QueueName = "MyTestQueue",
                QueueOwnerAWSAccountId = "80398EXAMPLE"
            };

            var response = client.GetQueueUrl(request);

            Console.WriteLine("Queue URL: " + response.QueueUrl);
            #endregion

            Console.ReadLine();
        }
        public CloudPlatformResponse DeleteMessages(CloudPlatformRequest request)
        {
            var sqsClient  = new AmazonSQSClient();
            var cpResponse = new CloudPlatformResponse();

            try
            {
                var queueUrl = sqsClient.GetQueueUrl(request.Source).QueueUrl;
                var response = sqsClient.PurgeQueue(queueUrl);
                cpResponse.HttpStatusCode = response.HttpStatusCode;
            }
            catch (PurgeQueueInProgressException)
            {
                //retry
                Thread.Sleep(TimeSpan.FromSeconds(60));
                cpResponse.HttpStatusCode = HttpStatusCode.InternalServerError;
            }
            return(cpResponse);
        }
        public async Task <CloudPlatformResponse> EnqueueMessagesAsync(CloudPlatformRequest request)
        {
            var cResponse = new CloudPlatformResponse();

            try
            {
                SendMessageBatchResponse response;
                var messages          = (IEnumerable <string>)request.Data[ServerlessBenchmark.Constants.Message];
                var batchMessageEntry =
                    messages.Select(message => new SendMessageBatchRequestEntry(Guid.NewGuid().ToString("N"), message))
                    .ToList();
                using (var client = new AmazonSQSClient())
                {
                    response = await client.SendMessageBatchAsync(client.GetQueueUrl(request.Source).QueueUrl, batchMessageEntry);
                }
                if (response.Failed.Any())
                {
                    var groupedFailures = response.Failed.GroupBy(failure => failure.Message);
                    foreach (var group in groupedFailures)
                    {
                        cResponse.ErrorDetails.Add(group.Key, group.Count());
                    }
                    cResponse.HttpStatusCode = HttpStatusCode.InternalServerError;
                }
                else
                {
                    cResponse.HttpStatusCode = HttpStatusCode.OK;
                }
            }
            catch (InvalidCastException)
            {
                Console.WriteLine("Data needs to be IEnumberable of strings");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(cResponse);
        }
예제 #19
0
        static void Main(string[] args)
        {
            var appSettings = ConfigurationManager.AppSettings;

            string sqsQueueName   = appSettings["AWSQueue" + args[0].ToString().ToUpper().Trim()].ToString();
            string sqsDLQueueName = appSettings["AWSDeadLetterQueue" + args[0].ToString().ToUpper().Trim()].ToString();
            int    maxSQSRetries  = Convert.ToInt32(appSettings["MaxSQSRetries"].ToString());
            string urlWMIService  = appSettings["WMIServiceURL" + args[0].ToString().ToUpper().Trim()].ToString();
            int    batchSize      = Convert.ToInt32(appSettings["BatchSize"].ToString());

            IAmazonSQS     sqsClient     = new AmazonSQSClient();
            IAWSSQSWrapper awsSQSWrapper = new AWSSQSWrapper(sqsClient);

            //Display our current list of Amazon SQS queues
            Console.WriteLine("Checking to see if AWS environment is available...\n");
            ListQueuesResponse lqr = awsSQSWrapper.LisQueues();

            bool queueExists   = false;
            bool dlqueueExists = false;

            if (lqr.QueueUrls != null)
            {
                foreach (String queueUrl in lqr.QueueUrls)
                {
                    if (queueUrl.ToUpper().Trim().EndsWith(sqsQueueName.ToUpper().Trim()))
                    {
                        queueExists = true;
                        Console.WriteLine("Queue Exists: {0}", queueUrl);

                        GetQueueAttributesResponse gQAR = awsSQSWrapper.GetQueueAttributes(queueUrl);
                        if (gQAR.Attributes != null)
                        {
                            foreach (KeyValuePair <string, string> tmp in gQAR.Attributes)
                            {
                                Console.WriteLine("     Attribute: {0}  Value:{1}", tmp.Key.ToString(), tmp.Value.ToString());
                            }
                        }
                    }

                    if (queueUrl.ToUpper().Trim().EndsWith(sqsDLQueueName.ToUpper().Trim()))
                    {
                        dlqueueExists = true;
                        Console.WriteLine("Queue Exists: {0}", queueUrl);

                        GetQueueAttributesResponse gQAR = awsSQSWrapper.GetQueueAttributes(queueUrl);
                        if (gQAR.Attributes != null)
                        {
                            foreach (KeyValuePair <string, string> tmp in gQAR.Attributes)
                            {
                                Console.WriteLine("     Attribute: {0}  Value:{1}", tmp.Key.ToString(), tmp.Value.ToString());
                            }
                        }
                    }
                }
            }

            if (!dlqueueExists)
            {
                Console.WriteLine("The dead letter queue was missing, let's build it...\n");
                awsSQSWrapper.CreateQueue(sqsDLQueueName, false);
                Console.WriteLine("Dead Letter Queue built successfuly.\n");
                Console.WriteLine("Press any key...");
                Console.ReadKey();
            }

            if (!queueExists)
            {
                Console.WriteLine("Message queue was missing, let's build it...\n");
                awsSQSWrapper.CreateQueue(sqsQueueName, false);
                awsSQSWrapper.AttachDeadLetterQueue(sqsQueueName, sqsDLQueueName, maxSQSRetries);
                Console.WriteLine("Message queue built successfuly.\n");
                Console.WriteLine("Press any key...");
                Console.ReadKey();
            }

            Console.WriteLine("AWS environment ready.\n");
            Console.WriteLine("Press any key...");
            Console.ReadKey();

            GetQueueUrlResponse awsGQUR = sqsClient.GetQueueUrl(sqsQueueName);

            Console.WriteLine("Creating test messages...");
            CreateMessages(awsSQSWrapper, awsGQUR.QueueUrl.ToString(), 10);
            Console.WriteLine("Creating test messages complete.");
            Console.WriteLine("Press any key...");
            Console.ReadKey();

            string response = GetREST();

            Console.WriteLine("GET Received: {0}", response);
            Console.WriteLine("Press any key...");
            Console.ReadKey();

            //string awsMessage = GetMessage(awsSQSWrapper, awsGQUR.QueueUrl);

            bool continueReceiving = true;

            while (continueReceiving)
            {
                ReceiveMessageResponse awsRMR = awsSQSWrapper.ReceiveMessage(awsGQUR.QueueUrl);

                if (awsRMR.Messages.Count > 0)
                {
                    response = PostREST(awsRMR.Messages[0].Body.ToString());
                    Console.WriteLine("POST Returned: {0}", response);
                    DeleteMessageResponse awsDMR = awsSQSWrapper.DeleteMessage(awsGQUR.QueueUrl, awsRMR.Messages[0].ReceiptHandle.ToString());
                    Console.WriteLine("Message Deleted: {0}", awsRMR.Messages[0].ReceiptHandle.ToString());
                }
                else
                {
                    continueReceiving = false;
                }
            }

            Console.WriteLine("\n\nAll messages sent.");
            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }