为什么80%的码农都做不了架构师?>>>   

Part III. Core Messaging

This section covers all aspects of the core messaging API in Spring Integration. Here you will learn about Messages, Message Channels, and Message Endpoints. Many of the Enterprise Integration Patterns are covered here as well, such as Filters, Routers, Transformers, Service-Activators, Splitters, and Aggregators. The section also contains material about System Management, including the Control Bus and Message History support.

(PS:该章节覆盖了Spring Integration的核心API讲解,你将了解到Messages, Message Channels, and Message Endpoints 是如何相互依赖的。大多数的企业集成模式的工作原理也基于这些原理[本人觉得 ESB 就是集成之一组件]), 例如 过滤、路由、协议转换、Service-Activators、分包、组包等, 该章节同时涉及一些系统管理方面的资料,例如控制总线以及历史消息支持等
由于该章节比较多,接下来的连续几篇文章都是关于该章节的分段描述。

3. Messaging Channels

3.1 Message Channels

While theMessageplays the crucial(重要) role of encapsulating(封装) data, it is theMessageChannelthat decouples(解 耦) message producers from message consumers.

(PS:消息作为传输数据的载体充当一个很重要的角色。同时,消息通道把消息生产者与消息消费者进行了解耦操作)

3.1.1 The MessageChannel Interface

Spring Integration's top-levelMessageChannelinterface is defined as follows.

(PS:作为Spring Integration 中一个重要的顶层接口类 MessageChannel,定义如下的接口方法: )

public interface MessageChannel {boolean send(Message message);boolean send(Message message, long timeout);
}

When sending a message, the return value will be true if the message is sent successfully. If the send call times out or is interrupted, then it will return false.

(PS:当进行发送消息是,如果返回值为 true,则说明该消息成功发送, 如果发送超时或者被中断,方法将返回 false,表示消息发送失败。)

PollableChannel

Since Message Channels may or may not buffer Messages (as discussed in the overview), there are two sub-interfaces defining the buffering (pollable) and non-buffering (subscribable) channel behavior. Here is the definition ofPollableChannel.

(PS:开始提到 消息通道支持两种模式,是否对支持消息缓存。如下的两个子接口分别针对这两种不同需求进行了定义: pollable[支持缓存] 与 subscribable[不支持缓存] 通道。如下的定义是针对 PollableChannel 接口的定义展现))

public interface PollableChannel extends MessageChannel {Message<?> receive();Message<?> receive(long timeout);}

Similar to the send methods, when receiving a message, the return value will be null in the case of a timeout or interrupt.

(PS:与发送方法类似,当通道接受一个消息时,当方法返回为null 则表明当前接受情况被中断或者超时)

SubscribableChannel

TheSubscribableChannelbase interface is implemented by channels that send Messages directly to their subscribedMessageHandlers. Therefore, they do not provide receive methods for polling, but instead define methods for managing those subscribers:

(PS:SubscribableChannel 继承了 MessageChannel 接口,并再次基础上提供了发送消息给订阅者的接口。 因此,他没有提供用户轮询接收消息的方法,作为代替的方法见下文定义:)

public interface SubscribableChannel extends MessageChannel {boolean subscribe(MessageHandler handler);boolean unsubscribe(MessageHandler handler);}

3.1.2 Message Channel Implementations

Spring Integration provides several different Message Channel implementations. Each is briefly described in the sections below.

(PS:Spring Integration提供了几种不同消息通道的实现类,每个介绍如下描述,
下面介绍的是 class 类是针对上述几个接口实现,上面介绍的是interface 这一点读者要注意

PublishSubscribeChannel

ThePublishSubscribeChannelimplementation broadcasts(广播) any Message sent to it to all of its subscribed handlers. This is most often used for sending Event Messages whose primary role is notification as opposed to(相对于) Document Messages which are generally intended to be processed by a single handler. Note that thePublishSubscribeChannelis intended for(用于) sending only. Since it broadcasts to its subscribers directly when itssend(Message)method is invoked, consumers cannot poll for Messages (it does not implementPollableChanneland therefore has noreceive()method). Instead, any subscriber must be aMessageHandleritself, and the subscriber'shandleMessage(Message)method will be invoked in turn.

(PS:PublishSubscribeChannel(广播订阅通道) 实现广播发送给他的任何消息给全部的订阅者。 这是最常见的用于发送事件消息,其主要作用是通知,而不是文件的消息一般都是为了要处理一个单一的处理。 注意 PublishSubscribeChannel 的send 方法是可以被直接调用的。因为广播的动作是在 send 触发后,自动调用的。 所以 作为消息订阅者,必须拥有一个 MessageHandler 并且注册到 PublishSubscribeChannel 上。 这个不同于 PollableChannel 接口的 receive() 方法,该方法是被客户端自动触发的。)

QueueChannel

TheQueueChannelimplementation wraps a queue. Unlike thePublishSubscribeChannel, theQueueChannelhas point-to-point semantics. In other words, even if(即使) the channel has multiple consumers, only one of them should receive any Message sent to that channel. It provides a default no-argument constructor (providing an essentially unbounded capacity ofInteger.MAX_VALUE) as well as a constructor that accepts the queue capacity:

(PS:QueueChannel 类内部封装了一个 队列用户存储消息。QueueChannel 支持 点对点 的传输策略。 通俗理解:即便QueueChannel 同时拥有对个消费者时,一个消息仅能给其中之一的消费者持有[广播模式对消息进行了复制操作 ]),同时 该类提供了无参数的构造函数[队列的容量默认是nteger.MAX_VALUE],同时也提供了一个指定容量大小 的构造函数,如下所示

public QueueChannel(int capacity)

A channel that has not reached its capacity limit will store messages in its internal queue, and thesend()method will return immediately even if no receiver is ready to handle the message. If the queue has reached capacity, then the sender will block until room is available. Or, if using the send call that accepts a timeout, it will block until either room is available or the timeout period elapses, whichever occurs first. Likewise, a receive call will return immediately if a message is available on the queue, but if the queue is empty, then a receive call may block until either a message is available or the timeout elapses. In either case, it is possible to force(强 制) an immediate(立即) return regardless(无论) of the queue's state by passing a timeout value of 0. Note however, that calls to the no-arg versions ofsend()andreceive()will block indefinitely(无限期).

(PS:当QueueChannel内部的队列容量没有达到使用上限,那么,调用send() 方法发送消息给channel时则会立刻返回, 消息被缓存于消息队列中等待被接收者接受。当内部队列达到上限时,这是在调用 send() 方法,将被阻塞,直到 队列出现空位或者超时被终端才返回。这种情况同样适用于接受。如果队列中存在自己需要的消息,则直接返回。当队列为空时, 接收方法同样阻塞,直到有消息到达或者超时被中断。当然,如果强制想让 receive() 方法不管是否接受到消息都 立即返回,则设置 超时时间 参数 为 0 即可。如果没有参数,阻塞将被无限期延长。)

PriorityChannel

Whereas theQueueChannelenforces first-in/first-out (FIFO) ordering, thePriorityChannelis an alternative implementation that allows for messages to be ordered within the channel based upon a priority. By default the priority is determined by the 'priority' header within each message. However, for custom priority determination logic, a comparator of typeComparator<Message<?>>can be provided to thePriorityChannel's constructor.

(PS:由于 QueueChannel 强制实现 先进先出 的消息消费模式,而 PriorityChannel 实现允许消息在队列内改变 自己顺序的功能(基于优先级)。默认的策略属性 定义在消息头中 的 priority映射的属性。但是需要自定义优先级判断逻辑 ,比较器实现了Comparator<Message<?>>接口,通过调用该接口实现 优先级策略。)

RendezvousChannel

TheRendezvousChannel(会合通道) enables a "direct-handoff"(直接切换) scenario where a sender will block until another party invokes the channel'sreceive()method or vice-versa(反之依然). Internally, this implementation is quite similar to theQueueChannelexcept that it uses aSynchronousQueue(a zero-capacity implementation ofBlockingQueue). This works well in situations(场景) where the sender and receiver are operating in different threads but simply dropping the message in a queue asynchronously is not appropriate(适当). In other words, with aRendezvousChannelat least the sender knows that some receiver has accepted the message, whereas(然而) with aQueueChannel, the message would have been stored to the internal queue and potentially never received.

(PS: RendezvousChannel 实现 "direct-handoff" 功能,当一个消息生产者发送消息到 RendezvousChannel后将被阻塞, 直到消费者调用 该 channel的 receive() 方法[反之亦然]。其内部实现是这样子的, 他实现很类似 QueueChannel 除了他使用的队列是 SynchronousQueue[JDK5 有标准提供,读者自己翻阅资料:队列大小 为 0 的 BlockingQueue的实现。],RendezvousChannel 是为了实现当 消费 与 生产 消息位于不同的线程时进行同步 使用。这样发送者可以明确知道消息是否被消费。而QueueChannel 则无法提供该功能的反馈。 )

Keep in mind(切记,牢记) that all of these queue-based channels are storing messages in-memory only by default. When persistence(持久化) is required, you can either provide a 'message-store' attribute within the 'queue' element to reference a persistent MessageStore implementation, or you can replace the local channel with one that is backed by a persistent broker, such as a JMS-backed channel or Channel Adapter. The latter option allows you to take advantage of any JMS provider's implementation for message persistence, and it will be discussed in Chapter 19, JMS Support. However, when buffering in a queue is not necessary, the simplest approach is to rely upon theDirectChanneldiscussed next.

(PS:切记:到目前为止描述的所有基于 队列模式的 消息通道对消息的缓存默认都是采用内存的。 如果需要对消息进行持久化是,你需要提供一个 'message-store' 属性的实现 用户 可持久化消息的 队列机制的实现, 或者 用一个持久化的队列机制取代 当前的 channel实现,例如 JMS 。文章在 19章对此有详细的描述。 反之,当不需要对消息进行缓存是,由一个比较简单的实现 那就是 DirectChannel。 )

TheRendezvousChannelis also useful for implementing request-reply operations. The sender can create a temporary, anonymous instance ofRendezvousChannelwhich it then sets as the 'replyChannel' header when building a Message. After sending that Message, the sender can immediately call receive (optionally providing a timeout value) in order to block while waiting for a reply Message. This is very similar to the implementation used internally by many of Spring Integration's request-reply components.

(PS: RendezvousChannel 常被应用与应答模式的场景,消息发送者创建一个临时的,匿名的 RendezvousChannel 实例 作为发送消息的返回通道被消息引用。 当消息发送后,发送者可以立刻 对 该临时的 channel 调用 receive 方法 ,这样在响应返回之前程序处于阻塞状态,直到响应返回或者超时。这是一个 Spring Integration 框架中最简单的 request-reply 模式组件的实现)

转载于:https://my.oschina.net/qfhxj/blog/95351

#翻译NO.4# --- Spring Integration Framework相关推荐

  1. #翻译NO.3# --- Spring Integration Framework

    为什么80%的码农都做不了架构师?>>>    2.4 Message Endpoints A Message Endpoint represents the "filte ...

  2. #翻译NO.5# --- Spring Integration Framework

    为什么80%的码农都做不了架构师?>>>    本人觉得这一章很重要,那就是 Spring默认的channel 的实现 DirectChannel,这个要大家多按照原文理解,开发者为 ...

  3. Spring Integration Framework简介

    我们非常了解Spring框架和JMS . 在本文中,我们将介绍称为Spring Integration的企业集成框架 . Spring Integration是一个开源企业集成框架,可增强Spring ...

  4. amqp rabbitmq_通过Spring Integration和RabbitMQ获得高可用性的AMQP支持的消息通道

    amqp rabbitmq Spring Integration消息通道默认情况下将消息存储在内存中. 这是因为内存速度快,易于实现,并且不会增加网络成本. 但是,在某些情况下,这可能会引起问题,因为 ...

  5. 通过Spring Integration和RabbitMQ获得高可用性的AMQP支持的消息通道

    Spring Integration消息通道默认情况下将消息存储在内存中. 这是因为内存速度快,易于实现,并且不会增加网络成本. 但是,在某些情况下,这可能会引起问题,因为如果应用程序崩溃或服务器意外 ...

  6. streaming api_通过Spring Integration消费Twitter Streaming API

    streaming api 1.概述 众所周知, Spring Integration具有用于与外部系统交互的大量连接器. Twitter也不例外,而且很长一段时间以来,因为Spring Social ...

  7. 通过Spring Integration消费Twitter Streaming API

    1.概述 众所周知, Spring Integration具有用于与外部系统交互的大量连接器. Twitter也不例外,而且很长一段时间以来,因为Spring Social一直是一个开箱即用的解决方案 ...

  8. Spring Integration完整示例

    本文是我们名为" Spring Integration for EAI "的学院课程的一部分. 在本课程中,向您介绍了企业应用程序集成模式以及Spring Integration如 ...

  9. 什么是Spring Integration?

    随着Spring Integration项目逐渐获得越来越多的采用和兴趣,企业集成或企业开发领域的开发人员很可能会遇到它. 他们可能会发现它很有趣,但并没有完全理解它的含义,所要解决的问题,可以从中获 ...

最新文章

  1. HDU1402(高精度乘法)
  2. python爬虫换电脑不能运行_python爬虫程序运行失败,求原因
  3. 【解题报告】【HDOJ1233】【最小生成树】还是畅通工程
  4. Java隐含对象实验报告,JSP隐含对象response实现文件下载
  5. 面试官:Spring事务失效的场景有哪些?如何解决?
  6. C语言基础教程之如何定义变量
  7. linux中/etc/fstab文件删除或修改了,导致系统无法启动
  8. Java 8新特性探究(十一)Base64详解
  9. 20181215《linux设备驱动开发详解》宋宝华 学习笔记(1)
  10. 什么是网络割接【转自红茶三杯】
  11. 基于单片机的功放protues_基于单片机的音乐播放器设计
  12. 核酸检测系统的潜在性能问题猜想
  13. 六款最佳、免费的网络延迟测试工具
  14. firefox的XPCOM的COM编程
  15. 单点登录(SSO)、CAS介绍
  16. colorAccent,colorPrimary,colorPrimaryDark做什么的?
  17. Shell脚本中的流程控制,如if判断,case语句,for循环,while循环
  18. 【学习记录】HT32F52352舵机控制
  19. 天才小毒妃 第879章 不许欺负伤残人氏
  20. android高德地图使用教程,Android 之 高德地图学习 一 基本地图

热门文章

  1. 搞机器学习需要数学基础吗?
  2. hashmap 不释放空间_刁难问题,为什么HashMap默认容量为16加载因子为0.75
  3. oracle11g安装到第7步,centos7安装oracle11g到这一步卡在了?也没有安装界面
  4. android 按钮点击间隔,如何自定义android中按下的长/延迟按钮的时间间隔
  5. c语言中二维数组怎么,c语言中什么是二维数组
  6. linux 8051 编译,[编译] 3、在Linux下搭建51单片机的开发烧写环境(makefile版)
  7. 编写登录成功和失败的处理器
  8. 多租户的数据库方案分析
  9. mybatis-Batch Executor
  10. 数据库压力变大,读写分离吧