From a6c0a8223eae739ce05e14a8d9ab868a11fe41ec Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Fri, 26 Oct 2018 16:30:05 -0400 Subject: [PATCH] Clarify listener endpoint registry bean https://stackoverflow.com/questions/52978691/registering-kafkalistenercontainers-with-kafkalistenerendpointregistry/52980393#comment92932278_52980393 --- src/reference/asciidoc/kafka.adoc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/reference/asciidoc/kafka.adoc b/src/reference/asciidoc/kafka.adoc index ed67781d..bc5c2349 100644 --- a/src/reference/asciidoc/kafka.adoc +++ b/src/reference/asciidoc/kafka.adoc @@ -1121,11 +1121,22 @@ Use a custom deserializer, the `JsonDeserializer` or the `(String|Bytes)JsonMess 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 manages the containers' lifecycles; it will auto-start any containers that have `autoStartup` set to `true`. +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 <> for more information. You can manage the lifecycle programmatically using the registry; starting/stopping the registry will start/stop all the registered containers. Or, you can get a reference to an individual container using its `id` attribute; you can set `autoStartup` on the annotation, which will override the default setting configured into the container factory. +Simply get a reference to the bean from the application context, such as auto wiring, to manage its registered containers: +==== +[source, java] +---- +@KafkaListener(id = "myContainer", topics = "myTopic", autoStartup = "false") +public void listen(...) { ... } + +---- +==== + +==== [source, java] ---- @Autowired @@ -1133,13 +1144,11 @@ private KafkaListenerEndpointRegistry registry; ... -@KafkaListener(id = "myContainer", topics = "myTopic", autoStartup = "false") -public void listen(...) { ... } + this.registry.getListenerContainer("myContainer").start(); ... - - registry.getListenerContainer("myContainer").start(); ---- +==== [[kafka-validation]] ===== @KafkaListener @Payload Validation