Search code examples
spring-integrationspring-integration-dsl

How to subscribe different channels by a specification?


I am looking for a switching operator in Spring IntegrationFlows. I need to subscribe to a channel based on the property that user can decide.

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows
                .from()
                .publishSubscribeChannel(
                        subscription -> {
                            if (true)//user is going to decide this.
                                subscription.subscribe(flow1());
                            else
                                subscription.subscribe(flow2());
                        }
                )
                .get();
    }

This is my basic implementation, but it doesn't work. Is a feature for that ?


Solution

  • If we talk about an application.properties, then there is nothing more than a conditions in configuration. See a @Value injection into a bean definition method: https://docs.spring.io/spring/docs/5.2.6.RELEASE/spring-framework-reference/core.html#beans-value-annotations

    So, something like this for your flow bean:

    @Bean
    public IntegrationFlow flow(Value("${your.prop}") String propValue) {
    

    Than you indeed can do that if...else insider a subscription lambda.

    Another way is to use a @ConditionalOnProperty from Spring Boot and have those flow1() and flow2() be as conditional bean.

    Then you can inject an IntegrationFlow into this your flow() bean - same method argument injection technique, - and only one of conditional beans is going to be available for you.

    In two words: everything what you can do with Spring dependency injection can be used when you declare IntegrationFlow beans.