public void RetrieveOrders(ILogger _logger, int?messagesToRetreive = null, CancellationToken?token = null)
        {
            var orders = new List <Order>();

            AmazonSQSConfig       amazonSQSConfig       = new AmazonSQSConfig();
            var                   count                 = 0;
            var                   amazonSQSClient       = new AmazonSQSClient(amazonSQSConfig);
            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest {
                QueueUrl = MyQueueUrl
            };

            while (true)
            {
                if (token != null && ((CancellationToken)token).IsCancellationRequested || (messagesToRetreive != null && count >= messagesToRetreive))
                {
                    break;
                }

                ReceiveMessageResponse receiveMessageResponse =
                    amazonSQSClient.ReceiveMessage(receiveMessageRequest);

                var result = receiveMessageResponse;
                if (result.Messages.Count != 0)
                {
                    for (int i = 0; i < result.Messages.Count; i++)
                    {
                        var order = JsonConvert.DeserializeObject <Order>(result.Messages[i].Body);
                        orders.Add(order);
                        Task.Run(async() => { await SiloManager.StartOrder(order); });

                        Console.WriteLine(order);
                        count++;
                        try
                        {
                            amazonSQSClient.DeleteMessage(MyQueueUrl, result.Messages[i].ReceiptHandle);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                }
                Thread.Sleep(1000);
            }

            //  return orders;
        }
Beispiel #2
0
        public void RetrieveOrders(ILogger _logger, int?messagesToRetreive = null, CancellationToken?token = null)
        {
            try
            {
                var count    = 0;
                var orders   = new List <Order>();
                var options  = new KafkaOptions(new Uri("http://localhost:9092"), new Uri("http://localhost:9092"));
                var router   = new BrokerRouter(options);
                var consumer = new Consumer(new ConsumerOptions("PizzaOrderTopic", router));
                foreach (var message in consumer.Consume())
                {
                    if (token != null && ((CancellationToken)token).IsCancellationRequested || (messagesToRetreive != null && count >= messagesToRetreive))
                    {
                        break;
                    }
                    try
                    {
                        var order = JsonConvert.DeserializeObject <Order>(System.Text.Encoding.Default.GetString(message.Value));
                        orders.Add(order);
                        Task.Run(async() => { await SiloManager.StartOrder(order); });
                        count++;
                        // Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                    }
                    catch (Exception e)
                    {
                        _logger.Info($"Error occured Kafka: {e}");
                    }
                    Console.WriteLine("Response: P{0},O{1} : {2}",
                                      message.Meta.PartitionId, message.Meta.Offset, message.Value);
                }
            }
            catch (Exception e)
            {
                _logger.Info($"Error occured Kafka: {e}");
                RetrieveOrders(_logger, messagesToRetreive, token);
            }


            //return orders;
        }