Container Management

针对注解创建的容器未在应用程序上下文中注册。你可以通过调用`RabbitListenerEndpointRegistry` Bean 上的 getListenerContainers() 来获取所有容器的集合。然后,你可以对该集合进行迭代,例如,停止或启动所有容器或调用注册表本身上的 Lifecycle 方法,这将在每个容器上调用这些操作。

Containers created for annotations are not registered with the application context. You can obtain a collection of all containers by invoking getListenerContainers() on the RabbitListenerEndpointRegistry bean. You can then iterate over this collection, for example, to stop or start all containers or invoke the Lifecycle methods on the registry itself, which will invoke the operations on each container.

也可以使用 id 通过 getListenerContainer(String id) 引用单个容器,例如,上面的代码段创建的容器 registry.getListenerContainer("multi")

You can also get a reference to an individual container by using its id, using getListenerContainer(String id) — for example, registry.getListenerContainer("multi") for the container created by the snippet above.

从版本 1.5.2 开始,可以使用 getListenerContainerIds() 获取已注册容器的 id 值。

Starting with version 1.5.2, you can obtain the id values of the registered containers with getListenerContainerIds().

从版本 1.5 开始,现在可以将 group 分配给 RabbitListener 端点的容器。这提供了一种机制来引用容器的子集。添加 group 属性将在上下文中使用组名注册类型为 Collection<MessageListenerContainer> 的 Bean。

Starting with version 1.5, you can now assign a group to the container on the RabbitListener endpoint. This provides a mechanism to get a reference to a subset of containers. Adding a group attribute causes a bean of type Collection<MessageListenerContainer> to be registered with the context with the group name.

默认情况下,停止容器会取消消费者并在停止前处理所有预取消息。从版本 2.4.14、3.0.6 开始,您可以将 forceStop 容器属性设置为 true,以便在处理完当前消息后立即停止,导致重新排列任何预取消息。例如,当使用独占或单活动消费者时,这将会很有用。

By default, stopping a container will cancel the consumer and process all prefetched messages before stopping. Starting with versions 2.4.14, 3.0.6, you can set the forceStop container property to true to stop immediately after the current message is processed, causing any prefetched messages to be requeued. This is useful, for example, if exclusive or single-active consumers are being used.