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}");
                }
            }
        }
Exemplo n.º 2
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);
            }
        }