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
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
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.
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