Changes in 2.0 Since 1.7
-
引入单独的连接工厂,以避免生产者队列死锁。
-
启用 AMQP 客户端库的自动恢复。
-
修改 RabbitTemplate 以提供更可靠的消息传递。
-
增强了监听器容器,包括新的 DirectMessageListenerContainer 和改进的预取值。
-
弃用 Jackson 1.x 支持,并添加对 JSON 和 XML 的转换支持。
-
改善了异常处理和消息回滚行为。
Using CachingConnectionFactory
从 2.0.2 版本开始,你可以将 RabbitTemplate
配置为使用与侦听器容器所使用的不同的连接。当生产者因任何原因被阻止时,此更改避免了死锁使用者。更多信息,请参阅 Using a Separate Connection。
Starting with version 2.0.2, you can configure the RabbitTemplate
to use a different connection to that used by listener containers.
This change avoids deadlocked consumers when producers are blocked for any reason.
See Using a Separate Connection for more information.
AMQP Client library
Spring AMQP 现在使用 RabbitMQ 团队提供的 amqp-client
新的 5.0.x 版本。此客户端默认配置自动恢复。请参阅 RabbitMQ Automatic Connection/Topology recovery。
Spring AMQP now uses the new 5.0.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 虽然与此功能兼容,但它拥有自己的恢复机制,且一般不需要客户端恢复功能。我们建议您禁用自动恢复,以避免当代理可用但连接尚未恢复时出现实例。从 1.7.1 版本开始,Spring AMQP 会禁用该功能,除非您明确创建自己的 RabbitMQ 连接工厂,并将其提供给 RabbitMQ 实例。客户端创建的 RabbitMQ 实例也默认禁用该选项。 |
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 that you disable |
General Changes
默认情况下, ExchangeBuilder
现在生成持久性交换。在 @QeueueBinding
中使用的注释 @Exchange
也默认声明持久性交换。在 @RabbitListener
中使用的注释 @Queue
默认声明命名的持久性队列,如果匿名则声明非持久性队列。更多信息,请参阅 Builder API for Queues and Exchanges 和 Annotation-driven Listener Endpoints。
The ExchangeBuilder
now builds durable exchanges by default.
The @Exchange
annotation used within a @QeueueBinding
also declares durable exchanges by default.
The @Queue
annotation used within a @RabbitListener
by default declares durable queues if named and non-durable if anonymous.
See Builder API for Queues and Exchanges and Annotation-driven Listener Endpoints for more information.
Deleted Classes
不再提供 UniquelyNameQueue
。创建具有唯一名称的持久非自动删除队列并不常见。此类已被删除。如果您需要其功能,请使用 new Queue(UUID.randomUUID().toString())
。
UniquelyNameQueue
is no longer provided.
It is unusual to create a durable non-auto-delete queue with a unique name.
This class has been deleted.
If you require its functionality, use new Queue(UUID.randomUUID().toString())
.
New Listener Container
DirectMessageListenerContainer
已添加到现有的 SimpleMessageListenerContainer
旁边。请参阅 Choosing a Container 和 Message Listener Container Configuration 以获取有关选择使用哪个容器以及如何配置容器的信息。
The DirectMessageListenerContainer
has been added alongside the existing SimpleMessageListenerContainer
.
See Choosing a Container and Message Listener Container Configuration for information about choosing which container to use as well as how to configure them.
Log4j Appender
此追加器不再可用,因为 log4j 已达到生命周期结束。请参阅 Logging Subsystem AMQP Appenders 以获取有关可用日志追加器的信息。
This appender is no longer available due to the end-of-life of log4j. See Logging Subsystem AMQP Appenders for information about the available log appenders.
RabbitTemplate
Changes
以前,如果非事务性参与现有事务,则它会在事务侦听器容器线程上运行。这是一个严重的错误。不过,用户可能依赖该行为。从 1.6.2 版本开始,您必须在模板上设置布尔值以便参与容器事务。
Previously, a non-transactional RabbitTemplate
participated in an existing transaction if it ran on a transactional listener container thread.
This was a serious bug.
However, users might have relied on this behavior.
Starting with version 1.6.2, you must set the channelTransacted
boolean on the template for it to participate in the container transaction.
RabbitTemplate
现在使用 DirectReplyToMessageListenerContainer
(默认)而不是为每个请求创建一个新的消费者。更多信息,请参阅 RabbitMQ Direct reply-to。
The RabbitTemplate
now uses a DirectReplyToMessageListenerContainer
(by default) instead of creating a new consumer for each request.
See RabbitMQ Direct reply-to for more information.
AsyncRabbitTemplate
现在支持直接回复。更多信息,请参阅 Async Rabbit Template。
The AsyncRabbitTemplate
now supports direct reply-to.
See Async Rabbit Template for more information.
RabbitTemplate`和`AsyncRabbitTemplate`现在具有`receiveAndConvert`和`convertSendAndReceiveAsType`方法,这些方法采用`ParameterizedTypeReference<T>`参数,通过这些参数,调用方可以指定将结果转换为的类型。这对于复杂类型或者在消息头中未传达类型信息时特别有用。它需要`SmartMessageConverter
,例如`Jackson2JsonMessageConverter`。请参阅Request/Reply Messaging、Async Rabbit Template、Converting From a Message
With RabbitTemplate
和[json-complex]以获取更多信息。
The RabbitTemplate
and AsyncRabbitTemplate
now have receiveAndConvert
and convertSendAndReceiveAsType
methods that take a ParameterizedTypeReference<T>
argument, letting the caller specify the type to which to convert the result.
This is particularly useful for complex types or when type information is not conveyed in message headers.
It requires a SmartMessageConverter
such as the Jackson2JsonMessageConverter
.
See Request/Reply Messaging, Async Rabbit Template, Converting From a Message
With RabbitTemplate
, and [json-complex] for more information.
你现在可以使用 RabbitTemplate
对专用通道执行多项操作。更多信息,请参阅 Scoped Operations。
You can now use a RabbitTemplate
to perform multiple operations on a dedicated channel.
See Scoped Operations for more information.
Listener Adapter
一个方便的 FunctionalInterface
可用于将 lambda 与 MessageListenerAdapter
配合使用。有关详细信息,请参阅 xref:amqp/receiving-messages/async-consumer.adoc#message-listener-adapter[MessageListenerAdapter
。
A convenient FunctionalInterface
is available for using lambdas with the MessageListenerAdapter
.
See MessageListenerAdapter
for more information.
Listener Container Changes
Prefetch Default Value
默认预取值曾经为 1,这可能会导致对有效消费者的使用不足。现在,默认预取值为 250,这应该可以让消费者在大多数常见场景中保持忙碌,从而提高吞吐量。
The prefetch default value used to be 1, which could lead to under-utilization of efficient consumers. The default prefetch value is now 250, which should keep consumers busy in most common scenarios and, thus, improve throughput.
在某些情况下,预取值应当较低,例如,当消息较大时(尤其是当处理速度较慢时,消息可能会在客户端进程中占用大量内存),以及当需要严格的消息顺序时(在这种情况下,应将预取值设回 1)。另外,如果消息量较少并且消费者较多(包括单个侦听器容器实例内的并发),您可能希望减少预取,以便在各个消费者之间更均匀地分配消息。
There are scenarios where the prefetch value should be low — for example, with large messages, especially if the processing is slow (messages could add up to a large amount of memory in the client process), and if strict message ordering is necessary (the prefetch value should be set back to 1 in this case). Also, with low-volume messaging and multiple consumers (including concurrency within a single listener container instance), you may wish to reduce the prefetch to get a more even distribution of messages across consumers.
有关预先获取的更多背景信息,请参阅有关 consumer utilization in RabbitMQ 的此文章和有关 queuing theory 的此文章。
For more background about prefetch, see this post about consumer utilization in RabbitMQ and this post about queuing theory.
Message Count
以前,MessageProperties.getMessageCount()
对容器发出的消息返回 0
。此属性仅当您使用 basicGet
(例如,来自 RabbitTemplate.receive()
方法)时才适用,现在对容器消息初始化为 null
。
Previously, MessageProperties.getMessageCount()
returned 0
for messages emitted by the container.
This property applies only when you use basicGet
(for example, from RabbitTemplate.receive()
methods) and is now initialized to null
for container messages.
Transaction Rollback Behavior
无论是否配置事务管理器,在事务回滚时重新排队消息现在都是一致的。更多信息,请参阅 A note on Rollback of Received Messages。
Message re-queue on transaction rollback is now consistent, regardless of whether or not a transaction manager is configured. See A note on Rollback of Received Messages for more information.
Shutdown Behavior
如果容器线程在 shutdownTimeout
内没有响应关闭,则默认情况下会强制关闭通道。请参阅 Message Listener Container Configuration 以获取更多信息。
If the container threads do not respond to a shutdown within shutdownTimeout
, the channels are forced closed by default.
See Message Listener Container Configuration for more information.
Connection Factory Changes
连接和通道侦听器接口现在提供一种获取异常信息的机制。更多信息,请参阅 Connection and Channel Listeners 和 Publishing is Asynchronous — How to Detect Successes and Failures。
The connection and channel listener interfaces now provide a mechanism to obtain information about exceptions. See Connection and Channel Listeners and Publishing is Asynchronous — How to Detect Successes and Failures for more information.
现在提供了一个新的 ConnectionNameStrategy
以从 AbstractConnectionFactory
填充目标 RabbitMQ 连接的应用特定标识。有关更多信息,请参见 Connection and Resource Management。
A new ConnectionNameStrategy
is now provided to populate the application-specific identification of the target RabbitMQ connection from the AbstractConnectionFactory
.
See Connection and Resource Management for more information.
Retry Changes
不再提供 MissingMessageIdAdvice
。其功能现在已内置。更多信息,请参阅 Failures in Synchronous Operations and Options for Retry。
The MissingMessageIdAdvice
is no longer provided.
Its functionality is now built-in.
See Failures in Synchronous Operations and Options for Retry for more information.
Anonymous Queue Naming
默认情况下,AnonymousQueues
现在使用默认 Base64UrlNamingStrategy
命名,而不是一个简单的 UUID
字符串。有关详细信息,请参阅 xref:amqp/broker-configuration.adoc#anonymous-queue[AnonymousQueue
。
By default, AnonymousQueues
are now named with the default Base64UrlNamingStrategy
instead of a simple UUID
string.
See AnonymousQueue
for more information.
@RabbitListener
Changes
你现在可以在 @RabbitListener
注解中提供简单的队列声明(仅绑定到默认交换)。有关更多信息,请参阅 Annotation-driven Listener Endpoints。
You can now provide simple queue declarations (bound only to the default exchange) in @RabbitListener
annotations.
See Annotation-driven Listener Endpoints for more information.
你现在可以配置 @RabbitListener
注解,以便将任何异常返回给发送方。你还可以配置 RabbitListenerErrorHandler
来处理异常。有关更多信息,请参阅 Handling Exceptions。
You can now configure @RabbitListener
annotations so that any exceptions are returned to the sender.
You can also configure a RabbitListenerErrorHandler
to handle exceptions.
See Handling Exceptions for more information.
现在可以使用 @QueueBinding
注解绑定具有多个路由键的队列。此外,@QueueBinding.exchange()
现在支持自定义交换类型,并默认声明持久交换。
You can now bind a queue with multiple routing keys when you use the @QueueBinding
annotation.
Also @QueueBinding.exchange()
now supports custom exchange types and declares durable exchanges by default.
现在可以在注解级别设置侦听器容器的 concurrency
,而无需为不同的并发设置配置不同的容器工厂。
You can now set the concurrency
of the listener container at the annotation level rather than having to configure a different container factory for different concurrency settings.
现在可以在注解级别设置侦听器容器的 autoStartup
属性,覆盖容器工厂中的默认设置。
You can now set the autoStartup
property of the listener container at the annotation level, overriding the default setting in the container factory.
现在可以在 RabbitListener
容器工厂中设置接收后和发送前(回复)的 MessagePostProcessor
实例。
You can now set after receive and before send (reply) MessagePostProcessor
instances in the RabbitListener
container factories.
请参阅 Annotation-driven Listener Endpoints 了解更多信息。
See Annotation-driven Listener Endpoints for more information.
从版本 2.0.3 开始,类级 @RabbitListener
上的一个 @RabbitHandler
注解可以被指定为默认值。有关更多信息,请参阅 Multi-method Listeners。
Starting with version 2.0.3, one of the @RabbitHandler
annotations on a class-level @RabbitListener
can be designated as the default.
See Multi-method Listeners for more information.
Container Conditional Rollback
在使用外部事务管理器(如 JDBC)时,现在当你向容器提供事务属性时,支持基于规则的回滚。在使用事务建议时,它现在也更加灵活。更多信息,请参阅 Conditional Rollback。
When using an external transaction manager (such as JDBC), rule-based rollback is now supported when you provide the container with a transaction attribute. It is also now more flexible when you use a transaction advice. See Conditional Rollback for more information.
Remove Jackson 1.x support
弃用在先前版本中,Jackson 1.x
转换器和相关组件现在已删除。你可以使用基于 Jackson 2.x 的类似组件。有关更多信息,请参见 JSON Message Converter。
Deprecated in previous versions, Jackson 1.x
converters and related components have now been deleted.
You can use similar components based on Jackson 2.x.
See JSON Message Converter for more information.
JSON Message Converter
当传入的 JSON 消息的 TypeId
设置为 Hashtable
时,默认转换类型现在为 LinkedHashMap
。以前,它是 Hashtable
。要恢复到 Hashtable
,可以在 DefaultClassMapper
上使用 setDefaultMapType
。
When the TypeId
is set to Hashtable
for an inbound JSON message, the default conversion type is now LinkedHashMap
.
Previously, it was Hashtable
.
To revert to a Hashtable
, you can use setDefaultMapType
on the DefaultClassMapper
.
XML Parsers
在分析 Queue
和 Exchange
XML 组件时,解析器不再将 name
属性值注册为 Bean 别名(如果存在 id
属性)。有关详细信息,请参阅 xref:amqp/broker-configuration.adoc#note-id-name[A Note On the id
和 name
属性。
When parsing Queue
and Exchange
XML components, the parsers no longer register the name
attribute value as a bean alias if an id
attribute is present.
See A Note On the id
and name
Attributes for more information.
Blocked Connection
你现在可以将 com.rabbitmq.client.BlockedListener
注入到 org.springframework.amqp.rabbit.connection.Connection
对象中。此外,当连接被代理阻塞或取消阻塞时,由 ConnectionFactory
发出 ConnectionBlockedEvent
和 ConnectionUnblockedEvent
事件。
You can now inject the com.rabbitmq.client.BlockedListener
into the org.springframework.amqp.rabbit.connection.Connection
object.
Also, the ConnectionBlockedEvent
and ConnectionUnblockedEvent
events are emitted by the ConnectionFactory
when the connection is blocked or unblocked by the Broker.
有关更多信息,请参阅 Connection and Resource Management。
See Connection and Resource Management for more information.