Beispiel #1
0
        public async Task <IActionResult> ProduceMessage(CreateMessageRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            await _service.ProduceAsync(request);

            return(Ok());
        }
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            // You can find a lot of similar code on Confluent's examples.
            // https://github.com/confluentinc/confluent-kafka-dotnet/blob/master/examples/Consumer/Program.cs

            using var consumer = _kafkaService.GetConsumer();

            try
            {
                consumer.Subscribe(topics);

                while (true)
                {
                    try
                    {
                        // setting the time span to 5 seconds so this will exit if there is no data after 5 seconds.
                        // this happens during the initial start.
                        var consumeResult = consumer.Consume(TimeSpan.FromSeconds(5));

                        if (consumeResult == null)
                        {
                            break;
                        }

                        if (consumeResult.IsPartitionEOF)
                        {
                            Console.WriteLine($"Reached end of topic {consumeResult.Topic}, partition {consumeResult.Partition}, offset {consumeResult.Offset}.");
                            break;
                        }

                        Console.WriteLine($"Received message at {consumeResult.TopicPartitionOffset}: {consumeResult.Message.Value}");

                        // setting
                        var setting = JsonSerializer.Deserialize <ProcessSettings>(consumeResult.Message.Value);
                        setting.LastProcessed = consumeResult.Message.Timestamp.UtcDateTime;
                        processSettings.Add(setting);
                    }
                    catch (ConsumeException e)
                    {
                        Console.WriteLine($"Consume error: {e.Error.Reason}");
                    }
                }
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Closing consumer.");
                consumer.Close();
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                // update setting
                ProcessSettings setting = new ProcessSettings();
                setting.SettingName = "KafkaLogCompaction";
                setting.Value       = "value";

                var json = JsonSerializer.Serialize(setting);
                await _kafkaService.ProduceAsync(topics, json, cancellationToken);

                _logger.LogInformation("Worker running at: {time}", setting.LastProcessed);
                await Task.Delay(2000, cancellationToken);
            }
        }