【From】 http://blog.csdn.net/w_x_z_/article/details/53316618

pring Ingegration 提供了基于Spring的EIP(Enterprise Integration Patterns,企业集成模式)的实现。Spring Integration 主要解决的问题是不同系统之间交互的问题,通过异步消息驱动来达到系统交互时系统之间的松耦合。

Spring Integration 主要有Message、Channel、Message EndPoint组成。

Message

Message是用来在不同部分之间传递的数据。Message有两部分组成:消息体(payload)与消息头(header)。消息体可以是任何数据类型;消息头表示的元数据就是解释消息体的内容。

/**
* A generic message representation with headers and body.
*
* @author Mark Fisher
* @author Arjen Poutsma
* @since 4.0
* @see org.springframework.messaging.support.MessageBuilder
*/
public interface Message<T> { /**
* Return the message payload.
*/
T getPayload(); /**
* Return message headers for the message (never {@code null} but may be empty).
*/
MessageHeaders getHeaders(); }

Channel

在消息系统中,消息发送者发送消息到通道(Channel),消息接受者从通道(Channel)接收消息。

1、顶级接口

(1) MessageChannel

MessageChannel 是Spring Integration消息通道的顶级接口:

public interface MessageChannel {

    /**
* Constant for sending a message without a prescribed timeout.
*/
long INDEFINITE_TIMEOUT = -1; /**
* Send a {@link Message} to this channel. If the message is sent successfully,
* the method returns {@code true}. If the message cannot be sent due to a
* non-fatal reason, the method returns {@code false}. The method may also
* throw a RuntimeException in case of non-recoverable errors.
* <p>This method may block indefinitely, depending on the implementation.
* To provide a maximum wait time, use {@link #send(Message, long)}.
* @param message the message to send
* @return whether or not the message was sent
*/
boolean send(Message<?> message); /**
* Send a message, blocking until either the message is accepted or the
* specified timeout period elapses.
* @param message the message to send
* @param timeout the timeout in milliseconds or {@link #INDEFINITE_TIMEOUT}
* @return {@code true} if the message is sent, {@code false} if not
* including a timeout of an interrupt of the send
*/
boolean send(Message<?> message, long timeout); }

MessageChannel 有两大子接口,分别是PollableChannel (可轮询)和SubscribableChannel(可订阅)。我们所有的消息通道类都是现实这两个接口。

(2) PollableChannel

PollableChannel 具备轮询获得消息的能力。

public interface PollableChannel extends MessageChannel {

    /**
* Receive a message from this channel, blocking indefinitely if necessary.
* @return the next available {@link Message} or {@code null} if interrupted
*/
Message<?> receive(); /**
* Receive a message from this channel, blocking until either a message is available
* or the specified timeout period elapses.
* @param timeout the timeout in milliseconds or {@link MessageChannel#INDEFINITE_TIMEOUT}.
* @return the next available {@link Message} or {@code null} if the specified timeout
* period elapses or the message reception is interrupted
*/
Message<?> receive(long timeout); }

(3) SubscribableChannel

SubscribableChannel 发送消息给订阅了MessageHanlder的订阅者

public interface SubscribableChannel extends MessageChannel {

    /**
* Register a message handler.
* @return {@code true} if the handler was subscribed or {@code false} if it
* was already subscribed.
*/
boolean subscribe(MessageHandler handler); /**
* Un-register a message handler.
* @return {@code true} if the handler was un-registered, or {@code false}
* if was not registered.
*/
boolean unsubscribe(MessageHandler handler); }
 2、常用消息通道

(1)、PublishSubscribeChannel

PublishSubscribeChannel允许广播消息给所有订阅者,配置方式如下:

    /**
* 允许广播消息给所有订阅者,当前消息通道的id为publishSubscribeChannel
* @return
*/
@Bean
public PublishSubscribeChannel publishSubscribeChannel(){
PublishSubscribeChannel channel = new PublishSubscribeChannel();
return channel;
}

其中,当前消息通道的id为publishSubscribeChannel。

(2)、QueueChannel

QueueChannel允许消息接收者轮询获得消息,用一个队列(queue)接收消息,队列的容量大小可配置,配置方式如下:

    @Bean
public QueueChannel queueChannel(){
QueueChannel channel = new QueueChannel(10);
return channel;
}

其中,QueueChannel构造参数10即为队列的容量。

(3)、PriorityChannel

PriorityChannel可按照优先级将数据存储到队列,它依据于消息的消息头priority属性,配置方式如下:

    @Bean
public PriorityChannel priorityChannel(){
PriorityChannel channel = new PriorityChannel(10);
return channel;
}

(4)、RendezvousChannel

RendezvousChannel确保每一个接收者都接收到消息后再发送消息,配置方式如下:

    @Bean
public RendezvousChannel rendezvousChannel(){
RendezvousChannel channel = new RendezvousChannel();
return channel;
}

(5) DirectChannel

DirectChannel是Spring Integration默认的消息通道,它允许将消息发送给为一个订阅者,然后阻碍发送直到消息被接收,配置方式如下:

    @Bean
public DirectChannel directChannel(){
DirectChannel channel = new DirectChannel();
return channel;
}

(6)、ExecutorChannel

ExecutorChannel可绑定一个多线程的task executor,配置方式如下:

    @Bean
public ExecutorChannel executorChannel(){
ExecutorChannel channel = new ExecutorChannel(executor());
return channel;
} @Bean
public Executor executor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}

3、通道拦截器

Spring Integration给消息通道提供了通道拦截器(ChannelInterceptor),用来拦截发送和接收消息的操作.

ChannelInterceptor接口定义如下,我们只需要实现这个接口即可:

 public interface ChannelInterceptor {

        Message<?> preSend(Message<?> message, MessageChannel channel);

        void postSend(Message<?> message, MessageChannel channel, boolean sent);

        void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex);

        boolean preReceive(MessageChannel channel);

        Message<?> postReceive(Message<?> message, MessageChannel channel);

        void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex);

    }

通过如下代码给所有的channel增加拦截器

channel.addInterceptor(someInterceptor);

Message EndPoint

消息端点(Message EndPoint)是真正处理消息的(Message)组件,它还可以控制通道的路由。我们可用的消息端点包含如下:

(1) Channel Adapter

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

Spring Integration内置了如下的适配器:

RabbitMQ、Feed、File、FTP/SFTP、Gemfire、HTTP、TCP/UDP、JDBC、JPA、JMS、Mail、MongoDB、Redis、RMI
Twitter、XMPP、WebServices(SOAP、REST)、WebSocket

(2) Gateway

消息网关(Gateway)类似于Adapter,但是提供了双向的请求/返回集成方式,也分为入站(inbound)和出站(outbound)。 
Spring Integration 对响应的Adapter都提供了Gateway。

(3) Service Activator

Service Activator 可调用Spring的Bean来处理消息,并将处理后的结果输出到指定的消息通道。

(4) Router

路由(Router) 可根据消息体内容(Payload Type Router)、消息头的值(Header Value Router) 以及定义好的接收表(Recipient List Router) 作为条件,来决定消息传递到的通道。

(5) Filter

过滤器(Filter) 类似于路由(Router),不同的是过滤器不决定消息路由到哪里,而是决定消息是否可以传递给消息通道。

(6) Splitter

拆分器(Splitter)将消息拆分为几个部分单独处理,拆分器处理的返回值是一个集合或者数组。

(7) Aggregator

聚合器(Aggregator)与拆分器相反,它接收一个java.util.List作为参数,将多个消息合并为一个消息。

(8) Enricher

当我们从外部获得消息后,需要增加额外的消息到已有的消息中,这时就需要使用消息增强器(Enricher)。消息增强器主要有消息体 
增强器(Payload Enricher)和消息头增强器(Header Enricher)两种。

(9) Transformer

转换器(Transformer)是对获得的消息进行一定的转换处理(如数据格式转换).

(10) Bridge

使用连接桥(Bridge)可以简单的将两个消息通道连接起来。

[转] Spring Integration 系统集成的更多相关文章

  1. Spring Integration - 自动轮询发送手机短信

    Spring Integration 配置 <?xml version="1.0" encoding="UTF-8"?> <beans xml ...

  2. spring Integration服务总线

    最新项目中使用数据交换平台,主要通过交换平台抓取HIS数据库医生医嘱检查检验等数据以及FTP上的txt文件,html等病程文件,生成XML文件,之后通过业务系统按业务规则对数据进行处理,再将其存入数据 ...

  3. Spring Integration实现分布式锁

    学习本篇之前,可以先看下文章 什么是分布式锁,了解下基本概念. 之前都是手写一个分布式锁,其实Spring早就提供了分布式锁的实现.早期,分布式锁的相关代码存在于Spring Cloud的子项目Spr ...

  4. Spring Integration概述

    1.   Spring Integration概述 1.1     背景 Spring框架的一个重要主题是控制反转.从广义上来说,Spring处理其上下文中管理的组件的职责.只要组件减轻了职责,它们同 ...

  5. ref:Spring Integration Zip 不安全解压(CVE-2018-1261)漏洞分析

    ref:https://mp.weixin.qq.com/s/SJPXdZWNKypvWmL-roIE0Q 0x00 漏洞概览 漏洞名称:Spring Integration Zip不安全解压 漏洞编 ...

  6. Spring Integration - WS Outbound Gateway

    1.通过MessageSender客户化http连接参数 AbstractHttpWebServiceMessageSender有若干实现子类: - CommonsHttpMessageSender( ...

  7. springboot实现分布式锁(spring integration,redis)

    Springboot实现分布式锁(Spring Integration+Redis) 一.在项目的pom.xml中添加相关依赖 1)Spring Integration依赖 <dependenc ...

  8. Spring Integration

    @ContextConfiguration directs Spring's test runner to locate a configuration file with the same name ...

  9. Spring Integration Zip不安全解压(CVE-2018-1261)漏洞复现

    不敢说分析,还是太菜了,多学习. 文章来源: 猎户安全实验室 存在漏洞的源码下载地址:https://github.com/spring-projects/spring-integration-ext ...

随机推荐

  1. jq获取table总行数

    var rows = $('table').find("tr").length;

  2. 用JQuery获取输入框中的光标位置

    (function ($, undefined) { $.fn.getCursorPosition = function () { var el = $(this).get(0); var pos = ...

  3. (转)使用Jquery+EasyUI 进行框架项目开发案例讲解之四---组织机构管理源码分享

    原文地址:http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章  <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码 ...

  4. 编写高质量代码改善C#程序的157个建议——建议16:元素数量可变的情况下不应使用数组

    建议16:元素数量可变的情况下不应使用数组 在C#中,数组一旦被创建,长度就不能改变.如果我们需要一个动态且可变长度的集合,就应该使用ArrayList或List<T>来创建. 而数组本身 ...

  5. delphi 指针 认识

    delphi 指针分为类型指针和无类型指针: 类型指针分为PChar.PInteger.PString等. 无类型指针Pointer. PPChar/PP...为指针的指针 @和Addr一样,为获取变 ...

  6. HTML5 Canvas核心技术图形动画与游戏开发 ((美)David Geary) 中文PDF扫描版​

    <html5 canvas核心技术:图形.动画与游戏开发>是html5 canvas领域的标杆之作,也是迄今为止该领域内容最为全面和深入的著作之一,是公认的权威经典.amazon五星级超级 ...

  7. [.net 多线程]ThreadPool的安全机制

    ThreadPool类,有两个方法我们没有用到,UnsafeQueueUserWorkItem 和UnsafeRegisterWaitForSingleObject. 为了完全理解这些方法,首先,我们 ...

  8. 指针和动态分配内存 (不定长度数组)------新标准c++程序设计

    背景: 数组的长度是定义好的,在整个程序中固定不变.c++不允许定义元素个数不确定的数组.例如: int n; int a[n]; //这种定义是不允许的 但是在实际编程中,往往会出现要处理的数据数量 ...

  9. 自用 Pycharm 主题配色分享(主题才是开发第一生产力)

    写在前面的话 是的,我又回来了,上一篇[使用 Visual Studio Code(VSCode)搭建简单的 Python + Django 开发环境]才说真香,结果用两天就发现很多恶心的问题拦住了菜 ...

  10. Spring AOP的实现机制

    AOP(Aspect Orient Programming),一般称为面向切面编程,作为面向对象的一种补充,用于处理系统中分布于各个模块的横切关注点,比如事务管理,日志,缓存等等.AOP 实现的关键在 ...