2016-07-14 90 views
1

如何通过注释配置入站通道适配器而不是常规配置文件?我能够为会话工厂定义bean,但是可以在以下位置:弹簧集成:通过注释的入站通道适配器配置

@Bean 
public DefaultFtpSessionFactory ftpSessionFactory() { 
     DefaultFtpSessionFactory ftpSessionFactory = new 
     DefaultFtpSessionFactory(); 
     ftpSessionFactory.setHost(host); 
     ftpSessionFactory.setPort(port); 
     ftpSessionFactory.setUsername(username); 
     ftpSessionFactory.setPassword(password); 
     return ftpSessionFactory; 
    } 

如何配置通过注释下给定的入站通道适配器?

<int-ftp:inbound-channel-adapter id="ftpInbound" 
           channel="ftpChannel" 
           session-factory="ftpSessionFactory" 
           filename-pattern="*.xml" 
           auto-create-local-directory="true" 
           delete-remote-files="false" 
           remote-directory="/" 
           local-directory="ftp-inbound" 
           local-filter="acceptOnceFilter"> 

    <int:poller fixed-delay="60000" max-messages-per-poll="-1"> 
     <int:transactional synchronization-factory="syncFactory" /> 
    </int:poller> 

</int-ftp:inbound-channel-adapter> 

@Artem比兰 修改后的代码是在

@EnableIntegration 
@Configuration 
public class FtpConfiguration { 
    @Value("${ftp.host}") 
    private String host; 
    @Value("${ftp.port}") 
    private Integer port; 
    @Value("${ftp.username}") 
    private String username; 
    @Value("${ftp.password}") 
    private String password; 
    @Value("${ftp.fixed.delay}") 
    private Integer fixedDelay; 
    @Value("${ftp.local.directory}") 
    private String localDirectory; 

    private final static Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); 

@Bean 
public SessionFactory<FTPFile> ftpSessionFactory() { 
    DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory(); 
    sessionFactory.setHost(host); 
    sessionFactory.setPort(port); 
    sessionFactory.setUsername(username); 
    sessionFactory.setPassword(password); 
    return new CachingSessionFactory<FTPFile>(sessionFactory); 
} 

@Bean 
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() { 
    FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory()); 
    fileSynchronizer.setDeleteRemoteFiles(false); 
    fileSynchronizer.setRemoteDirectory("/"); 
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.xml")); 
    return fileSynchronizer; 
} 

@Bean 
@InboundChannelAdapter(value = "ftpChannel", 
     poller = @Poller(fixedDelay = "60000", maxMessagesPerPoll = "-1")) 
public MessageSource<File> ftpMessageSource() { 
    FtpInboundFileSynchronizingMessageSource source = 
      new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer()); 
    source.setLocalDirectory(new File(localDirectory)); 
    source.setAutoCreateLocalDirectory(true); 
    source.setLocalFilter(new AcceptOnceFileListFilter<File>()); 
    return source; 
} 

}

在运行,我得到一个例外,因为 下没有名为 'ftpChannel' 豆定义

请注意,'渠道'关键字不可用时,连接入站通道适配器,其'价值',而不是。

我试图用PollableChannel连接通道,但也是徒劳无功。它是作为下:

@Bean 
public MessageChannel ftpChannel() { 
    return new PollableChannel() { 
     @Override 
     public Message<?> receive() { 
      return this.receive(); 
     } 

     @Override 
     public Message<?> receive(long l) { 
      return null; 
     } 

     @Override 
     public boolean send(Message<?> message) { 
      return false; 
     } 

     @Override 
     public boolean send(Message<?> message, long l) { 
      return false; 
     } 
    }; 
} 

我得到了一个错误“无法在超时发送消息:-1”。我做错了什么仍然?

我正在寻找的是要连接的应用程序的所有豆类启动,然后揭露一些方法来启动服务器的轮询,对它们进行处理,然后从本地删除它们,像这样

public void startPollingTheServer() { 
    getPollableChannel().receive(); 
} 

其中getPollableChannel()为我提供了用于轮询的有线bean。

回答

3

有一个@InboundChannelAdapter给你。

@Bean 
public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() { 
    FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory()); 
    fileSynchronizer.setDeleteRemoteFiles(false); 
    fileSynchronizer.setRemoteDirectory("/"); 
    fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.xml")); 
    return fileSynchronizer; 
} 

@Bean 
@InboundChannelAdapter(channel = "ftpChannel") 
public MessageSource<File> ftpMessageSource() { 
    FtpInboundFileSynchronizingMessageSource source = 
      new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer()); 
    source.setLocalDirectory(new File("ftp-inbound")); 
    source.setAutoCreateLocalDirectory(true); 
    source.setLocalFilter(new AcceptOnceFileListFilter<File>()); 
    return source; 
} 

再加上看看Reference Manual

另外要注意,请为Java DSL for Spring Integration,其中同样可能看起来像:

@Bean 
public IntegrationFlow ftpInboundFlow() { 
    return IntegrationFlows 
      .from(s -> s.ftp(this.ftpSessionFactory) 
          .preserveTimestamp(true) 
          .remoteDirectory("ftpSource") 
          .regexFilter(".*\\.txt$") 
          .localFilename(f -> f.toUpperCase() + ".a") 
          .localDirectory(this.ftpServer.getTargetLocalDirectory()), 
        e -> e.id("ftpInboundAdapter").autoStartup(false)) 
      .channel(MessageChannels.queue("ftpInboundResultChannel")) 
      .get(); 
} 
+0

另见https://github.com/spring-projects/spring-integration/pull/1851 –

+0

请注意@artem将不会收到有关您的问题编辑的通知 - 您必须在此处添加评论,说您已对他进行了更改以获取通知。 'channel'在版本4.3.0中被添加为'value'的别名 - 这也修复了缺失的频道问题。如果由于某种原因你不能移动到4.3,你可以明确地将通道定义为'@ Bean'('DirectChannel')。 –

+0

我可以通过提供如下队列通道来获得这个工作: @Bean public MessageChannel ftpChannel(){ return new QueueChannel(10); } – Anish

相关问题