Changes in 2.1 Since 2.0

AMQP Client library

Spring AMPQ 现使用 RabbitMQ 团队提供的 amqp-client 库的 5.4.x 版本。此客户端默认配置了自动恢复。参见 RabbitMQ Automatic Connection/Topology recovery

Spring AMQP now uses the 5.4.x version of the amqp-client library provided by the RabbitMQ team. This client has auto-recovery configured by default. See RabbitMQ Automatic Connection/Topology recovery.

从版本 4.0 开始,客户端默认启用自动恢复。Spring AMQP 与此功能兼容,但有自己的恢复机制,通常不需要客户端恢复功能。我们建议禁用 amqp-client 自动恢复,以避免在代理可用但连接尚未恢复时获取 AutoRecoverConnectionNotCurrentlyOpenException 实例。从版本 1.7.1 开始,Spring AMQP 会禁用它,除非您显式创建自己的 RabbitMQ 连接工厂并将其提供给 CachingConnectionFactory。由 RabbitConnectionFactoryBean 创建的 RabbitMQ ConnectionFactory 实例的选项在默认情况下也处于禁用状态。

As of version 4.0, the client enables automatic recovery by default. While compatible with this feature, Spring AMQP has its own recovery mechanisms and the client recovery feature generally is not needed. We recommend disabling amqp-client automatic recovery, to avoid getting AutoRecoverConnectionNotCurrentlyOpenException instances when the broker is available but the connection has not yet recovered. Starting with version 1.7.1, Spring AMQP disables it unless you explicitly create your own RabbitMQ connection factory and provide it to the CachingConnectionFactory. RabbitMQ ConnectionFactory instances created by the RabbitConnectionFactoryBean also have the option disabled by default.

Package Changes

某些类已移至不同的包。大部分是内部类,不影响用户应用程序。有两个例外,即 ChannelAwareMessageListenerRabbitListenerErrorHandler。这些接口现在位于 org.springframework.amqp.rabbit.listener.api 中。

Certain classes have moved to different packages. Most are internal classes and do not affect user applications. Two exceptions are ChannelAwareMessageListener and RabbitListenerErrorHandler. These interfaces are now in org.springframework.amqp.rabbit.listener.api.

Publisher Confirms Changes

在有未决确认的情况下,为发布者确认启用的通道不会返回到缓存。参见 Correlated Publisher Confirms and Returns 了解更多信息。

Channels enabled for publisher confirmations are not returned to the cache while there are outstanding confirmations. See Correlated Publisher Confirms and Returns for more information.

Listener Container Factory Improvements

你现在可以使用监听器容器工厂创建任何监听器容器,而不仅仅是那些与 @RabbitListener 注释或 @RabbitListenerEndpointRegistry 配合使用的容器。有关更多信息,请参见 Using Container Factories

You can now use the listener container factories to create any listener container, not only those for use with @RabbitListener annotations or the @RabbitListenerEndpointRegistry. See Using Container Factories for more information.

ChannelAwareMessageListener 现在继承自 MessageListener

ChannelAwareMessageListener now inherits from MessageListener.

Broker Event Listener

引入了 BrokerEventListener 以将所选的代理事件发布为 ApplicationEvent 实例。有关更多信息,请参见 Broker Event Listener

A BrokerEventListener is introduced to publish selected broker events as ApplicationEvent instances. See Broker Event Listener for more information.

RabbitAdmin Changes

RabbitAdmin 发现类型为 Declarables 的 Bean(它是一个容器,用于 Declarable - QueueExchange`和 `Binding 对象),并在代理上声明包含的对象。建议用户不要使用旧机制来声明 <Collection<Queue>>(以及其他机制),而应改用 Declarables Bean。默认情况下,旧机制被禁用。参见 Declaring Collections of Exchanges, Queues, and Bindings 了解更多信息。

The RabbitAdmin discovers beans of type Declarables (which is a container for Declarable - Queue, Exchange, and Binding objects) and declare the contained objects on the broker. Users are discouraged from using the old mechanism of declaring <Collection<Queue>> (and others) and should use Declarables beans instead. By default, the old mechanism is disabled. See Declaring Collections of Exchanges, Queues, and Bindings for more information.

AnonymousQueue 实例现在声明时默认将 x-queue-master-locator 设置为 client-local,以确保在应用程序已连接到的节点上创建队列。有关更多信息,请参见 Configuring the Broker

AnonymousQueue instances are now declared with x-queue-master-locator set to client-local by default, to ensure the queues are created on the node the application is connected to. See Configuring the Broker for more information.

RabbitTemplate Changes

你现在可以用 noLocalReplyConsumer 选项配置 RabbitTemplate,以便控制 sendAndReceive() 操作中的回复消费者 noLocal 标志。有关更多信息,请参见 Request/Reply Messaging

You can now configure the RabbitTemplate with the noLocalReplyConsumer option to control a noLocal flag for reply consumers in the sendAndReceive() operations. See Request/Reply Messaging for more information.

CorrelationData 进行发布者确认现已包含 ListenableFuture,您可以使用它来获取确认而不是使用回调。当启用返回和确认时,如果提供了相关数据,将使用返回的消息填充相关数据。参见 Correlated Publisher Confirms and Returns 了解更多信息。

CorrelationData for publisher confirmations now has a ListenableFuture, which you can use to get the acknowledgment instead of using a callback. When returns and confirmations are enabled, the correlation data, if provided, is populated with the returned message. See Correlated Publisher Confirms and Returns for more information.

现已提供名为 replyTimedOut 的方法来通知子类回复已超时,从而允许进行任何状态清理。参见 Reply Timeout 了解更多信息。

A method called replyTimedOut is now provided to notify subclasses that a reply has timed out, allowing for any state cleanup. See Reply Timeout for more information.

当使用 DirectReplyToMessageListenerContainer(默认)发出请求/返回消息时,您现在可以指定一个 ErrorHandler,以便当传输回复时(例如,延迟回复)发生异常时调用该错误处理程序。请参阅 RabbitTemplate 上的 setReplyErrorHandler。(自 2.0.11 版起)

You can now specify an ErrorHandler to be invoked when using request/reply with a DirectReplyToMessageListenerContainer (the default) when exceptions occur when replies are delivered (for example, late replies). See setReplyErrorHandler on the RabbitTemplate. (Also since 2.0.11).

Message Conversion

我们引入了一个新的 Jackson2XmlMessageConverter,以支持消息与 XML 格式之间进行转换。参见 Jackson2XmlMessageConverter 了解更多信息。

We introduced a new Jackson2XmlMessageConverter to support converting messages from and to XML format. See Jackson2XmlMessageConverter for more information.

Management REST API

RabbitManagementTemplate 现已弃用,建议直接使用 com.rabbitmq.http.client.Client(或 com.rabbitmq.http.client.ReactorNettyClient)。有关更多信息,请参见 Management REST API

The RabbitManagementTemplate is now deprecated in favor of the direct com.rabbitmq.http.client.Client (or com.rabbitmq.http.client.ReactorNettyClient) usage. See Management REST API for more information.

@RabbitListener Changes

现在,监听器容器工厂可以用一个`RetryTemplate`来进行配置,并且可以利用一个`RecoveryCallback`来进行可选配置,在发送应答时使用。有关更多信息,请参见Enable Listener Endpoint Annotations

The listener container factory can now be configured with a RetryTemplate and, optionally, a RecoveryCallback used when sending replies. See Enable Listener Endpoint Annotations for more information.

Async @RabbitListener Return

现在, @RabbitListener`方法可以返回`ListenableFuture<?>`或`Mono<?>。有关更多信息,请参见Asynchronous @RabbitListener Return Types

@RabbitListener methods can now return ListenableFuture<?> or Mono<?>. See Asynchronous @RabbitListener Return Types for more information.

Connection Factory Bean Changes

默认情况下,RabbitConnectionFactoryBean 现在调用 enableHostnameVerification()。若要恢复到以前的运行状况,请将 enableHostnameVerification 属性设置为 false

By default, the RabbitConnectionFactoryBean now calls enableHostnameVerification(). To revert to the previous behavior, set the enableHostnameVerification property to false.

Connection Factory Changes

CachingConnectionFactory 现在无条件地禁用底层 RabbitMQ ConnectionFactory 中的自动重试,即使在构造函数中提供了预先配置的实例。虽然已采取措施使 Spring AMQP 兼容自动重试,但仍然出现了一些问题。自 1.0.0 版起,Spring AMQP 就有了自己的重试机制,并且不需要使用客户端提供的重试。虽然在构建 CachingConnectionFactory 之后仍可以启用该功能(使用 cachingConnectionFactory.getRabbitConnectionFactory() .setAutomaticRecoveryEnabled()),但我们强烈建议您不要这样做。我们建议您在直接使用客户端工厂时使用单独的 RabbitMQ ConnectionFactory,以便在需要自动重试连接时使用(而不是使用 Spring AMQP 组件)。

The CachingConnectionFactory now unconditionally disables auto-recovery in the underlying RabbitMQ ConnectionFactory, even if a pre-configured instance is provided in a constructor. While steps have been taken to make Spring AMQP compatible with auto recovery, certain corner cases have arisen where issues remain. Spring AMQP has had its own recovery mechanism since 1.0.0 and does not need to use the recovery provided by the client. While it is still possible to enable the feature (using cachingConnectionFactory.getRabbitConnectionFactory() .setAutomaticRecoveryEnabled()) after the CachingConnectionFactory is constructed, we strongly recommend that you not do so. We recommend that you use a separate RabbitMQ ConnectionFactory if you need auto recovery connections when using the client factory directly (rather than using Spring AMQP components).

Listener Container Changes

现在,默认的`ConditionalRejectingErrorHandler`如果存在`x-death`头,则会完全丢弃导致致命错误的消息。有关更多信息,请参见Exception Handling

The default ConditionalRejectingErrorHandler now completely discards messages that cause fatal errors if an x-death header is present. See Exception Handling for more information.

Immediate requeue

引入了一个新的 ImmediateRequeueAmqpException,以通知监听器容器消息必须重新入队。要使用此功能,就需要添加一个新的 ImmediateRequeueMessageRecoverer 实现。

A new ImmediateRequeueAmqpException is introduced to notify a listener container that the message has to be re-queued. To use this feature, a new ImmediateRequeueMessageRecoverer implementation is added.

请查阅 Message Listeners and the Asynchronous Case 了解更多信息。