我有一个轮询器正在轮询远程目录,以便通过 sftp 传输文件,但如果在 x 次尝试后找不到文件,我想停止它。有一个简单的配置吗?
ApplicationContext.xml
<int-sftp:inbound-channel-adapter id="sftpInboundAdaptor"
session-factory="sftpSessionFactory"
local-directory="${local.dir}"
auto-create-local-directory="true"
auto-startup="false"
channel="SftpChannel"
remote-directory="${remote.dir}"
filename-pattern="XXXX"
delete-remote-files="false"
charset="UTF-8"
remote-file-separator="/"
local-filename-generator-expression="#this">
<int:poller max-messages-per-poll="1" fixed-rate="30000" >
</int:poller>
</int-sftp:inbound-channel-adapter>
Main.class
private static void sftpFile(String party) throws Exception {
SourcePollingChannelAdapter adapter = (SourcePollingChannelAdapter) applicationContext.getBean("sftpInboundAdaptor");
adapter.start();
SftpDownloader sftpProcessor = (SftpDownloader) applicationContext.getBean("sftpDownloader");
LOGGER.info((fileDownloaded ? "Successful" : "Failed") + " downloading file"");
}
SftpDownloader.class
public boolean receiveFile(String party, String fileType) throws SftpJobException {
if (Constants.1.equals(fileType)) {
return isFile1SftpSuccessful();
} else if (Constants.2.equals(fileType)) {
return isFile2SftpSuccessful(party);
}
return false;
}
private boolean isFile1SftpSuccessful() throws SftpJobException {
return isValidFile((File) SftpChannel.receive().getPayload());
}
private boolean isValidFile(File received) throws SftpJobException{
if (received.length() != 0) {
LOGGER.info("File is: {}", received.toString());
return true;
} else {
throw new SftpJobException("File size is 0, either no file exists an empty file was created. ")
}
}
当我查找上述文件(不存在)时,我似乎无限期地轮询,而如果文件不存在,我想抛出异常。