Masoom Sabri
Masoom Sabri

Reputation: 41

Spring Kafka- Prevent consumer to connect Kafka topic during build in Spring Boot

I did autoStartup(false) on the container factory but not sure where I should inject KafkaListenerEndpointRegistry.

I don't want consumer to connect at the time of build. It should connect Kafka topic after spring boot application start completely.

Upvotes: 4

Views: 12910

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 39998

You should inject the KafkaListenerEndpointRegistry in class with KafkaListener annotated method because from the docs

The listener containers created for @KafkaListener annotations are not beans in the application context. Instead, they are registered with an infrastructure bean of type KafkaListenerEndpointRegistry

so by using this KafkaListenerEndpointRegistry instance you can manage the life cycle of containers start/stop here

Example :

@Autowired
private KafkaListenerEndpointRegistry registry;

   ...

 @KafkaListener(id = "myContainer", topics = "myTopic", autoStartup = "false")
 public void listen(...) { ... }

 ...
registry.getListenerContainer("myContainer").start();

Upvotes: 5

Related Questions