我们可以在 Spring 集成中将 header 丰富为 RabbitMQ 的 POJO 吗?

Can we enrich the header as a POJO for RabbitMQ in Spring Integration?

我在 header RabbitMQ 的扩充中使用 POJO。我们可以这样做吗?在丰富 header 之后 queue 的消费者会得到 header 吗?其配置如下:

    <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>

任何 non-standard header 必须由 header-mapper<int-amqp:outbound-channel-adapter> 上提供(或在直接 RabbitTemplate 的情况下由 MessagePostProcessor 提供用法)。以及它们应该在消费者方面被接受 (<int-amqp:inbound-channel-adapter mapped-request-headers="myheader">)。

糟糕的是还不够。 AMQP 协议(​​以及任何有线协议)只能处理标准 object 类型 (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 header 只会像 String 一样被对待。

所以,请确保你的逻辑足够方便。