[英]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.