2017-02-10 138 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,则需要声明通道。入站适配器上不需要,因为服务激活器会自动声明其输入通道(如果不存在)。

相关问题