ftp/sftp spring 集成 - localDirectory 不能为空

ftp/sftp spring integration - localDirectory must not be null

我正在尝试轮询 ftp 服务器以获取文件并处理它们。我想在没有 xml 配置的情况下进行。我正在使用 spring boot(1.2.5) 和 spring 集成 ftp (4.1.5)

我为会话工厂创建了一个 bean

    @Bean
public DefaultFtpSessionFactory ftpSessionFactory() {
    DefaultFtpSessionFactory ftpSessionFactory = new DefaultFtpSessionFactory();
    ftpSessionFactory.setHost(ftpProperties.ftpHost);
    ftpSessionFactory.setPort(Integer.parseInt(ftpProperties.ftpPort));
    ftpSessionFactory.setUsername(ftpProperties.ftpUser);
    ftpSessionFactory.setPassword(ftpProperties .ftpPassword);
    return ftpSessionFactory;
}

我正在尝试按如下方式配置 ftp 服务器轮询。

    @Bean
@InboundChannelAdapter(value = "receiveChannel", poller = @Poller(fixedRate="1000"))
public MessageSource pollFtpForFiles() {
    File localDirectory = new File(sftpProperties.localDirectory);
    System.err.println("LocalDirectory::"+localDirectory.getAbsolutePath()+"::exists::"+localDirectory.isDirectory());
    FtpInboundFileSynchronizer ftpInboundFileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory());
    ftpInboundFileSynchronizer.setRemoteDirectory(sftpProperties.remoteDirectory);
    ftpInboundFileSynchronizer.synchronizeToLocalDirectory(localDirectory);
    return new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer);
}

但是当我 运行 应用程序时出现以下异常。 不确定我做错了什么。有人可以帮帮我吗。 (我试过 sftp 也有同样的结果)

LocalDirectory::localDirForSftpTransfer::exists::true

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sftpInboundSetup' defined in file [/Users/rsamban/Documents/workspace/Second/target/classes/com/yesVin/integration/sftp/SftpInboundSetup.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pollFtpForFiles' defined in class path resource [com/yesVin/integration/sftp/SftpInboundSetup.class]: **Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: localDirectory must not be null**
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.yesVin.integration.SecondApplication.main(SecondApplication.java:16)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pollFtpForFiles' defined in class path resource [com/yesVin/integration/sftp/SftpInboundSetup.class]: **Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: localDirectory must not be null**
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.resolveTargetBeanFromMethodWithBeanAnnotation(AbstractMethodAnnotationPostProcessor.java:362)
    at org.springframework.integration.config.annotation.InboundChannelAdapterAnnotationPostProcessor.createMessageSource(InboundChannelAdapterAnnotationPostProcessor.java:76)
    at org.springframework.integration.config.annotation.InboundChannelAdapterAnnotationPostProcessor.postProcess(InboundChannelAdapterAnnotationPostProcessor.java:62)
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.doWith(MessagingAnnotationPostProcessor.java:151)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:495)
    at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.postProcessAfterInitialization(MessagingAnnotationPostProcessor.java:131)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    ... 13 more
**Caused by: java.lang.IllegalArgumentException: localDirectory must not be null**
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.afterPropertiesSet(AbstractInboundFileSynchronizingMessageSource.java:132)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 28 more

谢谢 -拉梅什

操作:

ftpInboundFileSynchronizer.synchronizeToLocalDirectory(localDirectory);

不是你的责任。

我确定你的意思就是这个 setter:

FtpInboundFileSynchronizingMessageSource.setLocalDirectory(localDirectory);

换句话说,您的 @InboundChannelAdapter 应该如下所示:

@Bean
@InboundChannelAdapter(value = "receiveChannel", poller = @Poller(fixedRate="1000"))
public MessageSource pollFtpForFiles() {
    File localDirectory = new File(sftpProperties.localDirectory);
     System.err.println("LocalDirectory::"+localDirectory.getAbsolutePath()+"::exists::"+localDirectory.isDirectory());
    FtpInboundFileSynchronizer ftpInboundFileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory());
    ftpInboundFileSynchronizer.setRemoteDirectory(sftpProperties.remoteDirectory);
    FtpInboundFileSynchronizingMessageSource messageSource = new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer);
    messageSource.setLocalDirectory(localDirectory);
    return messageSource;
}