9.1 声明简单的集成流
9.1 声明简单的集成流
一般来说,
为了熟悉
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
第一个依赖项是
第二个依赖项是
接下来,需要为应用程序创建一种将数据发送到集成流的方法,以便将数据写入文件。为此,将创建一个网关接口,如下面所示。程序清单
package sia5;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.file.FileHeaders;
import org.springframework.messaging.handler.annotation.Header;
@MessagingGateway(defaultRequestChannel="textInChannel")
public interface FileWriterGateway {
void writeToFile(
@Header(FileHeaders.FILENAME) String filename,
String data);
}
尽管它是一个简单的
对于
现在已经有了一个消息网关,还需要配置集成流。尽管添加到构建中的
XML 配置Java 配置- 使用
DSL 进行Java 配置
我们将对
9.1.1 使用XML 定义集成流
尽管在本书中我避免使用
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-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/
springintegration-file.xsd">
<int:channel id="textInChannel" />
<int:transformer id="upperCase" input-channel="textInChannel"
output-channel="fileWriterChannel" expression="payload.toUpperCase()" />
<int:channel id="fileWriterChannel" />
<int-file:outbound-channel-adapter id="writer" channel="fileWriterChannel"
directory="/tmp/sia5/files" mode="APPEND" append-new-line="true" />
</beans>
分析一下程序清单
- 配置了一个名为
textInChannel 的通道,这与为FileWriterGateway 设置的请求通道是相同的。当在FileWriterGateway 上调用writeToFile() 方法时,结果消息被发布到这个通道。 - 配置了一个转换器来接收来自
textInChannel 的消息。它使用Spring Expression Language (SpEL)表达式在消息有效负载上调用toUpperCase() 。然后将大写操作的结果发布到fileWriterChannel 中。 - 配置了一个名为
fileWriterChannel 的通道,此通道用作连接转换器和外部通道适配器的管道。 - 最后,使用
int-file 命名空间配置了一个外部通道适配器。这个XML 命名空间由Spring Integration 的文件模块提供,用于编写文件。按照配置,它将接收来自fileWriterChannel 的消息,并将消息有效负载写到一个文件中,该文件的名称在消息的file_name 头中指定,该文件位于directory 属性中指定的目录中。如果文件已经存在,则将用换行来追加文件,而不是覆盖它。
如果希望在
@Configuration
@ImportResource("classpath:/filewriter-config.xml")
public class FileWriterIntegrationConfig { ... }
尽管基于
9.1.2 在Java 中配置集成流
大多数现代
作为如何使用
package sia5;
import java.io.File;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.file.FileWritingMessageHandler;
import org.springframework.integration.file.support.FileExistsMode;
import org.springframework.integration.transformer.GenericTransformer;
@Configuration
public class FileWriterIntegrationConfig {
@Bean
@Transformer(inputChannel="textInChannel", outputChannel="fileWriterChannel")
public GenericTransformer<String, String> upperCaseTransformer() {
return text -> text.toUpperCase();
}
@Bean
@ServiceActivator(inputChannel="fileWriterChannel")
public FileWritingMessageHandler fileWriter() {
FileWritingMessageHandler handler =
new FileWritingMessageHandler(new File("/tmp/sia5/files"));
handler.setExpectReply(false);
handler.setFileExistsMode(FileExistsMode.APPEND);
handler.setAppendNewLine(true);
return handler;
}
}
使用
至于文件写入
还会看到不需要显式地声明通道。如果不存在具有这些名称的
@Bean
public MessageChannel textInChannel() {
return new DirectChannel();
}
...
@Bean
public MessageChannel fileWriterChannel() {
return new DirectChannel();
}
可以说,
9.1.3 使用Spring Integration 的DSL 配置
让我们进一步尝试定义文件编写集成流。这一次,仍然使用
package sia5;
import java.io.File;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.file.dsl.Files;
import org.springframework.integration.file.support.FileExistsMode;
@Configuration
public class FileWriterIntegrationConfig {
@Bean
public IntegrationFlow fileWriterFlow() {
return IntegrationFlows
.from(MessageChannels.direct("textInChannel"))
.<String, String>transform(t -> t.toUpperCase())
.handle(Files.outboundAdapter(new File("/tmp/sia5/files"))
.fileExistsMode(FileExistsMode.APPEND)
.appendNewLine(true))
.get();
}
}
这个新配置尽可能简洁,用一个
在程序清单
注意,与
至于连接转换器和外部通道适配器的通道,甚至不需要通过名称引用它。如果需要显式配置通道,可以在流定义中通过调用
@Bean
public IntegrationFlow fileWriterFlow() {
return IntegrationFlows
.from(MessageChannels.direct("textInChannel"))
.<String, String>transform(t -> t.toUpperCase())
.channel(MessageChannels.direct("fileWriterChannel"))
.handle(Files.outboundAdapter(new File("/tmp/sia5/files"))
.fileExistsMode(FileExistsMode.APPEND)
.appendNewLine(true))
.get();
}
在使用
现在已经看到了使用三种不同配置风格定义的简单流,让我们回过头来看看