我是 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"/>
关于上述文件,我还有几个问题。
- 我对
<integration:channel id="strings"/>
不太清楚标签。为什么它只是字符串。可以使用哪些其他可能的值strings
。 - 据我测试
input-channel
integration:service-activator
的和output-channel
file-to-string-transformer
的与<integration:channel id="strings"/>
无关这是正确的吗? 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"
?我怎样才能调用其他方法,具体方法是什么。我尝试了很多方法,但仍然没有机会。
最佳答案
首先看来你的理论知识还不够,考虑看看书:EIP Book , Spring Integration in Action .
一般MessageChannel
是一个通过松散耦合的方式在服务之间传递消息的概念。
strings
只是一个bean 名称,它可以是任何值。 <integration:channel>
只是一个自定义标签,它会生成 MessageChannel
到底。为了更舒服,您应该阅读 Reference Manual .
自从您使用 <file:file-to-string-transformer>
,该组件的结果将是 String
文件表示。
您的<service-activator>
调用handleString
你的方法Handler
因为运行时payload
参数类型解析。
要使其与其他方法一起使用,您应该使用适当的变压器( <file:file-to-bytes-transformer>
)或根本不使用它 - <file:inbound-channel-adapter>
产生File
对象为payload
得到的消息。
关于java - Spring Integration - 文件适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24626033/