【问题标题】:Can we enrich the header as a POJO for RabbitMQ in Spring Integration?我们可以在 Spring Integration 中将标头丰富为 RabbitMQ 的 POJO 吗?
【发布时间】:2026-01-03 12:05:04
【问题描述】:
我在 RabbitMQ 的标头丰富中使用 POJO。我们可以那样做吗?丰富标题后,队列的消费者会得到那个标题吗?其配置如下:
<integration:chain input-channel="queeChannel" >
<integration:header-enricher>
<integration:header name="myheader" ref="myBean" method="myMethod"></integration:header>
</integration:header-enricher>
<integration:recipient-list-router
apply-sequence="true">
<integration:recipient channel="mqChannel"/>
</integration:recipient-list-router>
</integration:chain>
【问题讨论】:
标签:
spring-integration
spring-amqp
【解决方案1】:
任何非标准标头必须由header-mapper 在<int-amqp:outbound-channel-adapter> 上提供(或者在直接使用RabbitTemplate 的情况下与MessagePostProcessor 一起提供)。以及它们应该在消费者方面被接受 (<int-amqp:inbound-channel-adapter mapped-request-headers="myheader">)。
有什么不好的还不够。 AMQP 协议(以及任何有线协议)只能处理标准对象类型(DefaultMessagePropertiesConverter):
boolean valid = (value instanceof String) || (value instanceof byte[]) || (value instanceof Boolean)
|| (value instanceof LongString) || (value instanceof Integer) || (value instanceof Long)
|| (value instanceof Float) || (value instanceof Double) || (value instanceof BigDecimal)
|| (value instanceof Short) || (value instanceof Byte) || (value instanceof Date)
|| (value instanceof List) || (value instanceof Map);
if (!valid && value != null) {
value = value.toString();
}
因此,您的 POJO 标头将仅被视为 String。
所以,请确保您的逻辑足够方便。