コード例 #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Booking Processing Service Started");

            // Magic line to yield control of this background service back to the rest of the startup process
            await Task.Yield();

            // Create new consumer and producer
            var kafkaConsumerService = new KafkaConsumerService(_logger, _consumerConfig, KafkaTopics.TOPIC_BOOKING_REQUEST);
            var kafkaProducerService = new KafkaProducerService(_logger, _producerConfig, KafkaTopics.TOPIC_BOOKING_CONFIRMATION);

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    // Wait for a new booking request
                    var bookingRequest = kafkaConsumerService.WaitAndRead <MovieBooking>(stoppingToken);
                    if (bookingRequest != null)
                    {
                        _logger.LogInformation($"Processing new booking: {bookingRequest}");

                        // Create new booking confirmation based on a request
                        var bookingConfirmation = new BookingConfirmation(bookingRequest);

                        // Publish a booking confirmation to a Kafka stream
                        await kafkaProducerService.Write(bookingConfirmation);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError($"Error in processing : {e.Message}");
                }
            }
        }
コード例 #2
0
        public MessageConsumerChildActor()
        {
            Receive<int>(msg =>
            {
                Console.WriteLine("in consumer child before");
                consumerService = new KafkaConsumerService<MessageDTO>("keltonjohntest", "messagegroup", new Uri[] { new Uri("http://logsvrdev:9092") });
                Console.WriteLine("in consumer child after");
                var list = consumerService.Consume(); //new List<MessageDTO>();

                //consumerService.Consume().ContinueWith(items =>
                //{
                //    Console.WriteLine("in consumer count " + list.Count);
                //    list = items.Result;
                //    Console.WriteLine("after" + list.Count);
                //})
                //.PipeTo(Self);

                // Consume the topic "message"
                var actor = Context.ActorOf(Props.Create(() => new MessageCoordinatorActor()),
                    Guid.NewGuid().ToString());

                foreach (var item in list)
                {
                    actor.Forward(new Message(item.Message, Guid.NewGuid()));
                }

                 Console.WriteLine("IN consumer after send to database ");
                // Console.WriteLine(msg);
                // INSERT TO A DATABASE OR SEND AN EMAIL
            });
        }
コード例 #3
0
        private static void Consume2()
        {
            var svc = new KafkaConsumerService<Test>("john", "ConsumerGroup-MultiPartition-1", new[] { new Uri("http://logsvrdev:9092") });
            var list = svc.Consume();

            foreach (var item in list)
            {
                Console.WriteLine("v " + item.Title);
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            CancellationTokenSource token = new CancellationTokenSource();

            Console.CancelKeyPress += (_, e) =>
            {
                e.Cancel = true;
                token.Cancel();
            };

            // Create consumer config
            var conf = new ConsumerConfig
            {
                GroupId            = "kafka_test_cli",
                BootstrapServers   = "localhost:9092",
                AutoOffsetReset    = AutoOffsetReset.Earliest,
                EnablePartitionEof = true,
            };

            // Create a logger
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder
                .AddConsole()
                .AddDebug();
            });
            ILogger <Program> logger = loggerFactory.CreateLogger <Program>();

            logger.LogInformation("Consumer CLI started");

            // Create Kafka consumer and consume messages
            var kafkaConsumerService = new KafkaConsumerService(logger, conf, "topic_booking_request");

            try
            {
                while (!token.IsCancellationRequested)
                {
                    var bookingRequest = kafkaConsumerService.WaitAndRead(token.Token);
                    if (bookingRequest != null)
                    {
                        Console.WriteLine($"Consumed message '{bookingRequest}'");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
コード例 #5
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Welcome to KafkaCarsConsumer Console");
            Console.ForegroundColor = ConsoleColor.Green;

            string endpoint = string.Empty;
            string topic    = string.Empty;

            //default use for quickly test
            //string endpoint = "192.168.99.100:9092";
            //string topic = "cars";

            while (true)
            {
                #region commands
                if (!IsValidEndpoint(endpoint))
                {
                    Console.WriteLine("Write the kafka server endpoint (eg: 192.168.99.100:9092)");
                    endpoint = Console.ReadLine();
                    if (!IsValidEndpoint(endpoint))
                    {
                        Console.WriteLine("Error: invalid endpoint address!");
                        continue;
                    }
                }
                if (string.IsNullOrEmpty(topic))
                {
                    Console.WriteLine("Write the topic name");
                    topic = Console.ReadLine();
                    if (string.IsNullOrEmpty(topic))
                    {
                        Console.WriteLine("Error: invalid topic name!");
                        continue;
                    }
                }
                Console.WriteLine("Press 'Enter' to start or 'Esc' to exit");
                var start = Console.ReadKey();
                if (start.Key == ConsoleKey.Escape)
                {
                    break;
                }
                #endregion
                if (start.Key == ConsoleKey.Enter)
                {
                    var c = new KafkaConsumerService(endpoint, string.Format("{0}_group", topic));
                    c.Subscribe(topic);
                    Console.WriteLine("Starting consuming...");
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    while (true)
                    {
                        var cancToken = new CancellationTokenSource();
                        try
                        {
                            Console.WriteLine();
                            var m = c.Consume(cancToken);
                            Console.WriteLine(string.Format("Consumed: {0}", m));
                        }
                        catch (OperationCanceledException)
                        {
                            c.Unsubscribe();
                        }
                        catch (Exception ce)
                        {
                            throw ce;
                        }
                    }
                }
            }
        }