I need to start/stop an Ftp flow depending on the size of the incoming folder. I have a service that verifies the folder size:
@Service
public class IncomingFolderChecker {
private static final int MAX_ALLOWED_SIZE = 2000000;
@Value("${sftp.incoming}")
private String incomingDir;
@Autowired
private MessageChannel controlChannel;
public void checkFolderSize() {
if (FileUtils.sizeOfDirectory(new File(this.incomingDir)) > MAX_ALLOWED_SIZE) {
controlChannel.send(new GenericMessage<>("@sftpInboundAdapter.stop()")); // typo: was 'start()'
} else {
controlChannel.send(new GenericMessage<>("@sftpInboundAdapter.start()"));
}
}
}
I know that the control bus allows to do this. But that's all I know about Spring Integration. How can I hook this up using Java-DSL ?
First of all both your branches of the condition uses the same start()
command. I guess one of them should be stop()
. You code is correct as far as a controlChannel
is an input channel for the Control Bus component. To do that with Java DSL you only need such a simple bean:
@Bean
public IntegrationFlow controlBusFlow() {
return IntegrationFlows.from("controlChannel")
.controlBus()
.get();
}
If that's not a question, please clarify.
UPDATE
Everything together with the Spring Integration style and its Java DSL:
@Bean
public IntegrationFlow controlSftpInboundAdapter(@Value("${sftp.incoming}") String incomingDir) {
return IntegrationFlows
.from(() -> FileUtils.sizeOfDirectory(new File(incomingDir)) > MAX_ALLOWED_SIZE,
e -> e.poller(pollerFactory -> pollerFactory.fixedDelay(1000)))
.<Boolean, String>transform(p -> "@sftpInboundAdapter." + (p ? "start()" : "stop()"))
.controlBus()
.get();
}