0

I faced the problem with the following code:

    public IActionResult Consume(string topic)
    {
        try
        {
            var config = new ConsumerConfig
            {
                BootstrapServers = "kafka-01:9092,kafka-02:9092,kafka-03:9092",
                SecurityProtocol = SecurityProtocol.SaslPlaintext,
                SaslMechanism = SaslMechanism.ScramSha256,
                SaslUsername = "my-consumer",
                SaslPassword = "pass",
                GroupId = "my-test-group"
            };

            using IConsumer<Null, string> consumer = new ConsumerBuilder<Null, string>(config).Build();
            consumer.Subscribe(topic);

            var cts = new CancellationTokenSource();
            cts.CancelAfter(TimeSpan.FromSeconds(60));

            ConsumeResult<Null, string> result = consumer.Consume(cts.Token);
            consumer.Close();

            return Ok(result.Message.Value);
        }
        catch (OperationCanceledException cancelEx)
        {
            return NoContent();
        }
        catch (ConsumeException consumeEx)
        {
            return BadRequest(consumeEx.ToString());
        }
    }

But consumer doesn't work! As I see in Debug, it freezes until cancelationToken expires, throws an OperationCancelledException, and i receive no result.

The producer seems to work, it doesn't freeze or return error (using the similar config).

Why is doesn't work?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Laser42
  • 646
  • 2
  • 7
  • 24
  • Did you try setting the starting offset to the beginning of the topic or producing data while the consumer runs? – OneCricketeer Aug 19 '21 at 11:08
  • @OneCricketeer Haven't tried offset manipulation, will do. I tried to start consumering and then tried to produce. – Laser42 Aug 19 '21 at 12:12
  • 1
    @OneCricketeer tried to add to ConsumerConfig the following AutoOffsetReset = AutoOffsetReset.Earliest. After that I received an answer. Thank you – Laser42 Aug 19 '21 at 12:21

1 Answers1

1

By default consumers start at the end of the topic and wait for new messages

If you want to consume existing data, create a new groupId and set AutoOffsetReset = AutoOffsetReset.Earliest

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245