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("wisely-man@126.com")
.from("wisely-man@126.com"))
.handle(Mail.outboundAdapter("smtp.126.com") //2
.port(25)
.protocol("smtp")
.credentials("wisely-man@126.com", "******")
.javaMailProperties(p -> p.put("mail.debug", "false")), e -> e.id("smtpOut"))
.get();
}
}