Search code examples
spring-bootspring-integration

Correct way to start InboundChannelAdapter


I am using annotation based

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


@Bean
@InboundChannelAdapter(value = "channel",poller = @Poller(fixedDelay="1000"))
public MessageSource<?> inbound() {}

works perfectly.

But now I dont want it to start automatically , what is the correct way to start/stop?
p.s.
I am not using IntegrationFlow.


Solution

  • See @InboundChannelAdapter attributes:

    /**
     * {@code SmartLifecycle} options.
     * Can be specified as 'property placeholder', e.g. {@code ${foo.autoStartup}}.
     * @return if the channel adapter is started automatically or not.
     */
    String autoStartup() default "true";
    

    So, with the false it is not going to start automatically. You would need to autowire the SourcePollingChannelAdapter bean somewhere in your services to start() it manually.

    See @EndpointId annotation to place alongside with that @InboundChannelAdapter.

    Or you can use a Control Bus pattern to perform start() command:

    https://docs.spring.io/spring-integration/docs/current/reference/html/system-management.html#control-bus

    https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotations

    There won't be too much difference with Java DSL, unless you wouldn't need to search for @EndpointId since there is just obvious id() on the EdnpointSpec exposed along side with the poller().