Enable Listener Endpoint Annotations
若要启用对 @RabbitListener
注解的支持,你可以将 @EnableRabbit
添加到 @Configuration
类中。以下示例展示了如何操作:
To enable support for @RabbitListener
annotations, you can add @EnableRabbit
to one of your @Configuration
classes.
The following example shows how to do so:
@Configuration
@EnableRabbit
public class AppConfig {
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrentConsumers(3);
factory.setMaxConcurrentConsumers(10);
factory.setContainerCustomizer(container -> /* customize the container */);
return factory;
}
}
从版本 2.0 开始,DirectMessageListenerContainerFactory
也可用。它创建 DirectMessageListenerContainer
实例。
Since version 2.0, a DirectMessageListenerContainerFactory
is also available.
It creates DirectMessageListenerContainer
instances.
有关帮助您在 |
For information to help you choose between |
从版本 2.2.2 开始,你可以提供 ContainerCustomizer
实施(如上所示)。这可用于在容器创建和配置后对其进行进一步配置;例如,你可以使用它来设置容器工厂未公开的属性。
Starting with version 2.2.2, you can provide a ContainerCustomizer
implementation (as shown above).
This can be used to further configure the container after it has been created and configured; you can use this, for example, to set properties that are not exposed by the container factory.
版本 2.4.8 提供 CompositeContainerCustomizer
,用于希望应用多个定制程序的情况。
Version 2.4.8 provides the CompositeContainerCustomizer
for situations where you wish to apply multiple customizers.
默认情况下,基础架构会查找名为 rabbitListenerContainerFactory
的 Bean,将其作为用于创建消息侦听器容器的工厂的源。在这种情况下,且忽略 RabbitMQ 基础架构设置,processOrder
方法可以使用三个线程的核心轮询大小和十个线程的最大池大小进行调用。
By default, the infrastructure looks for a bean named rabbitListenerContainerFactory
as the source for the factory to use to create message listener containers.
In this case, and ignoring the RabbitMQ infrastructure setup, the processOrder
method can be invoked with a core poll size of three threads and a maximum pool size of ten threads.
您可以自定义要用于每个注释的侦听器容器工厂,或者可以通过实现 RabbitListenerConfigurer
接口配置明确的默认值。只有在未注册任何特定容器工厂的情况下才需要使用该默认值。有关完整详细信息和示例,请参阅 Javadoc。
You can customize the listener container factory to use for each annotation, or you can configure an explicit default by implementing the RabbitListenerConfigurer
interface.
The default is required only if at least one endpoint is registered without a specific container factory.
See the Javadoc for full details and examples.
容器工厂提供添加 MessagePostProcessor
实例的方法,这些实例在接收到消息(调用侦听器之前)和发送回复之前应用。
The container factories provide methods for adding MessagePostProcessor
instances that are applied after receiving messages (before invoking the listener) and before sending replies.
请参阅 Reply Management以了解有关答复的信息。
See Reply Management for information about replies.
从版本 2.0.6 开始,你可以向侦听器容器工厂添加 RetryTemplate
和 RecoveryCallback
。当发送回复时会使用它。在用尽重试次数时调用 RecoveryCallback
。你可以使用 SendRetryContextAccessor
来获取上下文中的信息。以下示例展示了如何操作:
Starting with version 2.0.6, you can add a RetryTemplate
and RecoveryCallback
to the listener container factory.
It is used when sending replies.
The RecoveryCallback
is invoked when retries are exhausted.
You can use a SendRetryContextAccessor
to get information from the context.
The following example shows how to do so:
factory.setRetryTemplate(retryTemplate);
factory.setReplyRecoveryCallback(ctx -> {
Message failed = SendRetryContextAccessor.getMessage(ctx);
Address replyTo = SendRetryContextAccessor.getAddress(ctx);
Throwable t = ctx.getLastThrowable();
...
return null;
});
如果你更喜欢 XML 配置,可以使用 <rabbit:annotation-driven>
元素。检测到使用 @RabbitListener
注解的任何 Bean。
If you prefer XML configuration, you can use the <rabbit:annotation-driven>
element.
Any beans annotated with @RabbitListener
are detected.
对于 SimpleRabbitListenerContainer
实例,你可以使用类似于以下内容的 XML:
For SimpleRabbitListenerContainer
instances, you can use XML similar to the following:
<rabbit:annotation-driven/>
<bean id="rabbitListenerContainerFactory"
class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="concurrentConsumers" value="3"/>
<property name="maxConcurrentConsumers" value="10"/>
</bean>
对于 DirectMessageListenerContainer
实例,你可以使用类似于以下内容的 XML:
For DirectMessageListenerContainer
instances, you can use XML similar to the following:
<rabbit:annotation-driven/>
<bean id="rabbitListenerContainerFactory"
class="org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="consumersPerQueue" value="3"/>
</bean>
从版本 2.0 开始,@RabbitListener
注解具有 concurrency
属性。它支持 SpEL 表达式(#{…}
)和属性占位符(${…}
)。其含义和允许的值取决于容器类型,如下所示:
Starting with version 2.0, the @RabbitListener
annotation has a concurrency
property.
It supports SpEL expressions (#{…}
) and property placeholders (${…}
).
Its meaning and allowed values depend on the container type, as follows:
-
For the
DirectMessageListenerContainer
, the value must be a single integer value, which sets theconsumersPerQueue
property on the container. -
For the
SimpleRabbitListenerContainer
, the value can be a single integer value, which sets theconcurrentConsumers
property on the container, or it can have the form,m-n
, wherem
is theconcurrentConsumers
property andn
is themaxConcurrentConsumers
property.
在任何一种情况下,此设置都会覆盖工厂中的设置。以前,如果你的侦听器需要不同的并发性,则必须定义不同的容器工厂。
In either case, this setting overrides the settings on the factory. Previously you had to define different container factories if you had listeners that required different concurrency.
该注解还允许通过 autoStartup
和 executor
(从 2.2 开始)注解属性覆盖工厂的 autoStartup
和 taskExecutor
属性。为每个属性使用不同的执行器有助于在日志和线程转储中识别与每个侦听器关联的线程。
The annotation also allows overriding the factory autoStartup
and taskExecutor
properties via the autoStartup
and executor
(since 2.2) annotation properties.
Using a different executor for each might help with identifying threads associated with each listener in logs and thread dumps.
版本 2.2 还添加了 ackMode
属性,它允许你覆盖容器工厂的 acknowledgeMode
属性。
Version 2.2 also added the ackMode
property, which allows you to override the container factory’s acknowledgeMode
property.
@RabbitListener(id = "manual.acks.1", queues = "manual.acks.1", ackMode = "MANUAL")
public void manual1(String in, Channel channel,
@Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
...
channel.basicAck(tag, false);
}