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

Has*_*mad 8 java spring spring-integration

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

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

编辑:

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

<int-mqtt:message-driven-channel-adapter 
    client-id="foo"
    auto-startup="true"
    url="tcp://192.168.97.164:1883"
    topics="testtopic/#"
    channel="writeToFile" />

<file:outbound-channel-adapter
    id="writeToFile"
    auto-startup="true"
    directory="./test/out"
    delete-source-files="false"/>

<int:transformer id="Transformer"
    ref="MessageTransformer"
    input-channel="readFromFile"
    output-channel="mqttOut"
    method="bytesFromFile" />

<bean id="MessageTransformer" class="MessageTransformer"/>

<int-mqtt:outbound-channel-adapter 
    id="mqttOut"
    client-id="foo"
    url="tcp://192.168.97.164:1883"
    auto-startup="false"
    default-qos="1"
    default-retained="true"
    default-topic="testtopic/bla"
    />

    <file:inbound-channel-adapter
    auto-startup="false" 
    id="readFromFile"
    directory="./test/in"
    filename-pattern="myFile*">
    <int:poller id="poller"
        fixed-rate="5000" />     
</file:inbound-channel-adapter>
Run Code Online (Sandbox Code Playgroud)


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

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

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

Gar*_*ell 15

设置autoStartup="false"并直接start()/ stop()他们,或使用<control-bus/>(发送@myAdapter.start()).

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

编辑:

在这里阅读控制总线.

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

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

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

创建网关接口

public interface Controller {

    void control(String command);

}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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