带有消息编号的骆驼标头



使用apache骆驼,我想用不同的路由键发送到rabbitmq交换消息以进行负载平衡(现在我将使用4个路由键进行交换,将来更多)。是否有一种简单的方法可以添加不同的标头(路由键.setheader(" rabbitmq.routing_key",envelope.getRoutingKey());)到消息?

更新:

i解决了处理器和$ {id}的问题:

.setHeader("id", simple("${id}"))
.process(new Processor() {
    public void process(Exchange exchange) throws Exception {
    String id = exchange.getIn().getHeader("id").toString();
    String newRoutingKey = ROUTING_KEY_PREFIX +
            (Integer.valueOf(id.split(":")[MESSAGE_NUMBER_IND]) % ROUTING_KEYS_NUMBER);
    exchange.getIn().removeHeader("id");
    exchange.getIn().setHeader("rabbitmq.ROUTING_KEY", newRoutingKey);
} }).to(rmqQueue)

有任何隐藏问题吗?

您可以使用TOD动态设置路由键。

xml语法:

<toD uri="rabbitmq://hostname[:port]/exchangeName?routingKey=**${header.routekey}**"/>

header.Routekey是您打算使用的动态键,并且设置在标题中。

在Java DSL的情况下,语法可能看起来像:

.tod(" rabbitmq://hostName [:port]/fressangekey?rutingkey = $ {header.routekey} ");

最新更新