Spring Integration manually start/stop channel adapter via control bus

后端 未结 3 1927
长情又很酷
长情又很酷 2020-12-06 06:59

Is there anyway to manually start/init a channel adapter?

I have two pairs of inbound/outbound adapters in my context.xml and would like to decide at runtime which o

相关标签:
3条回答
  • 2020-12-06 07:21

    To achieve this, you need to first set the channel-adapter auto-startup property to false auto-startup="false" and then using control bus start/stop the adapter

    See here control bus example - https://github.com/spring-projects/spring-integration-samples/tree/master/basic/control-bus

    0 讨论(0)
  • 2020-12-06 07:23

    Set autoStartup="false" and either directly start()/stop() them, or use a <control-bus/> (send @myAdapter.start()).

    Getting a direct reference (autowire etc), depends on the endpoint type. If it's a polled endpoint, inject a SourcePollingChannelAdapter; message-driven adapters vary, but generally are a MessageProducerSupport or MessagingGatewaySupport.

    EDIT:

    Read about the control-bus here.

    Give the inbound adapter an id attribute.

    Add <control-bus input-channel="control"/>

    Add <int:gateway service-interface="foo.Controller" default-request-channel="control"/>

    Create a gateway interface

    public interface Controller {
    
        void control(String command);
    
    }
    

    @Autowire the gateway (or use context.getBean(Controller.class)).

    Then, when you are ready to start the adapter, call, e.g. gateway.control("@mqttOut.start()").

    You don't need auto-startup="false" on the outbound adapters.

    However, for a simple use case like this, you might want to investigate using Spring profiles instead (put the adapters in a profile and enable the profile at runtime.

    0 讨论(0)
  • 2020-12-06 07:33

    I was looking for same example using spring integration Java DSL, but haven't found anything, so I've created my own. It shows to be pretty simple to configure.

    @Bean
    public IntegrationFlow controlBus() {
        return IntegrationFlows.from(controlChannel())
                .controlBus()
                .get();
    }
    
    @Bean
    public MessageChannel controlChannel() {
        return MessageChannels.direct().get();
    }
    

    To Stop it:

    controlChannel.send(new GenericMessage<>("@myInboundAdapter.stop()"));

    https://github.com/CauchyPeano/sftp-poller-control-bus

    0 讨论(0)
提交回复
热议问题