SFTP @Poller不触发轮询没有任何反应

时间:2019-04-11 20:01:11

标签: spring-integration spring-batch spring-integration-sftp

我正在尝试设置将合并csv的spring boot应用程序。我在春季启动应用程序或filezilla SFTP服务器上都没有看到任何活动,但是如果我将相同的代码更改为FTP,那么它将起作用


  @Component
    @EnableIntegration
    public class IntegrationConfiguration {
        @Autowired
        FTPConfigProperties ftpConfigProperties;
        @Autowired
        private BeanFactory beanFactory;
        @Value("classpath:certificate.crt")
        Resource certficateFile;
        @Bean
        public SessionFactory<ChannelSftp.LsEntry> ftpSessionFactory() {
            DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
            factory.setHost("127.0.0.1");
            factory.setPort(990);
            factory.setUser("abhinav");
            factory.setPassword("nssdw");

            factory.setPrivateKey(certficateFile);
            factory.setAllowUnknownKeys(true);
            return new CachingSessionFactory<ChannelSftp.LsEntry>(factory, 100000);
        }

        @Bean
        public SftpInboundFileSynchronizer ftpInboundFileSynchronizer() {
            SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(ftpSessionFactory());
            fileSynchronizer.setDeleteRemoteFiles(false);
            fileSynchronizer.setRemoteDirectory("/");
            fileSynchronizer.setFilter(filter());
            fileSynchronizer.setDeleteRemoteFiles(false);
            fileSynchronizer.setPreserveTimestamp(true);
            fileSynchronizer.setBeanFactory(beanFactory);
            return fileSynchronizer;
        }
    //here the poller is configured 
        @Bean
        @InboundChannelAdapter(channel = "fromSftpChannel", poller = @Poller(fixedDelay = "10000"))
        public MessageSource<File> ftpMessageSource() throws Exception {
            SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
                    ftpInboundFileSynchronizer());
            source.setLocalDirectory(new File("ftp-inbound"));
            source.setAutoCreateLocalDirectory(true);
            source.setMaxFetchSize(1);
            source.setBeanFactory(beanFactory);
            source.setUseWatchService(true);
            return source;
        }

        public CompositeFileListFilter<ChannelSftp.LsEntry> filter() {
            CompositeFileListFilter<ChannelSftp.LsEntry> filter = new CompositeFileListFilter<ChannelSftp.LsEntry>();
            filter.addFilter(new SftpSimplePatternFileListFilter("*.csv"));
            filter.addFilter(acceptOnceFilter());
            filter.addFilter(new LastModifiedLsEntryFileListFilter());
            return filter;
        }

        @Bean
        public SftpPersistentAcceptOnceFileListFilter acceptOnceFilter() {
            SftpPersistentAcceptOnceFileListFilter filter = new SftpPersistentAcceptOnceFileListFilter(metadataStore(),"ftpPersistentAcceptOnce");
            filter.setFlushOnUpdate(true);
            return filter;
        }


        @Bean
        public ConcurrentMetadataStore metadataStore() {
            PropertiesPersistingMetadataStore propertiesPersistingMetadataStore = new PropertiesPersistingMetadataStore();
            propertiesPersistingMetadataStore.setBaseDirectory("./metastore");
            propertiesPersistingMetadataStore.setFileName("ftpStream.properties");
            return propertiesPersistingMetadataStore;
        }

        @Bean
        @ServiceActivator(inputChannel = "jobChannel", outputChannel = "nullChannel")
        protected JobLaunchingMessageHandler launcher(JobLauncher jobLauncher) {
            return new JobLaunchingMessageHandler(jobLauncher);
        }
    }

在下一个调用中,我触发弹簧批处理,然后转到维修执行器

@Component
public class FileToJobTransformer implements ApplicationContextAware {
    private ApplicationContext context;
   @Autowired
    private Job job;



    @Transformer(inputChannel = "fromSftpChannel", outputChannel = "jobChannel")
    public JobLaunchRequest transform(File aFile) throws Exception {
        String fileName = aFile.getName();
        JobParameters jobParameters = new JobParametersBuilder().addString(
                "input.file", aFile.getAbsolutePath()).toJobParameters();
        JobLaunchRequest request = new JobLaunchRequest(job, jobParameters);
        return request;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}

客户代码如下


public class LastModifiedLsEntryFileListFilter implements FileListFilter<ChannelSftp.LsEntry> {

    private static final long DEFAULT_AGE = 60;

    private volatile long age = DEFAULT_AGE;

    public long getAge() {
        return this.age;
    }

    public void setAge(long age) {
        setAge(age, TimeUnit.SECONDS);
    }

    public void setAge(long age, TimeUnit unit) {
        this.age = unit.toSeconds(age);
    }

    @Override
    public List<ChannelSftp.LsEntry> filterFiles(ChannelSftp.LsEntry[] files) {

        System.out.println("files = [" + files.length + "]");

        List<ChannelSftp.LsEntry> list = new ArrayList<ChannelSftp.LsEntry>();

        long now = System.currentTimeMillis() / 1000;

        for (ChannelSftp.LsEntry file : files) {
            if (file.getAttrs()
                    .isDir()) {
                continue;
            }
            int lastModifiedTime = file.getAttrs()
                    .getMTime();

            if (lastModifiedTime + this.age <= now) {
                list.add(file);
            }
        }

        Collections.reverse(list);
      ArrayList<ChannelSftp.LsEntry> oneElementList = new  ArrayList<ChannelSftp.LsEntry>(1) ;
      oneElementList.add(list.get(0));
        return oneElementList;
    }

}


0 个答案:

没有答案