2016-10-24 159 views
0

使用Spring FTP集成和註解配置,我從FTP服務器下載了文件。下載後,我們的應用程序會觸發連接服務器並找到任何新添加的文件,如果添加了任何文件,它將從服務器下載。但我不需要維護FTP服務器會話並在首次連接或第一次下載後斷開服務器連接。如何在文件下載後斷開FTP服務器連接?

代碼:

public class FtpServices { 

    @Bean(name="ftpSessionFactory") 
    public DefaultFtpSessionFactory ftpSessionFactory() { 
     System.out.println("session"); 

     DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); 
     sf.setHost("localhost"); 
     sf.setPort(21); 
     sf.setUsername("user"); 
     sf.setPassword("password"); 

     return sf; 
    } 

    @Bean 
    public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() { 
     System.out.println("2"); 
     FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory()); 
     fileSynchronizer.setDeleteRemoteFiles(false); 
     fileSynchronizer.afterPropertiesSet(); 
     fileSynchronizer.setRemoteDirectory("/test/"); 
     // fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.docx")); 
     fileSynchronizer.setFilter(filter); 
     return fileSynchronizer; 
    } 

    @Bean() 
    @InboundChannelAdapter(value="ftpChannel", poller = @Poller(fixedDelay = "50", maxMessagesPerPoll = "1")) 
    public FtpInboundFileSynchronizingMessageSource ftpMessageSource() { 
     System.out.println(3); 
     FtpInboundFileSynchronizingMessageSource source = 
       new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer()); 
     source.setLocalDirectory(new File("D:/Test-downloaded/")); 
     //source.stop(); 
     return source; 
    } 

    @Bean 
    @ServiceActivator(inputChannel = "ftpChannel", requiresReply="false") 
    public MessageHandler handler() { 
     System.out.println(4); 
     MessageHandler handler = new MessageHandler() { 

      @Override 
      public void handleMessage(Message<?> message) throws MessagingException { 
       System.out.println(message.getPayload()+" @ServiceActivator"); 
       System.out.println(" Message Header :"+message.getHeaders()); 
      } 
     }; 
     return handler; 
    } 

    @Bean(name = PollerMetadata.DEFAULT_POLLER) 
    public PollerMetadata defaultPoller() { 

     PollerMetadata pollerMetadata = new PollerMetadata(); 
     pollerMetadata.setTrigger(triggerOnlyOnce()); 
     return pollerMetadata; 
    } 

} 

,也是我重寫AbtractFTPSessionFactory.java測試FTP服務器的連接和斷開過程。

protected void postProcessClientAfterConnect(T t) throws IOException { 
    System.out.println("After connect"); 
} 

protected void postProcessClientBeforeConnect(T client) throws IOException { 
    System.out.println("Before connect"); 
} 

控制檯:

INFO : org.springframework.context.support.DefaultLifecycleProcessor - Starting beans in phase -2147483648 
    INFO : org.springframework.context.support.DefaultLifecycleProcessor - Starting beans in phase 0 
    Before connect 
    After connect 
    D:\Test-downloaded\demo 1.txt @ServiceActivator 
    Message Header :{id=e4a1fd7f-0bbf-9692-f70f-b0ac68b4dec4, timestamp=1477317086272} 
    D:\Test-downloaded\demo.txt @ServiceActivator 
    Message Header :{id=9115ee92-12b4-bf1f-d592-9c13bf7a27fa, timestamp=1477317086324} 
    Before connect 
    After connect 
    Before connect 
    After connect 
    Before connect 
    After connect 
    Before connect 
    After connect 
    Before connect 
    After connect 
    Before connect 
    After connect 

感謝。

回答

1

這確實是任何@InboundChannelAdapter的目的:定期輪詢目標系統以尋找新數據。

要做到這一點,一旦我們有時會建議OnlyOnceTrigger

public class OnlyOnceTrigger implements Trigger { 

    private final AtomicBoolean done = new AtomicBoolean(); 

    @Override 
    public Date nextExecutionTime(TriggerContext triggerContext) { 
      return !this.done.getAndSet(true) ? new Date() : null; 
    } 

} 

但是,這可能不適合你的情況下工作,因爲有可能不被期望在源FTP目錄中的文件呢。

因此,我們必須進行民意調查,直到您將收到所需的文件,並在符合條件時接受.stop()適配器。

爲了這個目的,你可以使用任何下游的邏輯來確定狀態或考慮實施AbstractMessageSourceAdvice被注入到@PollerPollerMetadatahttp://docs.spring.io/spring-integration/reference/html/messaging-channels-section.html#conditional-pollers

+0

謝謝您的回答。 –

+0

它的工作正常,但它仍然觸發AbstractMessageSourceAdvice子類和一些業務邏輯後,我從beforeReceive(MessageSource 源)返回false,所以它不會觸發FTP服務器。是否影響我們的應用程序性能。 –

+0

好吧,不是那麼多,但是完全避免觸發,你可以考慮停止入站通道適配器端點 –

相關問題