Search code examples
spring-integrationspring-integration-dsl

Multiple Independent IntegrationFlow


Is below the correct way to configure multiple independent IntegrationFlows in the same Spring Boot application? Is there any more optimization that could be done?

@Bean("flow1")
public IntegrationFlow integrationFlow1() {

    return IntegrationFlows.from(jdbcMessageSource1(), p -> p.poller(pollerSpec1()))
                            .split()
                            .channel(c -> c.executor(Executors.newCachedThreadPool()))
                            .transform(transformer1, "transform")
                            .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
                            .handle(Http.outboundGateway(url1)
                                        .httpMethod(HttpMethod.POST)
                                        .expectedResponseType(String.class)
                                        .requestFactory(requestFactory))
                            .get();
}

@Bean("flow2")
public IntegrationFlow integrationFlow2() {

    return IntegrationFlows.from(jdbcMessageSource2(), p -> p.poller(pollerSpec2()))
                            .split()
                            .channel(c -> c.executor(Executors.newCachedThreadPool()))
                            .transform(transformer2, "transform")
                            .enrichHeaders(headerEnricherSpec -> headerEnricherSpec.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
                            .handle(Http.outboundGateway(url2)
                                        .httpMethod(HttpMethod.POST)
                                        .expectedResponseType(String.class)
                                        .requestFactory(requestFactory))
                            .get();
}

Solution

  • What you have so far is fully OK and legit.

    If your flows don't share any common logic, then have the structure similar is expected. On the other hand even if they look similar at the moment, it doesn't mean that their logic (one or both) might not be changed in the future. Of course it is much safer to divide your business logic into separate microservices, but that's not wrong to have several units of work in the same application.

    You may need to pay attention that shared ThreadPoolTaskScheduler in Spring Boot has only one thread by default. So, to support those polling flows in parallel you might increase the pool config: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.task-execution-and-scheduling