我的要求是从我的 spring-batch 应用程序的文件服务器中的远程目录下载多个文件。此处的示例之一 建议使用 SourcePollingChannelAdapter。在此示例中,适配器已启动 adapter.start() 但并未停止。那么它的生命周期是如何工作的呢?我的要求是首先下载&只有当所有文件都下载后,然后继续阅读文件。但这似乎是下载文件的一种异步过程。那么,当所有文件都已下载并准备好继续进行时,我将如何获得通知?我没有看到任何其他方法可以将任何处理程序注入适配器/可轮询通道。
使用的示例配置:
<int-sftp:inbound-channel-adapter id="sftpInbondAdapterBean"
auto-startup="false" channel="receiveChannelBean" session-factory="sftpSessionFactory"
local-directory="${sftp.download.localDirResource}"
remote-directory="${sftp.download.remoteFilePath}"
auto-create-local-directory="true" delete-remote-files="false"
filename-regex=".*\.csv$">
<int:poller fixed-rate="5000" max-messages-per-poll="3" />
</int-sftp:inbound-channel-adapter>
<int:channel id="receiveChannelBean">
<int:queue/>
</int:channel>
如果显式调用了adapter.stop(),它会抛出InterruptedException。如果未调用 stop ,则文件以异步方式下载而不会阻塞,因此下一步不知道下载是否已完成或正在进行中。
编辑:DownloadingTasklet 的代码片段
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
try {
sftpInbondAdapter.start();
Thread.sleep(15000); // ---Is this mandatory ?
} catch (Exception e) {
throw new BatchClientException("Batch File Download was un-successful !", e);
} finally {
// sftpInbondAdapter.stop();
Logger.logDebug("BATCH_INPUT_FILE_DOWNLOAD", "COMPLETED");
}
return RepeatStatus.FINISHED;
}