Search code examples
spring-integrationspring-integration-dsl

Spring Integration DSL how to send messages asynchronously to other internal channels


Now I accept an http request to handle time-consuming tasks. So i want to reply the requester first and then use other flow async to handler it.

I try to use CompletableFuture.runAsync in a handler() method and then use MessageChannel.send to do . But I think there should be a more elegant way

 @Bean
    public IntegrationFlow testFlow(){
        return IntegrationFlows.from("requestChannel")//accept an request
                .handle(handlerSomeThing())//do something
                .handle()?// how to send message to main and reply quickly
                .enrichHeaders(c->c.header(HttpHeaders.STATUS_CODE,HttpStatus.OK))
                .get();
    }

 @Bean
    public IntegrationFlow mainFlow(){
        return IntegrationFlows.from("mainChannel")//accept an request
                .handleMainLogic()//handler main logic

                .get();
    }

Solution

  • User a publishSubscribeChannel.

    See this answer.

    Add a task executor so that the two subflows run in parallel.