Spring 集成中 Codec 和 MessageConverter 的区别
Difference between Codec and MessageConverter in Spring Integration
Codec 是在 Spring Integration 4.2 中引入的。
但是docs doesn't really describe how a Codec is different from a MessageConverter
中的描述或者在哪些场景下使用哪个抽象?
基本上我想知道的是:
- 为什么
Codec
抽象与 MessageConverter
看起来相似?
- 为什么要使用
Codec
而不是 MessageConverter
,反之亦然?
- 你什么时候会选择使用一个而不是另一个?
此问题已在 Spring Cloud Stream where there is a default Kryo Codec configured but recently there has been work around MessageConverter
's 的上下文中突出显示。
这是一个灰色地带。
MessageConverters
用于Spring 集成在两个方面:
- 将消息的某些外部表示转换为 spring-消息
Message<?>
- 例如to/from 一条 mqtt 消息。
- 实施 datatype on message channels.
另一方面,编解码器仅在将消息放在网络上时处理消息负载(XD 中的 MessageBus
或 Spring Cloud Stream 中的 Binder
)。 Kryo 是 Java 序列化的替代方法。
应用程序通常不会直接处理编解码器,但 Spring 集成提供了一个 CodecMessageConverter
,它在转换时将编解码器带到 encode/decode 有效负载。
它还提供了一个基于编解码器的转换器,因此应用程序可以在流程中的其他地方执行 encoding/decoding(如果需要)。
因此,在 Spring Cloud Stream 的上下文中,Kryo 编解码器用于 encode/decode Binder 中的有效负载。
消息转换器用于使用通道 dataType
功能在 Binder 绑定到传输的应用程序内实现转换。
让我们看一个使用 Spring Cloud DataFlow 的示例:
stream create foo --definition "source | processor --outputType=application/json | sink"
假设源发出一些由处理器接收的 POJO,处理器通常在内部发出一个 Map
对象,但接收器想要接收 JSON,然后是一个 MessageConverter
因为 outputType
声明而为你做的。
源和处理器之间以及处理器和接收器之间的数据作为 kryo 传输。
Codec 是在 Spring Integration 4.2 中引入的。
但是docs doesn't really describe how a Codec is different from a MessageConverter
中的描述或者在哪些场景下使用哪个抽象?
基本上我想知道的是:
- 为什么
Codec
抽象与MessageConverter
看起来相似? - 为什么要使用
Codec
而不是MessageConverter
,反之亦然? - 你什么时候会选择使用一个而不是另一个?
此问题已在 Spring Cloud Stream where there is a default Kryo Codec configured but recently there has been work around MessageConverter
's 的上下文中突出显示。
这是一个灰色地带。
MessageConverters
用于Spring 集成在两个方面:
- 将消息的某些外部表示转换为 spring-消息
Message<?>
- 例如to/from 一条 mqtt 消息。 - 实施 datatype on message channels.
编解码器仅在将消息放在网络上时处理消息负载(XD 中的 MessageBus
或 Spring Cloud Stream 中的 Binder
)。 Kryo 是 Java 序列化的替代方法。
应用程序通常不会直接处理编解码器,但 Spring 集成提供了一个 CodecMessageConverter
,它在转换时将编解码器带到 encode/decode 有效负载。
它还提供了一个基于编解码器的转换器,因此应用程序可以在流程中的其他地方执行 encoding/decoding(如果需要)。
因此,在 Spring Cloud Stream 的上下文中,Kryo 编解码器用于 encode/decode Binder 中的有效负载。
消息转换器用于使用通道 dataType
功能在 Binder 绑定到传输的应用程序内实现转换。
让我们看一个使用 Spring Cloud DataFlow 的示例:
stream create foo --definition "source | processor --outputType=application/json | sink"
假设源发出一些由处理器接收的 POJO,处理器通常在内部发出一个 Map
对象,但接收器想要接收 JSON,然后是一个 MessageConverter
因为 outputType
声明而为你做的。
源和处理器之间以及处理器和接收器之间的数据作为 kryo 传输。