2014-07-08 179 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

感謝您的快速回復,這是非常有用的。 –