Reputation: 1
I am using @kafkaListener annotation provided by spring to create kafka container
@KafkaListener(topics = "#", group = "#", containerFactory = "myContainerFactory")
public void listen(final ConsumerRecord<?, ?> message, Acknowledgment ack) {
Is there any way we can get a handle of the ConcurrentMessageListenerContainer which the @kafkaListener annotation creates under the hood?I would like to programmatically start/stop the container which is my final goal. Thanks in advance
Upvotes: 0
Views: 679
Reputation: 174739
See the documentation.
@KafkaListener Lifecycle Management
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. This bean is automatically declared by the framework and manages the containers' lifecycles; it will auto-start any containers that have autoStartup set to true. All containers created by all container factories must be in the same phase. See Listener Container Auto Startup for more information. You can manage the lifecycle programmatically by using the registry. Starting or stopping the registry will start or stop all the registered containers. Alternatively, you can get a reference to an individual container by using its id attribute. You can set autoStartup on the annotation, which overrides the default setting configured into the container factory. You can get a reference to the bean from the application context, such as auto-wiring, to manage its registered containers. The following examples show how to do so:
@KafkaListener(id = "myContainer", topics = "myTopic", autoStartup = "false")
public void listen(...) { ... }
@Autowired
private KafkaListenerEndpointRegistry registry;
...
this.registry.getListenerContainer("myContainer").start();
...
Upvotes: 1