public class SftpConfiguration {
public DefaultSftpSessionFactory sftpFactory() {
DefaultSftpSessionFactory factory=new DefaultSftpSessionFactory();
factory.setHost("*******");
factory.setPort(22);
factory.setAllowUnknownKeys(true);
factory.setUser("ishant");
factory.setPassword("**********");
return factory;
}
@Bean(name="mydefaultsync")
public SftpInboundFileSynchronizer synchronizer(){
SftpInboundFileSynchronizer sync = new SftpInboundFileSynchronizer(sftpFactory());
sync.setDeleteRemoteFiles(true);
sync.setRemoteDirectory("C:/Users/ishant/Downloads");
sync.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
sync.setPreserveTimestamp(true);
sync.
return sync;
}
@Bean(name="sftpMessageSource")
@InboundChannelAdapter(channel="fileuploaded", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> sftpMessageSource(){
SftpInboundFileSynchronizingMessageSource source =
new SftpInboundFileSynchronizingMessageSource(synchronizer());
source.setLocalDirectory(new File("tmp/incoming"));
source.setAutoCreateLocalDirectory(true);
source.setMaxFetchSize(1);
System.out.println(source);
return source;
}
@ServiceActivator(inputChannel = "fileuploaded")
public void handleIncomingFile(File file) throws IOException {
log.info(String.format("handleIncomingFile BEGIN %s", file.getName()));
String content = FileUtils.readFileToString(file, "UTF-8");
log.info(String.format("Content: %s", content));
log.info(String.format("handleIncomingFile END %s", file.getName()));
}
}
1条答案
按热度按时间kq0g1dla1#
框架中有一个,但它本身。
参见
org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer
。它发出以下日志消息:因此,每次当没有文件从远程目录传输时,您都会在日志中看到一条消息,其中包含
0
。在
org.springframework.integration.endpoint.AbstractPollingEndpoint
中还有一个:当没有新数据要生成时,在计划的轮询任务上发出。
您还可以查看
ReceiveMessageAdvice
实现的afterReceive()
方法,只需检查result
上的null
并发出相应的日志消息。