Java Spring Integration 通过控制总线手动启动/停止通道适配器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23915524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Integration manually start/stop channel adapter via control bus
提问by Hasib. A. Samad
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 one of them I want to get started.
我的 context.xml 中有两对入站/出站适配器,我想在运行时决定要开始使用其中的哪一个。
EDIT:
编辑:
The concrete scenario:
I have a client, that can be configured at runtime to be an mqtt publisher or subscriber.
My context.xml looks like this:
具体场景:
我有一个客户端,可以在运行时配置为 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>
As you can see, I have two settings:
1. Subscriber case: Read mqtt message -> Write to file
2. Publisher case: Poll a file from directory -> Send via mqtt
如您所见,我有两个设置:
1. 订阅者案例:读取 mqtt 消息 -> 写入文件
2. 发布者案例:从目录中轮询文件 -> 通过 mqtt 发送
I decide at runtime what setting is to be applied.
我在运行时决定要应用什么设置。
So can you kindly tell me how this control-bus thing would fit here exactly?
所以你能告诉我这个控制总线的东西到底如何适合这里吗?
采纳答案by Gary Russell
Set autoStartup="false"
and either directly start()
/stop()
them, or use a <control-bus/>
(send @myAdapter.start()
).
设置autoStartup="false"
并直接start()
/stop()
他们,或使用<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
.
获取直接引用(自动装配等)取决于端点类型。如果是轮询端点,则注入一个SourcePollingChannelAdapter
; 消息驱动的适配器各不相同,但通常是MessageProducerSupport
或MessagingGatewaySupport
。
EDIT:
编辑:
Read about the control-bus here.
Give the inbound adapter an id
attribute.
给入站适配器一个id
属性。
Add <control-bus input-channel="control"/>
添加 <control-bus input-channel="control"/>
Add <int:gateway service-interface="foo.Controller" default-request-channel="control"/>
添加 <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)
).
@Autowire
网关(或使用context.getBean(Controller.class)
)。
Then, when you are ready to start the adapter, call, e.g. gateway.control("@mqttOut.start()")
.
然后,当您准备好启动适配器时,请调用,例如gateway.control("@mqttOut.start()")
。
You don't need auto-startup="false"
on the outbound adapters.
您不需要auto-startup="false"
出站适配器。
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.
但是,对于像这样的简单用例,您可能希望改为使用 Spring 配置文件进行调查(将适配器放在一个配置文件中并在运行时启用该配置文件。
回答by Rajesh Gheware
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
为此,您需要首先将通道适配器自动启动属性设置为 false auto-startup="false"
,然后使用控制总线启动/停止适配器
See here control bus example - https://github.com/spring-projects/spring-integration-samples/tree/master/basic/control-bus
请参阅此处的控制总线示例 - https://github.com/spring-projects/spring-integration-samples/tree/master/basic/control-bus
回答by Igor Konoplyanko
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.
我正在寻找使用 spring 集成 Java DSL 的相同示例,但没有找到任何东西,所以我创建了自己的示例。它显示配置非常简单。
@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()"));
controlChannel.send(new GenericMessage<>("@myInboundAdapter.stop()"));