Example #1
0
        public KafkaGasService(KafkaConsumerMenager consumerMenager, KafkaProducerService kafkaProducerService)
        {
            this.consumerMenager = consumerMenager;
            KafkaProducerService = kafkaProducerService;

            this.KafkaAdaptor = new KafkaAdaptor();
        }
        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}");
                }
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Welcome to KafkaCarsProducer 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 p = new KafkaProducerService(endpoint);
                    Console.WriteLine("Starting producer");
                    while (true)
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        //generate a car
                        var car    = Helpers.CarGenerator.GenerateCar();
                        var offset = p.SendMessageAsync(topic, car);
                        Console.WriteLine(string.Format("Produced offset: {0} - Car: {1}", offset, car.Message()));
                        Thread.Sleep(500);
                    }
                }
            }
        }
Example #4
0
        private async Task ProccessAddCar(KpCarModel kpCar, KafkaProducerService producerService)
        {
            var mappViewModel = this.adaptor.MappCarViewModel(kpCar);

            var id = await this.manager.AddNewCarAsync(mappViewModel);

            string jsonString = JsonSerializer.Serialize(id);

            await producerService.ProduceMessageAsync(jsonString);
        }
        private async Task ProcessAddPerson(KpPersonModel kpPerson, KafkaProducerService producerService)
        {
            var personVM = this.adaptor.Transform(kpPerson);

            var id = await this.manager.AddNewPersonAsync(personVM);

            string jsonString = JsonSerializer.Serialize(id);

            await producerService.ProduceMessageAsync(jsonString);
        }
        public async Task <ActionResult> PostAsync([FromBody] MovieBooking booking)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _logger.LogInformation($"New movie booking received: {booking}");

            // Publish a new booking to the stream
            var kafkaProducerService = new KafkaProducerService(_logger, _config, KafkaTopics.TOPIC_BOOKING_REQUEST);
            await kafkaProducerService.Write(booking);

            return(Ok("Your booking is being processed"));
        }
Example #7
0
        public ICarGasService GetCarGasService()
        {
            KafkaConsumerMenager consumerMenager = kafkaServiceFactory
                                                   .GetKafkaConsumerMenager(
                kafkaOptions.Consumers[0],
                kafkaOptions.Settings.BootstrapServer
                );

            KafkaProducerService kafkaProducerService = new KafkaProducerService(
                kafkaOptions.Settings,
                kafkaOptions.Producers[0]
                );

            return(new KafkaGasService(consumerMenager, kafkaProducerService));
        }
Example #8
0
        private async Task ProcessQuery(KafkaProducerService producerService)
        {
            var allCars = await this.manager.GetCarsAsync();

            List <KpCarModel> cars = this.adaptor.MappCarViewModel(allCars);

            KafkaProtocolModel kafkaProtocol = new KafkaProtocolModel
            {
                Data = cars,
                Head = new KpHeapModel
                {
                    Action  = "Result",
                    Version = "1"
                }
            };

            string jsonString = JsonSerializer.Serialize(kafkaProtocol);

            await producerService.ProduceMessageAsync(jsonString);
        }
        public async Task ProccessMessage(KafkaOptions kafkaOptions, IServiceScope serviceScope, string message)
        {
            IKafkaServiceFactory kafkaFactory = serviceScope.ServiceProvider
                                                .GetRequiredService <IKafkaServiceFactory>();

            IFactory factory = serviceScope.ServiceProvider
                               .GetRequiredService <IFactory>();

            this.manager = new PersonManager(factory);
            this.adaptor = factory.GetAdaptor();

            KafkaProducerService producerService = kafkaFactory.GetProducerService
                                                   (
                kafkaOptions.Settings,
                kafkaOptions.Producers[0]
                                                   );

            if (int.TryParse(message, out int id))
            {
                KafkaProtocolModel kafkaProtocol = this.GetProtocol(message);

                switch (kafkaProtocol.Head.Action)
                {
                case "QUERY":
                    await this.ProcessQuery(producerService);

                    break;

                case "ADD_PERSON":
                    await this.ProcessAddPerson(kafkaProtocol.Data[0], producerService);

                    break;
                }
            }
            else
            {
                var carID = int.Parse(message);
                await this.RemoveCar(carID);
            }
        }