Search code examples
springjava-8spring-integrationmessagingdsl

Spring Integration DSL: wont execute other components after executing routeToRecipients


Upon running the application, the flow stops at routeToRecipients and other components were not executed. (See the inline comments in the code below) No error occured.

But when I remove the routeToRecipients(), other components execute.

Is there something wrong with my integration flow?

@MessagingGateway
public interface gateway {
    @Gateway(requestChannel = "request.input")
    void process(List<Msg> test);
}

@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
    return Pollers.fixedDelay(1000).get();
}

@Bean
public IntegrationFlow flow() {
    return IntegrationFlows.from("request.input")
            .split()
            .channel(MessageChannels.executor(Executors.newCachedThreadPool()))
            .transform(this.transformer::transform)
            .routeToRecipients(r -> 
                    r.recipient("channel1")
                     .recipient("channel2")
                     .recipient("channel3")
                     .recipient("channel4"))
            .transform(this.transformer::transform2) // <---this is not executed :(
            .handle(new GenericHandler<Msg>() {     // <---this too is not executed
                @Override
                public Object handle(Msg payload, Map<String, Object> headers) {
                    System.out.println("test service activator!");
                    return null;
                }
            })
            .get();
}


@Bean
public IntegrationFlow flow1() {
    return IntegrationFlows.from("channel1")
            .handle(new GenericHandler<Msg>() {
                @Override
                public Object handle(Msg payload, Map<String, Object> headers) {
                    System.out.println("Test route Channel 1")
                    return payload;
                }
            })
            .channel("output")
            .get();
}

@Bean
public IntegrationFlow flow2() {
    return IntegrationFlows.from("channel2")
            .handle(new GenericHandler<Msg>() {
                @Override
                public Object handle(Msg payload, Map<String, Object> headers) {
                    System.out.println("Test route Channel 2")
                    return payload;
                }
            })
            .channel("output")
            .get();
}

@Bean
public IntegrationFlow flow3() {
    return IntegrationFlows.from("channel3")
            .handle(new GenericHandler<Msg>() {
                @Override
                public Object handle(Msg payload, Map<String, Object> headers) {
                    System.out.println("Test route Channel 3")
                    return payload;
                }
            })
            .channel("output")
            .get();
}

    @Bean
public IntegrationFlow flow4() {
    return IntegrationFlows.from("channel4")
            .handle(new GenericHandler<Msg>() {
                @Override
                public Object handle(Msg payload, Map<String, Object> headers) {
                    System.out.println("Test route Channel 4")
                    return payload;
                }
            })
            .channel("output")
            .get();
}

Solution

  • A router does not have an output channel - it only sends messages to the recipients.

    You can either put a publish/subscribe channel before the router with the router being one subscriber and the remaining flow the second subscriber.

    Or add channel5 and start the remaining flow (in a new flow) with that channel.