2016-07-14 132 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

相關問題