EE颠覆者第九章 Spring Integration

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();
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java EE开发的颠覆者在线看是指在Java Enterprise Edition(Java EE)开发领域中一种新兴的技术或工具,能够颠覆传统的开发方式,使开发人员能够更加方便、高效地进行应用程序的开发和部署。 传统的Java EE开发方式通常需要在本地搭建开发环境、安装并配置各种开发工具和中间件,并且需要手动编译、打包和部署应用程序。这种方式费时费力,不利于快速迭代和开发效率的提升。 而Java EE开发的颠覆者在线看则提供了一种更加现代化和便捷的开发方式。开发者可以通过在线平台进行开发,无需在本地进行环境搭建和工具配置。在线平台提供了各种功能模块和工具,如代码编辑器、调试器、数据库管理器等,开发者可以直接在浏览器中进行开发和调试。 Java EE开发的颠覆者在线看还提供了自动化部署和持续集成功能。开发者可以将应用程序直接部署到云端服务器,并通过集成的CI/CD工具进行自动化构建和测试。这大大加快了开发和发布过程,提高了开发效率。 此外,Java EE开发的颠覆者在线看还支持容器化部署。开发者可以将应用程序通过容器技术(如Docker)打包,并部署到云平台上的容器集群中。这种方式可以提供更好的水平扩展性和高可用性,同时也简化了应用程序的部署和管理。 总的来说,Java EE开发的颠覆者在线看通过提供现代化的开发工具、自动化部署和持续集成、容器化部署等功能,使Java EE开发更加高效、便捷和可靠。这种技术将成为未来Java EE开发的趋势,为开发者带来更好的开发体验和效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值