@KafkaListener
Lifecycle Management
为 @KafkaListener`注释创建的侦听器容器不是应用程序上下文中 bean。相反,它们使用类型为“`KafkaListenerEndpointRegistry
”的基础架构 bean 注册。此 bean 由框架自动声明,并管理容器生命周期;它将自动启动具有 autoStartup`设置为“`true
”的所有容器。所有容器工厂创建的所有容器必须在相同的“phase
”中。有关详情,请参阅“Listener Container Auto Startup”。您可以使用注册表以编程方式管理生命周期。启动或停止注册表将启动或停止所有已注册的容器。或者,您可以使用容器的“id
”属性获取对单个容器的引用。您可以在注释中设置“autoStartup
”,该属性会覆盖容器工厂中配置的默认设置。您可以从应用程序上下文中获取 bean 的引用(例如自动装配),以管理其注册的容器。以下示例显示了如何执行此操作:
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();
...
该注册表只维护其管理的容器的生命周期;声明为 Bean 的容器不受注册表管理,可以从应用程序上下文中获取。可以通过调用注册表的 getListenerContainers()
方法来获取托管容器的集合。2.2.5 版添加了一个便捷方法 getAllListenerContainers()
,它返回所有容器的集合,包括注册表管理的容器和声明为 Bean 的容器。返回的集合将包括任何已初始化的原型 Bean,但它不会初始化任何延迟的 Bean 声明。
The registry only maintains the life cycle of containers it manages; containers declared as beans are not managed by the registry and can be obtained from the application context.
A collection of managed containers can be obtained by calling the registry’s getListenerContainers()
method.
Version 2.2.5 added a convenience method getAllListenerContainers()
, which returns a collection of all containers, including those managed by the registry and those declared as beans.
The collection returned will include any prototype beans that have been initialized, but it will not initialize any lazy bean declarations.
在应用程序上下文经过刷新后注册的端点将立即启动,无论其 autoStartup
属性如何,以遵守 SmartLifecycle
合约,其中 autoStartup
仅在应用程序上下文初始化期间予以考虑。延迟注册的一个示例是具有 @KafkaListener
的原型范围的 bean,其中会在上下文初始化后创建一个实例。从版本 2.8.7 开始,您可以将注册表的 alwaysStartAfterRefresh
属性设为 false
,然后容器的 autoStartup
属性将定义是否启动该容器。
Endpoints registered after the application context has been refreshed will start immediately, regardless of their autoStartup
property, to comply with the SmartLifecycle
contract, where autoStartup
is only considered during application context initialization.
An example of late registration is a bean with a @KafkaListener
in prototype scope where an instance is created after the context is initialized.
Starting with version 2.8.7, you can set the registry’s alwaysStartAfterRefresh
property to false
and then the container’s autoStartup
property will define whether or not the container is started.