Spring Integration运行示例
SimpleFlowApplication.java
package sia5; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; @SpringBootApplication public class SimpleFlowApplication { public static void main(String[] args) { SpringApplication.run(SimpleFlowApplication.class, args); } @Bean public CommandLineRunner writeData(FileWriterGateway gateway, Environment env) { return args -> { String[] activeProfiles = env.getActiveProfiles(); if (activeProfiles.length > 0) { String profile = activeProfiles[0]; System.out.println("profile:" + profile); gateway.writeToFile("simple.txt", "Hello, Spring Integration! (" + profile + ")"); } else { System.out.println("No active profile set. Should set active profile to one of xmlconfig, javaconfig, or javadsl."); } }; } }
FileWriterGateway.java
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); }
FileWriterIntegrationConfig.java
package sia5; import java.io.File; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.Profile; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.annotation.Transformer; import org.springframework.integration.dsl.IntegrationFlow; import org.springframework.integration.dsl.IntegrationFlows; //import org.springframework.integration.dsl.channel.MessageChannels; import org.springframework.integration.file.FileWritingMessageHandler; import org.springframework.integration.file.dsl.Files; import org.springframework.integration.file.support.FileExistsMode; import org.springframework.integration.transformer.GenericTransformer; @Configuration public class FileWriterIntegrationConfig { @Profile("xmlconfig") @Configuration @ImportResource("classpath:/filewriter-config.xml") public static class XmlConfiguration {} @Profile("javaconfig") @Bean @Transformer(inputChannel="textInChannel", outputChannel="fileWriterChannel") public GenericTransformer<String, String> upperCaseTransformer() { return text -> text.toUpperCase(); } @Profile("javaconfig") @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; } // // DSL Configuration // /*@Profile("javadsl") @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(); }*/ }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>simple-flow</artifactId>
<name>simple-flow</name>
<description>simple-flow</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
运行配置如下:

浙公网安备 33010602011771号