2
votes

I am trying to implement the routing slip using spring integration, The configuration is as follows

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

}

And

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


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

In order to consult the routing slip after header enricher I have added

@BridgeTo

This implementation works, but works once in 2 times, after debugging the spring code I stumbled into this code and figured out the problem.

In UnicastingDispatcher Class

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

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

And it is picking in round robin basis. So once it is picking the BridgeHandler and JmsSendingMessageHandler the next.

Is there any configuration I have missed to consult the BridgeHandler all the time?

1

1 Answers

2
votes

It looks like you have two subscribers on the channel.

It is normal to round robin dispatch when there are multiple consumers on a channel.