java 如何可以添加日志每次轮询结果在没有检测到的文件.我想添加一个日志时,轮询结果为空每次

tktrz96b  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(134)
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()));
    }
}
kq0g1dla

kq0g1dla1#

框架中有一个,但它本身。
参见org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer。它发出以下日志消息:

if (this.logger.isDebugEnabled()) {
            this.logger.debug(transferred + " files transferred from '" + remoteDirectory + "'");
        }

因此,每次当没有文件从远程目录传输时,您都会在日志中看到一条消息,其中包含0
org.springframework.integration.endpoint.AbstractPollingEndpoint中还有一个:

if (message == null) {
        this.logger.debug("Received no Message during the poll, returning 'false'");
        return null;
    }

当没有新数据要生成时,在计划的轮询任务上发出。
您还可以查看ReceiveMessageAdvice实现的afterReceive()方法,只需检查result上的null并发出相应的日志消息。

相关问题