2014-07-08 129 views
0

我是Spring的新手,我正在写一个文件适配器,它从输入文件夹读取文件并将文件移动到输出文件夹。我玩了下面的例子,但有一些点我不清楚。春季集成 - 文件适配器

<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="C:\Users\my\Desktop\files\in" 
           filename-regex="[a-z]+.txt"> 
    <integration:poller id="poller" fixed-delay="5000"/> 
</file:inbound-channel-adapter> 

<file:file-to-string-transformer input-channel="filesIn" output-channel="strings"/> 

<integration:channel id="strings"/> 

<integration:service-activator input-channel="strings" 
           output-channel="filesOut" 
           ref="handler"/> 

<file:outbound-channel-adapter id="filesOut" directory="C:\Users\my\Desktop\files\out"/> 

<bean id="handler" class="org.springframework.integration.samples.filecopy.Handler"/> 

我有几个关于上述文件的更多问题。

  1. 我没有对<integration:channel id="strings"/>标签清晰的概念。为什么它只是字符串。其他可能的值是什么,可以用来代替strings
  2. 正如我测试input-channelintegration:service-activatoroutput-channelfile-to-string-transformer<integration:channel id="strings"/>无关,是正确的吗?
  3. handler用于integration:service-activator的bean有3种方法。

    public class Handler { 
    
    public String handleString(String input) { 
        System.out.println("Copying text: " + input); 
        return input.toUpperCase(); 
    } 
    
    public File handleFile(File input) { 
        System.out.println("Copying file: " + input.getAbsolutePath()); 
        return input; 
    } 
    
    public byte[] handleBytes(byte[] input) { 
        System.out.println("Copying " + input.length + " bytes ..."); 
        return new String(input).toUpperCase().getBytes(); 
    } } 
    

    而且目前它触发只有一个方法是handleString(String whichTakesString)是不是因为<integration:service-activator input-channel="strings"的?我怎样才能调用其他方法,怎么做。我试图找出很多方法,但仍然没有机会。

回答

1

一切看起来你没有足够的理论知识,因此考虑到阅读的书籍的第一:EIP BookSpring Integration in Action

一般而言,MessageChannel是一种通过松散耦合方式在业务之间传递消息的概念。

strings只是一个豆名它可以是任何值。 <integration:channel>只是一个自定义标签,最终会生成MessageChannel。为了更舒适,你应该阅读Reference Manual

由于您使用的是<file:file-to-string-transformer>,因此此组件的结果将为String文件表示形式。

您的<service-activator>调用handleString方法Handler因为运行时参数类型的分辨率为​​。

,使其与其他方法的工作,你应该使用合适的变压器(<file:file-to-bytes-transformer>),或根本就不使用它 - <file:inbound-channel-adapter>产生File对象​​的结果消息。

+0

感谢您的快速回复,这是非常有用的。 –