Modifying Messages - Compression and More

存在一些扩展点。它们允许您在消息发送到 RabbitMQ 之前或接收后立即对消息执行某些处理。

A number of extension points exist. They let you perform some processing on a message, either before it is sent to RabbitMQ or immediately after it is received.

Message Converters中所示,一个这样的扩展点在 AmqpTemplate``convertAndReceive`操作中,你可以在其中提供 `MessagePostProcessor。例如,在你的 POJO 转换后,`MessagePostProcessor`允许你在 `Message`上设置自定义头或属性。

As can be seen in Message Converters, one such extension point is in the AmqpTemplate convertAndReceive operations, where you can provide a MessagePostProcessor. For example, after your POJO has been converted, the MessagePostProcessor lets you set custom headers or properties on the Message.

从 1.4.2 版开始,已向 RabbitTemplate - setBeforePublishPostProcessors()setAfterReceivePostProcessors() 添加了其他扩展点。第一个使后处理器能够在发送至 RabbitMQ 之前立即运行。在使用批处理时(请参阅 Batching),这会在组装批处理之后并且在发送批处理之前被调用。第二个在收到消息后立即被调用。

Starting with version 1.4.2, additional extension points have been added to the RabbitTemplate - setBeforePublishPostProcessors() and setAfterReceivePostProcessors(). The first enables a post processor to run immediately before sending to RabbitMQ. When using batching (see Batching), this is invoked after the batch is assembled and before the batch is sent. The second is invoked immediately after a message is received.

这些扩展点用于诸如压缩之类的功能,为此提供了几个 MessagePostProcessor 实现。GZipPostProcessorZipPostProcessorDeflaterPostProcessor 在发送之前压缩消息,而 GUnzipPostProcessorUnzipPostProcessorInflaterPostProcessor 解压缩收到的消息。

These extension points are used for such features as compression and, for this purpose, several MessagePostProcessor implementations are provided. GZipPostProcessor, ZipPostProcessor and DeflaterPostProcessor compress messages before sending, and GUnzipPostProcessor, UnzipPostProcessor and InflaterPostProcessor decompress received messages.

从 2.1.5 版开始,GZipPostProcessor 可以使用 copyProperties = true 选项配置,以创建原始消息属性的副本。默认情况下,出于性能原因会重复使用这些属性,并使用压缩内容编码和可选的 MessageProperties.SPRING_AUTO_DECOMPRESS 标头对其进行修改。如果你保留对原始出站消息的引用,其属性也会随之改变。因此,如果你的应用程序使用这些消息后处理器保留出站消息的副本,请考虑打开 copyProperties 选项。

Starting with version 2.1.5, the GZipPostProcessor can be configured with the copyProperties = true option to make a copy of the original message properties. By default, these properties are reused for performance reasons, and modified with compression content encoding and the optional MessageProperties.SPRING_AUTO_DECOMPRESS header. If you retain a reference to the original outbound message, its properties will change as well. So, if your application retains a copy of an outbound message with these message post processors, consider turning the copyProperties option on.

从 2.2.12 版开始,你可以配置压缩后处理器在内容编码元素之间使用的分隔符。在 2.2.11 及更早版本中,这是硬编码为 :,现在将其设置为压缩器上的 , ` by default. The decompressors will work with both delimiters. However, if you publish messages with 2.3 or later and consume with 2.2.11 or earlier, you MUST set the `encodingDelimiter 属性 :。当你的消费者升级到 2.2.11 或更高版本时,你可以恢复为 `、`的默认值。

Starting with version 2.2.12, you can configure the delimiter that the compressing post processors use between content encoding elements. With versions 2.2.11 and before, this was hard-coded as :, it is now set to , ` by default. The decompressors will work with both delimiters. However, if you publish messages with 2.3 or later and consume with 2.2.11 or earlier, you MUST set the `encodingDelimiter property on the compressor(s) to :. When your consumers are upgraded to 2.2.11 or later, you can revert to the default of `, `.

同样,SimpleMessageListenerContainer 也具有 setAfterReceivePostProcessors() 方法,允许在容器收到消息后执行解压缩。

Similarly, the SimpleMessageListenerContainer also has a setAfterReceivePostProcessors() method, letting the decompression be performed after messages are received by the container.

从 2.1.4 版开始,addBeforePublishPostProcessors()addAfterReceivePostProcessors() 已被添加到 RabbitTemplate 中,以允许将新后处理器分别附加到发布前和接收后后处理器的列表中。此外,还提供了移除后处理器的相关方法。同样,AbstractMessageListenerContainer 也添加了 addAfterReceivePostProcessors()removeAfterReceivePostProcessor() 方法。有关更多详细信息,请参阅 RabbitTemplateAbstractMessageListenerContainer 的 JavaDoc。

Starting with version 2.1.4, addBeforePublishPostProcessors() and addAfterReceivePostProcessors() have been added to the RabbitTemplate to allow appending new post processors to the list of before publish and after receive post processors respectively. Also there are methods provided to remove the post processors. Similarly, AbstractMessageListenerContainer also has addAfterReceivePostProcessors() and removeAfterReceivePostProcessor() methods added. See the Javadoc of RabbitTemplate and AbstractMessageListenerContainer for more detail.