Spring Integration 系統集成
提供了基於 Spring EIP Enterprise Integration Patterns
企業集成模式
不同系統之前的交互
通過異步消息驅動來達到系統與系統交互時系統之間的鬆耦合。
Spring Integration Java DSL
實現分類讀取遠程的特定文件,實現發送郵件。
實戰
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-feed</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
<version>1.1.0.M1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
讀取:https://spring.io/blog.atom ,新聞聚合文件
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Spring</title>
<link rel="alternate" href="https://spring.io/blog" />
<link rel="self" href="https://spring.io/blog.atom" />
<id>http://spring.io/blog.atom</id>
<icon>https://spring.io/favicon.ico</icon>
<updated>2020-04-17T20:12:51Z</updated>
<entry>
<title>Spring Cloud App Broker 1.0.5 released</title>
<link rel="alternate" href="https://spring.io/blog/2020/04/17/spring-cloud-app-broker-1-0-5-released" />
<category term="releases" label="Releases" />
<author>
<name>Roy Clarkson</name>
</author>
<id>tag:spring.io,2020-04-17:4033</id>
<updated>2020-04-17T20:12:51Z</updated>
</entry>
</feed>
讀取到的消息通過分類 Category
將消息轉到不同的消息通道
將 releases 和 engineering寫入磁盤。
news發送郵件
@SpringBootApplication
public class Ch94Application {
public static void main(String[] args) {
SpringApplication.run(Ch94Application.class, args);
}
@Value("https://spring.io/blog.atom") // 1
Resource resource;
@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() { // 2 默認輪詢方式讀取
return Pollers.fixedRate(500).get();
}
@Bean
public FeedEntryMessageSource feedMessageSource() throws IOException { //3
FeedEntryMessageSource messageSource = new FeedEntryMessageSource(resource.getURL(), "news");
return messageSource;
}
@Bean
public IntegrationFlow myFlow() throws IOException {
return IntegrationFlows.from(feedMessageSource()) //4
.<SyndEntry, String> route(payload -> payload.getCategories().get(0).getName(),//5
mapping -> mapping.channelMapping("releases", "releasesChannel") //6
.channelMapping("engineering", "engineeringChannel")
.channelMapping("news", "newsChannel"))
.get(); // 7
}
@Bean
public IntegrationFlow releasesFlow() {
return IntegrationFlows.from(MessageChannels.queue("releasesChannel", 10)) //1
.<SyndEntry, String> transform(
payload -> "《" + payload.getTitle() + "》 " + payload.getLink() + getProperty("line.separator")) //2
.handle(Files.outboundAdapter(new File("e:/springblog")) //3
.fileExistsMode(FileExistsMode.APPEND) //4
.charset("UTF-8") //5
.fileNameGenerator(message -> "releases.txt") //6
.get())
.get();
}
@Bean
public IntegrationFlow engineeringFlow() {
return IntegrationFlows.from(MessageChannels.queue("engineeringChannel", 10))
.<SyndEntry, String> transform(
payload -> "《" + payload.getTitle() + "》 " + payload.getLink() + getProperty("line.separator"))
.handle(Files.outboundAdapter(new File("e:/springblog"))
.fileExistsMode(FileExistsMode.APPEND)
.charset("UTF-8")
.fileNameGenerator(message -> "engineering.txt")
.get())
.get();
}
@Bean
public IntegrationFlow newsFlow() {
return IntegrationFlows.from(MessageChannels.queue("newsChannel", 10))
.<SyndEntry, String> transform(
payload -> "《" + payload.getTitle() + "》 " + payload.getLink() + getProperty("line.separator"))
.enrichHeaders( //1
Mail.headers()
.subject("來自Spring的新聞")
.to("[email protected]")
.from("[email protected]"))
.handle(Mail.outboundAdapter("smtp.126.com") //2
.port(25)
.protocol("smtp")
.credentials("[email protected]", "******")
.javaMailProperties(p -> p.put("mail.debug", "false")), e -> e.id("smtpOut"))
.get();
}
}