简体   繁体   English

Spring Integration-文件适配器

[英]Spring Integration - file adapter

I'm newbie to Spring, I am writing a file adapter which reads file from the input folder and move the files to output folder. 我是Spring的新手,正在编写一个文件适配器,该适配器将从输入文件夹中读取文件并将文件移至输出文件夹。 I played with the following examples but there are some points that are not clear to me. 我玩了以下示例,但是有些地方我不清楚。

<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"/>

I have few more questions regarding the above file. 关于以上文件,我还有其他几个问题。

  1. I don't have a clear idea about the <integration:channel id="strings"/> tag. 对于<integration:channel id="strings"/>标记,我不清楚。 Why its just string. 为什么它只是字符串。 What are the other possible values which can be used instead strings . 可以代替strings使用的其他可能值是什么?
  2. As I tested input-channel of the integration:service-activator and output-channel of the file-to-string-transformer has nothing to do with <integration:channel id="strings"/> is it correct? 正如我测试input-channel的的integration:service-activatoroutput-channel的的file-to-string-transformer无关与<integration:channel id="strings"/>是它正确的吗?
  3. handler bean which is used in integration:service-activator has 3 methods. integration:service-activator使用的handler bean integration:service-activator有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(); } } 

    And currently it fires only one method which is handleString(String whichTakesString) is it because of the <integration:service-activator input-channel="strings" ? 并且当前它仅触发一个方法handleString(String whichTakesString)是因为<integration:service-activator input-channel="strings"吗? How can I call the other methods, what is the way to do it. 我如何调用其他方法,该怎么做? I tried to figure this out many ways but still no chance. 我试图通过多种方法来解决这个问题,但仍然没有机会。

First of all it looks like you haven't got enough theoretical knowledges, so consider to read the books: EIP Book , Spring Integration in Action . 首先,您似乎还没有足够的理论知识,所以请考虑阅读以下书籍: EIP书籍Spring Integration in Action

In general MessageChannel is a concept to carry messages between services via loosely-coupling manner. 通常, MessageChannel是通过松耦合方式在服务之间传递消息的概念。

strings is just a bean name and it can be any value. strings只是一个bean名称 ,它可以是任何值。 <integration:channel> is just a custom tag, which produces a MessageChannel in the end. <integration:channel>只是一个自定义标记,它最终会产生一个MessageChannel To be more comfortable with that you should to read the Reference Manual . 为了使自己更加满意,您应该阅读参考手册

Since you use <file:file-to-string-transformer> , the result of this component will be String file representation. 由于您使用<file:file-to-string-transformer> ,因此此组件的结果将是String文件表示形式。

Your <service-activator> invokes handleString method of your Handler because the runtime payload argument type resolution. 您的<service-activator>调用您的Handler handleString方法,因为运行时payload参数类型解析。

To make it worked with other methods you should use appropriate transformer ( <file:file-to-bytes-transformer> ) or don't use it at all - the <file:inbound-channel-adapter> produces File object as payload for resulting message. 为了使其与其他方法一起使用,您应该使用适当的转换器( <file:file-to-bytes-transformer> )或根本不使用它- <file:inbound-channel-adapter>产生File对象作为payload用于结果消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM