Spring集成路由滑动

时间:2016-05-24 05:46:20

标签: spring spring-integration spring-messaging

我正在尝试使用spring集成来实现路由滑动,配置如下

@Bean
@Transformer(inputChannel = "inputChannel", outputChannel = "replyChannel")
public HeaderEnricher headerEnricher() {
    return new HeaderEnricher(Collections.singletonMap(IntegrationMessageHeaderAccessor.ROUTING_SLIP,
            new RoutingSlipHeaderValueMessageProcessor("channel1", "channel2", "channel3"
                    , "channel4"
            )));

}

并且

 @Bean
public MessageChannel inputChannel() {
    return new DirectChannel();
}


@Bean
@BridgeTo
public MessageChannel replyChannel() {
    return new DirectChannel();
}

为了在标题丰富之后查阅路由单我已添加

@BridgeTo

这个实现工作正常,但是在调试弹簧代码之后,它只运行了两次,我偶然发现了这个代码并找出了问题。

在UnicastingDispatcher类

private boolean doDispatch(Message<?> message) {
    if (this.tryOptimizedDispatch(message)) {
        return true;
    }
    boolean success = false;
    Iterator<MessageHandler> handlerIterator = this.getHandlerIterator(message);
    if (!handlerIterator.hasNext()) {
        throw new MessageDispatchingException(message, "Dispatcher has no subscribers");
    }
    List<RuntimeException> exceptions = new ArrayList<RuntimeException>();
    while (!success && handlerIterator.hasNext()) {
        MessageHandler handler = handlerIterator.next();
        try {
            handler.handleMessage(message);
            success = true; // we have a winner.
        }
        catch (Exception e) {
            RuntimeException runtimeException = this.wrapExceptionIfNecessary(message, e);
            exceptions.add(runtimeException);
            this.handleExceptions(exceptions, message, !handlerIterator.hasNext());
        }
    }
    return success;
}


Iterator<MessageHandler> handlerIterator = this.getHandlerIterator(message); 

给出 [org.springframework.integration.jms.JmsSendingMessageHandler#11,routingSlip.replyChannel.bridgeTo.handler]

它正在以循环方式挑选。 所以,一旦它选择了BridgeHandler和JmsSendingMessageHandler,那么它就是。

我是否有任何配置错过了一直咨询BridgeHandler?

1 个答案:

答案 0 :(得分:1)

看起来你在频道上有两个订阅者。

当频道上有多个消费者时,循环调度是正常的。