Spring Integration

  1. Spring Integration主要解决的问题是不同系统之间交互的问题,通过异步消息驱动来达到系统交互时系统之间的松耦合。
  2. Spring Integration主要由Message,Channel和Message EndPoint组成。
  3. Message:用来在不同部分之间传递的数据。由消息体(payload)和消息头(header)。消息体可以是任何数据类型(xml,json,Java对象);消息头的元数据就是解释消息体的内容。
  4. Channel:消息发送者发送消息到通道,消息接收者从通道接收消息。
  5. Message EndPoint:是真正处理xiao消息的组件,还可以控制通道的路由。可用的消息端点有:

(1)Channel Adapter:是一种连接外部或传输协议的端点,分为入站(inbound)和出站(outbound),通道适配器是单向的,入站通道适配器只支持接收消息,出站通道适配器只支持输出消息。

      6. Spring Integration Java DSL :Spring Integration 提供IntegrationFlow来定义系统继承流程,

        通过IntegrationFlows和IntegrationFlowBuilder来实现使用Fluent API来定义流程。

        一个简单的流程定义如下:

@Bean public IntegrationFlow demoFlow() { return IntegrationFlows.from("input")//从Channel input获取消息 .<SyndEntry, String> transform(Integer::parseInt)//将消息转换成整数 .get();//获取集成流程并注册为Bean }

完整示例:

package com.integration;

import com.rometools.rome.feed.synd.SyndEntry; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.core.io.Resource; import org.springframework.integration.dsl.IntegrationFlow; import org.springframework.integration.dsl.IntegrationFlows; import org.springframework.integration.dsl.channel.MessageChannels; import org.springframework.integration.dsl.core.Pollers; import org.springframework.integration.dsl.file.Files; import org.springframework.integration.dsl.mail.Mail; import org.springframework.integration.feed.inbound.FeedEntryMessageSource; import org.springframework.integration.file.support.FileExistsMode; import org.springframework.integration.scheduling.PollerMetadata;

import java.io.File; import java.io.IOException;

import static java.lang.System.getProperty;

@SpringBootApplication public class IntegrationApplication {

//自动获得资源 @Value("https://spring.io/blog.atom") Resource resource;

public static void main(String[] args) { SpringApplication.run(IntegrationApplication.class, args); }

//配置默认的轮询方式 @Bean(name = PollerMetadata.DEFAULT_POLLER) public PollerMetadata poller() { return Pollers.fixedRate(500).get(); }

@Bean public FeedEntryMessageSource feedMessageSource() throws IOException { FeedEntryMessageSource messageSource = new FeedEntryMessageSource(resource.getURL(), "news"); return messageSource; }

@Bean public IntegrationFlow myFlow() throws IOException { return IntegrationFlows.from(feedMessageSource()) //通过route来选择路由,消息体(payload)的类型为SyndEntry,判断条件类型为String //判断的值通过payload获得的分类(Categroy) //不同的分类值转向不同的消息通道 .<SyndEntry, String>route(payload -> payload.getCategories().get(0).getName(), mapping -> mapping.channelMapping("releases", "releasesChannel") .channelMapping("engineering", "engineeringChannel") .channelMapping("news", "newsChannel")) .get();//获得IntegrationFlow实体,配置为Spring的Bean }

//releases流程 @Bean public IntegrationFlow releasesFlow() { return IntegrationFlows.from(MessageChannels.queue("releasesChannel", 10)) //从releasesChannel获取数据 .<SyndEntry, String>transform( //transform方法做数据转换,payload类型为SynEntry,将其转换为字符串类型, // 并自定义数据的格式 payload -> "《" + payload.getTitle() + "》" + payload.getLink() + getProperty("line.separator")) //handle方法处理file的出站适配器,Files类是由Spring Integration Java DSL提供的 // Fluent API 用来构造文件输出的适配器 .handle(Files.outboundAdapter(new File("g:/springblog")) .fileExistsMode(FileExistsMode.APPEND) .charset("UTF-8") .fileNameGenerator(message -> "releases.txt") .get()) .get(); }

//engineering流程,与releases流程相同 @Bean public IntegrationFlow engineeringFlow() { return IntegrationFlows.from(MessageChannels.queue("engineeringChannel", 10)) .<SyndEntry, String> transform( e -> "《" + e.getTitle() + "》" + e.getLink() + getProperty("line.separator")) .handle(Files.outboundAdapter(new File("g:/springblog")) .fileExistsMode(FileExistsMode.APPEND) .charset("UTF-8") .fileNameGenerator(message -> "engineering.txt") .get()) .get(); }

//news流程 @Bean public IntegrationFlow newsFlow() { return IntegrationFlows.from(MessageChannels.queue("newsChannel", 10)) .<SyndEntry, String> transform( payload -> "《" + payload.getTitle() + "》" + payload.getLink() + getProperty("line.separator")) //提供enrichHeaders方法增加消息头的信息 .enrichHeaders( Mail.headers() .subject("来自Spring的新闻") .to("guang2_tan@126.com") .from("guang2_tan@126.com")) //邮件发送的相关信息通过Spring Integration Java DSL提供的Mail的headers方法来构造 //使用handle方法自定义邮件发送的出站适配器,使用Spring Integration Java DSL提供的Mail.outboundAdapter来构造 //使用guang2_tan@126.com向自己发送邮件 .handle(Mail.outboundAdapter("smtp.126.com") .port(25) .protocol("smtp") .credentials("guang2_tan@126.com", "abc1234") .javaMailProperties(p -> p.put("mail.debug", "false")), e -> e.id("smtpOut")) .get(); } }

可能会出现:javax.mail.AuthenticationFailedException: 550

解决办法:可能邮箱没有开通pop/stmp协议

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )