[英]Spring integration - stop polling if no file returned
我有一个轮询器正在轮询远程目录,以便 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. ")
}
}
当我查找上述文件(不存在)时,我似乎会无限期地进行轮询,而如果该文件不存在,我想抛出异常。
请参阅智能轮询- 您可以检测到缺少消息并停止轮询器。
4.2 版引入了 AbstractMessageSourceAdvice。 建议链中的任何 Advice 对象都是此类的子类,仅应用于接收操作。 这些类实现以下方法:
beforeReceive(MessageSource<?> source)
此方法在 MessageSource.receive() 方法之前调用。 它使您可以在此时检查和/或重新配置源。 返回 false 将取消此轮询(类似于上面提到的 PollSkipAdvice)。
Message<?> afterReceive(Message<?> result, MessageSource<?> source)
这个方法在receive()方法之后调用; 同样,您可以重新配置源,或者根据结果采取任何操作(如果源没有创建消息,则可以为空)。 您甚至可以返回不同的消息!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.