2017-02-10 135 views
0

我開發了一個代碼,通過Spring Integration File Support教程,其中我從特定位置輪詢文件並進一步處理它們。對於投票的目的我已經使用Spring集成的入站和出站通道適配器,所以我有我的bean.xml爲同一如下:爲什麼SFTP入站/出站通道適配器有單獨的通道聲明,爲什麼不用簡單的文件入站/出站通道適配器?

 <?xml version="1.0" encoding="UTF-8"?> 
     <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:integration="http://www.springframework.org/schema/integration" 
      xmlns:file="http://www.springframework.org/schema/integration/file" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/integration 
        http://www.springframework.org/schema/integration/spring-integration.xsd 
        http://www.springframework.org/schema/integration/file 
        http://www.springframework.org/schema/integration/file/spring-integration-file.xsd"> 
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" /> 
      <file:inbound-channel-adapter id="filesIn" 
       directory="file:${java.io.tmpdir}/input"> 
       <integration:poller id="poller" fixed-rate="60000" /> 
      </file:inbound-channel-adapter> 
      <integration:service-activator 
       input-channel="filesIn" output-channel="filesOut" ref="handler" /> 
      <file:outbound-channel-adapter id="filesOut" 
       directory="file:${java.io.tmpdir}/archive" delete-source-files="true"> 
      </file:outbound-channel-adapter> 
      <bean id="handler" class="com.m.c.Handler" /> 
     </beans> 

現在我的主類是:

@SpringBootConfiguration 
    @EnableScheduling 
    public class App{ 
     private static final Log LOGGER = LogFactory.getLog(App.class); 

     public static void main(String[] args) 
     { 
      SpringApplication.run(App.class, args); 
     } 

     @Scheduled(fixedDelay = 60000) 
     public static void display() throws InvalidFormatException, IOException{ 
      ApplicationContext context = new ClassPathXmlApplicationContext("/spring/integration/bean.xml", App.class); 
      File inDir = (File) new DirectFieldAccessor(context.getBean(FileReadingMessageSource.class)).getPropertyValue("directory"); 
      LiteralExpression expression = (LiteralExpression) new DirectFieldAccessor(context.getBean(FileWritingMessageHandler.class)).getPropertyValue("destinationDirectoryExpression"); 
      File outDir = new File(expression.getValue()); 
      LOGGER.info("Input directory is: " + inDir.getAbsolutePath()); 
      LOGGER.info("Archive directory is: " + outDir.getAbsolutePath()); 
      LOGGER.info("==================================================="); 
     } 
    } 

並有一個處理文件的Handler類,這對我來說工作得很好。

現在的問題是我想爲SFTP遠程服務器創建相同的機制,並從該位置輪詢文件,並將處理後的文件放在同一個SFTP位置的不同文件夾中。

因此,我配置了我的bean.xml,併爲SFTP編寫了入站和出站通道適配器。當我來到服務激活器部分時,我發現我需要爲入站和出站通道適配器配置單獨的通道,並在服務激活器中提供它們的ID,這在簡單文件輪詢器中沒有看到,它工作正常。那麼爲什麼入站和出站通道適配器需要單獨的通道?如果他們需要,我們如何爲計劃文件輪詢服務實現相同的功能?

+0

我剛剛運行了與SFTP類似配置的模擬測試用例,並且對我的隱式通道沒有任何異議。所以,你的新配置可能有問題。 –

回答

-1

您沒有顯示相關配置,所以我不確定您在問什麼,但通道適配器上的channel是可選的;如果省略,則通道名稱與id相同。但總是有一個渠道。是否需要實際聲明頻道取決於消費者方面。

如果您在出站適配器上明確使用channel,則需要聲明通道。入站適配器上不需要,因爲服務激活器會自動聲明其輸入通道(如果不存在)。

相關問題