Spring 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)}.
*