Alberto Alegria
Alberto Alegria

Reputation: 1060

How can I retrieve Kafka messages inside a controller in Spring Boot?

The messages created by the producer are all being consumed as expected.
The thing is, I need to create an endpoint to retrieve the latest messages from the consumer.
Is there a way to do it? Like an on-demand consumer?

I found this SO post but is only to consume the last N records. I want to consume the latest without caring about the offsets.

Spring Kafka Consumer, rewind consumer offset to go back 'n' records

I'm working with Kotlin but if you have the answer in Java I don't mind either.

Upvotes: 0

Views: 803

Answers (1)

Gary Russell
Gary Russell

Reputation: 174494

There are several ways to create listener containers dynamically; you can then start/stop them on demand. To get the records back into the controller, you'd need to use something like a blocking queue, or make the controller itself a MessageListener.

These answers show a couple of techniques for creating containers on demand:

How to dynamically create multiple consumers in Spring Kafka

Kafka Consumer in spring can I re-assign partitions programmatically?

Upvotes: 1

Related Questions