Spring Integration通过控制总线手动启动/停止通道适配器

反正手动启动/初始化通道适配器?

我在context.xml中有两对入站/出站适配器,并希望在运行时决定我想要启动哪一个。

编辑:

具体方案:
我有一个客户端,可以在运行时配置为mqtt发布者或订阅者。
我的context.xml看起来像这样:

        

如您所见,我有两个设置:
1.订户案例:读取mqtt消息 – >写入文件
2.发布者案例:从目录中轮询文件 – >通过mqtt发送

我在运行时决定应用什么设置。

那么你能告诉我这个控制总线的东西究竟适合这里吗?

设置autoStartup="false"并直接start() / stop()它们,或使用 (发送@myAdapter.start() )。

获得直接引用(autowire等)取决于端点类型。 如果是轮询端点,请注入SourcePollingChannelAdapter ; 消息驱动的适配器各不相同,但通常是MessageProducerSupportMessagingGatewaySupport

编辑:

在这里阅读控制总线 。

为入站适配器提供id属性。

添加

添加

创建网关接口

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

@Autowire网关(或使用context.getBean(Controller.class) )。

然后,当您准备启动适配器时,请调用,例如gateway.control("@mqttOut.start()")

您不需要在出站适配器上auto-startup="false"

但是,对于这样的简单用例,您可能希望调查使用Spring配置文件(将适配器放在配置文件中并在运行时启用配置文件)。

要实现这一点,您需要先将通道适配器自动启动属性设置为false auto-startup="false"然后使用控制总线启动/停止适配器

请参阅此处控制总线示例 – https://github.com/spring-projects/spring-integration-samples/tree/master/basic/control-bus

我正在寻找使用Spring集成Java DSL的相同示例,但没有找到任何东西,所以我创建了自己的。 它显示配置非常简单。

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

要阻止它:

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

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