Spring Integration устанавливает удаленный каталог SFTP как ноль - PullRequest
2 голосов
/ 21 мая 2019

Я создал тасклет для загрузки файла на SFTP-сервер с помощью Spring-Integration-Batch. При запуске пакета кажется, что удаленный каталог не настроен правильно в AbstractInboundFileSynchronizer, из-за чего программа пытается синхронизировать ноль с локальным каталогом.

Когда я попытался вызвать afterPropertiesSet () AbstractInboundFileSynchronizer непосредственно перед synchronizeToLocalDirectory (), он загрузил файлы с предупреждением «No beanFactory».

Ниже приведен мой тасклет:

public class FtpFileDownloadTasklet implements Tasklet {

    @Value("${ftp.source.directory}")
    private String remoteDirectory;

    @Value("${ftp.dest.directory}")
    private String localDirectory;

    @Value("${ftp.source.file.extn}")
    private String sourceFileExtn;

    @Autowired
    private SessionFactory<LsEntry> sftpSessionFactory;

    private SftpInboundFileSynchronizer fileSynchronizer;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        if (sftpSessionFactory.getSession().isOpen()) {
            fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory);
            fileSynchronizer.setDeleteRemoteFiles(false);
            fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*" + sourceFileExtn));
            fileSynchronizer.setRemoteDirectory(remoteDirectory);

            fileSynchronizer.synchronizeToLocalDirectory(new File(localDirectory));
        }

        return RepeatStatus.FINISHED;
    }
}

My Session factory:

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
   DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
   factory.setHost(host);
   factory.setPort(port);
   factory.setUser(username);
   factory.setPassword(password);
   factory.setAllowUnknownKeys(true);
   return new CachingSessionFactory<LsEntry>(factory);
}

Но без вызова afterPropertiesSet () я получаю сообщение об ошибке ниже:

org.springframework.messaging.MessagingException: Problem occurred while synchronizing 'null' to local directory; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to list files; nested exception is 4: 
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:315) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:293) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at com.ftp.demo.tasklet.FtpFileDownloadTasklet.execute(FtpFileDownloadTasklet.java:52) ~[classes/:na]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:273) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) ~[spring-batch-infrastructure-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:203) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:399) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:313) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:144) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:137) [spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
    at com.ftp.demo.main.SpringBatchIntegrationftpDemoApplication.main(SpringBatchIntegrationftpDemoApplication.java:44) [classes/:na]
Caused by: org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to list files; nested exception is 4: 
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:446) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:308) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 20 common frames omitted
Caused by: org.springframework.core.NestedIOException: Failed to list files; nested exception is 4: 
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:103) ~[spring-integration-sftp-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:50) ~[spring-integration-sftp-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.list(CachingSessionFactory.java:230) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.transferFilesFromRemoteToLocal(AbstractInboundFileSynchronizer.java:323) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.lambda$synchronizeToLocalDirectory$0(AbstractInboundFileSynchronizer.java:309) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:437) ~[spring-integration-file-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 21 common frames omitted
Caused by: com.jcraft.jsch.SftpException: 
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1747) ~[jsch-0.1.55.jar:na]
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1553) ~[jsch-0.1.55.jar:na]
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:91) ~[spring-integration-sftp-5.1.4.RELEASE.jar:5.1.4.RELEASE]
    ... 26 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.jcraft.jsch.ChannelSftp.remoteAbsolutePath(ChannelSftp.java:2943) ~[jsch-0.1.55.jar:na]
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1572) ~[jsch-0.1.55.jar:na]
    ... 28 common frames omitted
...