在这篇文章中,我们将为您详细介绍#翻译NO.3#---SpringIntegrationFramework的内容,并且讨论关于#springfield翻译的相关问题。此外,我们还会涉及一些关于#翻译N
在这篇文章中,我们将为您详细介绍#翻译NO.3# --- Spring Integration Framework的内容,并且讨论关于#springfield翻译的相关问题。此外,我们还会涉及一些关于#翻译NO.1# --- Spring Integration Framework、#翻译NO.2# --- Spring Integration Framework、#翻译NO.4# --- Spring Integration Framework、#翻译NO.6# --- Spring Integration Framework的知识,以帮助您更全面地了解这个主题。
本文目录一览:- #翻译NO.3# --- Spring Integration Framework(#springfield翻译)
- #翻译NO.1# --- Spring Integration Framework
- #翻译NO.2# --- Spring Integration Framework
- #翻译NO.4# --- Spring Integration Framework
- #翻译NO.6# --- Spring Integration Framework
#翻译NO.3# --- Spring Integration Framework(#springfield翻译)
2.4 Message Endpoints
A Message Endpoint represents the "filter" of a pipes-and-filters architecture. As mentioned above(如上所述), the endpoint''s primary role is to connect application code to the messaging framework and to do so in a non-invasive manner(非侵入的). In other words, the application code should ideally have no awareness(意识) of the Message objects or the Message Channels. This is similar to the role of a Controller in the MVC paradigm. Just as a Controller handles HTTP requests, the Message Endpoint handles Messages. Just as Controllers are mapped to URL patterns, Message Endpoints are mapped to Message Channels. The goal is the same in both cases: isolate(隔离) application code from the infrastructure. These concepts are discussed at length along with all of the patterns that follow in the Enterprise Integration Patterns book. Here, we provide only a high-level description of the main endpoint types supported by Spring Integration and their roles. The chapters that follow will elaborate and provide sample code as well as configuration examples.
消息末端 充当 pipes-and-filters 体系中的 filter角色,正如上文提到的,他主要职责以一种 非侵入的方式连接应用层与消息框架的实现。换一种方式描述,应用层无需太多考虑消息本身或者消息通道。 这个角色类似与MVC模式下的Controller层所提供的功能。而Controller仅仅用于处理HTTP requests,而HTTP requestsMessage Endpoint 处理Message。Controllers提供针对URL的映射机制,Message Endpoints提供针对channel的映射机制。 但是他们具有共同的目标:从消息框架中隔离应用层代码。接下来的章节会用较长的文字来描述相关细节。但是更多的细节请参考 Enterprise Integration Patterns 一书。在这里我们仅仅提供一个大概的与Spring Integration想关的概念解释。同时,接下来的章节 会提供一些代码实例。
2.4.1 Transformer
A Message Transformer(改造器) is responsible for converting a Message''s content or structure and returning the modified Message. Probably the most common type of transformer is one that converts the payload of the Message from one format to another (e.g. from XML Document to java.lang.String). Similarly, a transformer may be used to add, remove, or modify the Message''s header values.
(PS:协议转换器:提供转换消息格式以及内容的功能,大多数的协议转换器用于格式化消息,例如把一个Xml格式的消息 转换为String消息,同时,协议转换器还可以对消息头的信息进行修改。)
2.4.2 Filter
A Message Filter determines(决定) whether a Message should be passed to(被传递到) an output channel at all. This simply requires a boolean test method that may check for a particular payload content type, a property value, the presence of a header, etc. If the Message is accepted, it is sent to the output channel, but if not it will be dropped (or for a more severe implementation, an Exception could be thrown). Message Filters are often used in conjunction(关联) with a Publish Subscribe channel, where multiple consumers may receive the same Message and use the filter to narrow down the set of Messages to be processed based on some criteria.
(PS:消息过滤器 用于判断一个消息是否可以被流转到另外一个输出channl。他需要一个通过判断消息体中 中的负载内容、一个属性值或者消息头中的某一属性是否满足指定的调教,从而返回一个简单的boolean值 来决定。一旦返回为true,则消息允许流转到下一个输出channel中。反之则被丢弃(或者抛出一个异常信息)。 消息过滤器常被用于连接 广播/订阅 模式的通道下,用于对广播的消息进行有效过滤。从而只获取与自己业务相关 的消息进一步进行处理。 )
Be careful not to confuse (混淆) the generic use of "filter" within the Pipes-and-Filters architectural pattern with this specific endpoint type that selectively narrows down the Messages flowing between two channels. The Pipes-and-Filters concept of "filter" matches more closely with Spring Integration''s Message Endpoint: any component that can be connected to Message Channel(s) in order to send and/or receive Messages.(PS:注意:不要混淆这里所描述的"filter" 与 Pipes-and-Filters 体系中的 Filters。 这里的"filter" 是相对狭义的,仅只两个channel之间的消息过滤组件。 Pipes-and-Filters 中 "filter"概念是,与Spring Integration紧密想关的Message Endpoint:任何组件都可以通过 Message Endpoint接入 消息通道进行消息的收发。)
2.4.3 Router
A Message Router is responsible for deciding(决定) what channel or channels should receive the Message next (if any). Typically the decision is based upon the Message''s content and/or metadata available in the Message Headers. A Message Router is often used as a dynamic alternative(选 择) to a statically configured output channel on a Service Activator or other endpoint capable of sending reply Messages. Likewise, a Message Router provides a proactive alternative to the reactive Message Filters used by multiple subscribers as described above.
(PS: 消息路由器 提供判断消息流向那个channel 的功能,通过对消息内容或者消息元数据 判断,从而决定下一个需要流向的channel。消息路由器用于动态选择已经配置好的输出channel通过 Service Activator或者其他具有发送响应消息的Messages,同样,消息路由器也常被使用与替代订阅通道使用。 他能够前提对消息进行分发)
2.4.4 Splitter
A Splitter is another type of Message Endpoint whose responsibility is to accept a Message from its input channel, split that Message into multiple Messages, and then send each of those to its output channel. This is typically used for dividing a "composite" payload object into a group of Messages containing the sub-divided payloads.
(PS:这个章节主要是描述 对消息有效负载的一个分包处理,由于这一块我都没整明白,翻译的主要来自google, 消息分包器 对于来自输入通道的消息进行分包处理。然后分别发送到输出通道。 这通常是用于“复合”的有效载荷对象划分为一组信息,包括细分的有效载荷。)
2.4.5 Aggregator
Basically a mirror-image of the Splitter, the Aggregator is a type of Message Endpoint that receives multiple Messages and combines them into a single Message. In fact, Aggregators are often downstream consumers in a pipeline that includes a Splitter. Technically, the Aggregator is more complex than a Splitter, because it is required to maintain state (the Messages to-be-aggregated), to decide when the complete group of Messages is available, and to timeout if necessary. Furthermore, in case of a timeout, the Aggregator needs to know whether to send the partial results or to discard them to a separate channel. Spring Integration provides aCompletionStrategyas well as configurable settings for timeout, whether to send partial results upon timeout, and the discard channel.
(PS:针对分发器分发的镜像包,聚合器就是针对这些分发的报文进行组装,还原为一条完整的消息。事实上, 聚合器常使用与配置Splitter的下游的channel中。他需要维持被切割的不完整的报文,直到整个完成消息到达。 他支持 消息超时 设置,后面翻译来自google urthermore,在超时的情况下,聚合需要知道是否要发送的部分结果,或将其丢弃到一个单独的通道。 Spring 集成提供了一个的CompletionStrategy以及可配置的超时设置,是否超时后,和丢包信道发送的部分结果。)
2.4.6 Service Activator
A Service Activator is a generic endpoint for connecting a service instance to the messaging system.(一个服务激活是一个通用的端点连接服务实例的消息传递系 统。) The input Message Channel must be configured, and if the service method to be invoked is capable of returning a value, an output Message Channel may also be provided.
(PS:Service Activator 是一个比较通用的endpoint统称,该组件主要用于连接一个消息传输舒服实例。 该组件必须配置 输入channel。当一个service 的方法被调用可以返回一个值。另外该组件支持配置 输出channel)
The output channel is optional, since each Message may also provide its own ''Return Address'' header. This same rule applies for all consumer endpoints.(PS:注意,输出通道是可选的,因为每个消息可以提供自己的返回地址通过消息头的元数据描述信息。 这个规则也适用与其他任何消费型的endpoints)
The Service Activator invokes an operation on some service object to process the request Message, extracting(提取) the request Message''s payload and converting if necessary (if the method does not expect a Message-typed parameter). Whenever the service object''s method returns a value, that return value will likewise be converted to a reply Message if necessary (if it''s not already a Message). That reply Message is sent to the output channel. If no output channel has been configured, then the reply will be sent to the channel specified in the Message''s "return address" if available.
(PS:Service Activator调用服务端一个指定的操作来处理消息,从中提供请求消息的有效负载并且提供 消息类型转换功能。当服务端方法返回一个值,该值同样被转换为响应消息(或者输出到配置输出channel的通道中。 如果没有配置输出通道,他将尝试输出到消息头指定的响应者的地址上。))

2.4.7 Channel Adapter
A Channel Adapter is an endpoint that connects a Message Channel to some other system or transport. Channel Adapters may be either inbound or outbound. Typically, the Channel Adapter will do some mapping between the Message and whatever object or resource is received-from or sent-to the other system (File, HTTP Request, JMS Message, etc). Depending on the transport, the Channel Adapter may also populate or extract Message header values. Spring Integration provides a number of Channel Adapters, and they will be described in upcoming chapters.
(PS:Channel Adapter 主要实现消息通道与其他系统之间的连接。 适配器分对内与对外两种,一般而言,Channel Adapter提供消息与实体系统你哦个[File, HTTP Request, JMS] 之间的发送或者接收映射。按照传输,Channel Adapter同样可以填充以及修改消息Header的中的值。 Spring Integration 提供了种类繁多的适配器,会在之后的章节中介绍。)

An inbound "Channel Adapter" endpoint connects a source system to a MessageChannel.
An outbound "Channel Adapter" endpoint connects a MessageChannel to a target system.
#翻译NO.1# --- Spring Integration Framework
Part II. Overview of Spring Integration Framework
Spring Integration provides an extension of the Spring programming model to support the well-known Enterprise Integration Patterns. It enables lightweight messaging within Spring-based applications and supports integration with external systems via declarative adapters. Those adapters provide a higher-level of abstraction over Spring''s support for remoting, messaging, and scheduling. Spring Integration''s primary goal is to provide a simple model for building enterprise integration solutions while maintaining the separation of concerns that is essential for producing maintainable, testable code.
(PS:Spring Integration框架针对总所周知企业集成模式提供了面向Spring开发模式的解决途径, 他支持基于Spring容器配置轻量级的消息传输以及用过内部提供的多种适配器与外部多个异构的系统进行接入以及通信, 这些适配器是基于Spring容器的高度抽象的组件,涵盖远程调用、消息传输以及任务调度等。Spring Integration的首要 目标是通过简单的对象从而实现企业集成的解决方案,并且该框架聚焦于分离点以及提供良好的维护性与测试性)
2. Spring Integration Overview
2.1 Background
One of the key themes of the Spring Framework is inversion of control. In its broadest sense, this means that the framework handles responsibilities on behalf of the components that are managed within its context. The components themselves are simplified since they are relieved of those responsibilities. For example, dependency injection relieves the components of the responsibility of locating or creating their dependencies. Likewise, aspect-oriented programming relieves business components of generic cross-cutting concerns by modularizing them into reusable aspects. In each case, the end result is a system that is easier to test, understand, maintain, and extend.
(PS:Spring Framework一个显著的特征就是“控制反转”,从字面上理解,框架具有管理并且向外界提供这些组件的功能, ,每个组件仅仅需要关注自己的业务逻辑实现即可,通过依赖注入实现了组件之间的解耦以及相互的依赖关系, 同样,面向切面编程,通过该途径可以统一管理组件的横向特性,使其模块更加具有重用性。总之一句话,这些特性使得 系统更加容易理解、测试、维护与扩展)
Furthermore, the Spring framework and portfolio provide a comprehensive programming model for building enterprise applications. Developers benefit from the consistency of this model and especially the fact that it is based upon well-established best practices such as programming to interfaces and favoring composition over inheritance. Spring''s simplified abstractions and powerful support libraries boost developer productivity while simultaneously increasing the level of testability and portability.
(PS:此外,通过spring框架去构建企业级的应用程序,作为开发人员,要习惯这种模式并且从中获取更好的开发实践,例如 面向接口的模式便于组合,而不仅仅是继承。Spring家族提供了强大而抽象的底层支撑,这样可以使得开发人员提高开发效率。)
Spring Integration is motivated by these same goals and principles. It extends the Spring programming model into the messaging domain and builds upon Spring''s existing enterprise integration support to provide an even higher level of abstraction. It supports message-driven architectures where inversion of control applies to runtime concerns, such as when certain business logic should execute and where the response should be sent. It supports routing and transformation of messages so that different transports and different data formats can be integrated without impacting testability. In other words, the messaging and integration concerns are handled by the framework, so business components are further isolated from the infrastructure and developers are relieved of complex integration responsibilities.
(PS:Spring Integration开发的目标与原则也是遵循上述特性的,他对Spring框架已有消息模型进一步扩展与补充, 并且整个框架体系基于Spring框架开发,并且提供更好的抽象。他支持基于反转控制模式下的消息驱动的实时场景, 例如,什么时候该执行指定的业务逻辑以及什么时候获取发送的响应。同事他还支持消息路由以及实时协议转换, 换句话说:消息发送以及如何集成是该框架所关注的,这样进一步使得业务逻辑分离,从而降低系统的耦合性。)
As an extension of the Spring programming model, Spring Integration provides a wide variety of configuration options including annotations, XML with namespace support, XML with generic "bean" elements, and of course direct usage of the underlying API. That API is based upon well-defined strategy interfaces and non-invasive, delegating adapters. Spring Integration''s design is inspired by the recognition of a strong affinity between common patterns within Spring and the well-known Enterprise Integration Patterns as described in the book of the same name by Gregor Hohpe and Bobby Woolf (Addison Wesley, 2004). Developers who have read that book should be immediately comfortable with the Spring Integration concepts and terminology.
(PS:作为基于Spring编程模式的补充,Spring Integration提供了大量的注解以及Xml命名空间的支持,通过配置为标准 的Spring Bean组件,通过接口模式访问其业务方法。这些API都是面向接口并且都是非侵入式、委托适配器。 该框架的设计灵感来自于企业集成模式以及普通模式的广泛性。 )
2.2 Goals and Principles
Spring Integration is motivated by the following goals:
-
Provide a simple model for implementing complex enterprise integration solutions.(提供解决复杂的企业集成的一个简单的解决措施)
-
Facilitate asynchronous, message-driven behavior within a Spring-based application.(基于Spring框架的 异步的、消息驱动的解决方案)
-
Promote intuitive, incremental adoption for existing Spring users.(更好的与Spring框架集成,提供基于Spring开发的便利)
Spring Integration is guided by the following principles:
-
Components should be loosely coupled for modularity and testability.(组件解耦使其模块化以及可测试化)
-
The framework should enforce separation of concerns between business logic and integration logic.(框架强制分离集成逻辑 与 业务逻辑)
-
Extension points should be abstract in nature but within well-defined boundaries to promote reuse and portability.(扩展点的抽象设计使其更加便于重用以及可移植性)
#翻译NO.2# --- Spring Integration Framework
2.3 Main Components
From the vertical perspective(从纵向角度来看), a layered architecture facilitates separation of concerns(一个分层的体系结构,有利于分离组件), and interface-based contracts between layers promote loose coupling(解耦). Spring-based applications are typically designed this way, and the Spring framework and portfolio provide a strong foundation for following this best practice for the full-stack of an enterprise application. Message-driven architectures add a horizontal perspective, yet these same goals are still relevant. Just as "layered architecture" is an extremely generic and abstract paradigm, messaging systems typically follow the similarly abstract "pipes-and-filters"(管道过滤体系) model. The "filters" represent(代表) any component that is capable of producing and/or consuming messages, and the "pipes"(管道) transport(传输) the messages between filters so that the components themselves remain loosely-coupled(解耦). It is important to note that these two high-level paradigms are not mutually exclusive(互相排斥). The underlying messaging infrastructure that supports the "pipes" should still be encapsulated (封装) in a layer whose contracts are defined as interfaces. Likewise, the "filters" themselves would typically be managed within a layer that is logically above the application''s service layer, interacting with those services through interfaces much in the same way that a web-tier would.
(PS:从纵向角度来看:一个分层清晰的框架体系,更加有利层间的解耦。这是典型的Spring框架系列所遵从的原则。 Spring框架组合为实现full-stack模式的企业集成提供了坚实的基础与良好的实践。 通过消息驱动框架提供的横向透视图,也实现了同样的目标与原则,正如“分层结构”是一个非常通用的和抽象的范式, 通讯系统通常遵循同样抽象的“管道和过滤器”模型,其中“Filter”代表这样一种组件:该组件介于具有生产、消费消息以及 与管道传输消息的中间层,他实现了两者之间的解耦。他们之间并不存在互斥的行为,底层消息传输体系依然是通过既定的 接口封装来支持“pipes”,同样,"filters" 自身受制于应用层之上的逻辑层。通过接口实现业务数据传输的网络传输层。 )
2.3.1 Message
In Spring Integration, a Message is a generic wrapper for any Java object combined with metadata used by the framework while handling that object. It consists of a payload (有效负载) and headers(消息头). The payload can be of any type and the headers hold commonly required information such as id, timestamp, correlation id, and return address. Headers are also used for passing values to and from connected transports. For example, when creating a Message from a received File, the file name may be stored in a header to be accessed by downstream components. Likewise, if a Message''s content is ultimately(最终) going to be sent by an outbound Mail adapter, the various properties (to, from, cc, subject, etc.) may be configured as Message header values by an upstream component. Developers can also store any arbitrary key-value pairs in the headers.
(PS:在Spring Integration框架中,消息是一个很重要的组件,他是包含有元数据的普通的Java 对象。他由消息头与有效负载组成 。其中有效负载可以是任何类型的数据。而消息头定义了传输的一些公用信息,例如消息ID,时间戳、返回地址、相关ID 。同样,消息头也常被用来传输一些数据。例如,文件名称可以被存储在消息头中被下游的组件所访问。 同样,消息内容最终将被发送到外部的邮件适配器,很多属性(来自、抄送、主题等)同样可以定义在消息头中被上游的组件。 开发者存储这些信息是采用 key-value 的模式进行存储的。)
2.3.2 Message Channel
A Message Channel represents(代表、充当) the "pipe" of a pipes-and-filters architecture. Producers send Messages to a channel, and consumers receive Messages from a channel. The Message Channel therefore decouples(解耦) the messaging components, and also provides a convenient point for interception(拦截) and monitoring(监控) of Messages.
(PS:消息通道,充当“pipes-and-filters”体系中的"pipe"角色。生产者发送一个消息到通道,消费者从通道中 获取消息。消息通道仍然是解耦的消息传输组件。该组件一系列例如拦截、监控等特性。 )

A Message Channel may follow either Point-to-Point or Publish/Subscribe semantics. With a Point-to-Point channel, at most one consumer can receive each Message sent to the channel. Publish/Subscribe channels, on the other hand, will attempt to broadcast each Message to all of its subscribers. Spring Integration supports both of these.
(PS:消息通道支持 点对点 与 广播/订阅两种模式,对于点对点模式的通道,通道中的发送的消息只能被一个消费者持有。 而广播/订阅通道则不同,它尝试把消息传输给每一个订阅者,Spring Integration对这两种模式都提供了很好的支持)
Whereas "Point-to-Point" and "Publish/Subscribe" define the two options for how many consumers will ultimately receive each Message, there is another important consideration(考虑,思考): should the channel buffer messages? In Spring Integration, Pollable(可 轮询) Channels are capable of buffering Messages within a queue. The advantage of buffering is that it allows for throttling(节流) the inbound Messages and thereby prevents(防止) overloading(过载) a consumer. However, as the name suggests, this also adds some complexity, since a consumer can only receive the Messages from such a channel if a poller is configured. On the other hand, a consumer connected to a Subscribable Channel is simply Message-driven. The variety(种类) of channel implementations available in Spring Integration will be discussed in detail in Section 3.1.2, “Message Channel Implementations”.
(PS:实际上 点对点 以及 广播/订阅 定义了到底有多少个消费者能从通道中获取一个消息的策略。从而引发另外一个 值得思考的问题,通道是否支持缓存消息呢?在Spring Integration,一种支持轮询机制的通道实现具有缓存消息 的特性。该特性的引入主要是为了节流内部消息向外输出以及防止外部的消息对内部系统造成过载。从“轮询”字面定义可以猜到: 该通道增加了配置复杂性,理由是通过轮询消费者每次仅能获取一条消息。另外一种订阅通道者相反, 消费者连接该通道是基于简单的 消息驱动 模式。Integration 提供了这对两种模式多种实现,从而应用到不同的场景中。 这些会在之后的章节中逐一进行展开描述。)
2.3.3 Message Endpoint消息终端
One of the primary goals(一个很重要的目标) of Spring Integration is to simplify the development of enterprise integration solutions(解决方案) through inversion of control(反转控制). This means that you should not have to implement consumers and producers directly, and you should not even have to build Messages and invoke send or receive operations on a Message Channel. Instead(相反), you should be able to focus on your specific domain model with an implementation based on plain Objects. Then, by providing declarative configuration, you can "connect" your domain-specific(特定领域的,这里可理解为业务逻辑) code to the messaging infrastructure provided by Spring Integration. The components responsible(有责任) for these connections are Message Endpoints. This does not mean that you will necessarily connect your existing application code directly. Any real-world enterprise integration solution will require some amount of code focused upon integration concerns such as routing and transformation. The important thing is to achieve(实现) separation of concerns between such integration logic and business logic. In other words, as with the Model-View-Controller paradigm for web applications, the goal should be to provide a thin but dedicated layer that translates inbound requests into service layer invocations, and then translates service layer return values into outbound replies. The next section will provide an overview of the Message Endpoint types that handle these responsibilities, and in upcoming chapters, you will see how Spring Integration''s declarative configuration options provide a non-invasive way to use each of these.
(PS:一个很重要的目标对于Spring Integration而言,就是利用Spring框架的反转控制提供一个轻量级的企业集成解决方案。 这意味者作为开发人员无需关注生产者与消费者,甚至无需关注消息的创建以及首发等细节。相反,开发者应该重点 关注如何简单实现业务逻辑本身,然后,通过声明式配置,可以把你的业务逻辑与Spring Integration进行无缝集成。 实现这种连接的责任就是 Message Endpoints 所要完成的工作。但是者并不意味着着你可以直接去使用它们。在一个 现实的企业集成场景下,需要大量不同功能的模块实现不同的功能点,例如路由功能、协议转换功能等。 还有一个需要重点指出的就是:他实现业务逻辑与集成逻辑的分离解耦。这个类似与大家熟知的MVC模式。 ........ 下面就是对MVC的描述 自己阅读吧 呵呵~~~~~)
#翻译NO.4# --- Spring Integration Framework
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 模式组件的实现)
#翻译NO.6# --- Spring Integration Framework
3.1.3 Channel Interceptors
One of the advantages of a messaging architecture is the ability to provide common behavior and capture(提取) meaningful(有意义的) information about the messages passing through the system in a non-invasive(非入侵的) way. Since theMessagesare being sent to and received fromMessageChannels, those channels provide an opportunity(时机) for intercepting(拦截) the send and receive operations. TheChannelInterceptorstrategy interface provides methods for each of those operations:
(PS:作为消息传输架构的另外一个高级特性就是通过非入侵的方式从传输的消息中提供有效的数据。 当消息通过 消息通道 从开始发送到被接受,那些通道提供在发送和接受是的拦截机制。 接口ChannelInterceptor 提供 上述环节的拦截方法。)
public interface ChannelInterceptor {
Message<?> preSend(Message<?> message, MessageChannel channel);
void postSend(Message<?> message, MessageChannel channel, boolean sent);
boolean preReceive(MessageChannel channel);
Message<?> postReceive(Message<?> message, MessageChannel channel);
}
After implementing the interface, registering the interceptor with a channel is just a matter of calling:
一个实现该接口的实例,通过如下的方式注册到 指定的 channel 中。
channel.addInterceptor(someChannelInterceptor);
The methods that return a Message instance can be used for transforming the Message or can return ''null'' to prevent(防止) further processing (of course, any of the methods can throw a RuntimeException). Also, thepreReceivemethod can return ''false'' to prevent the receive operation from proceeding.
(PS:上述方法姜返回一个 消息 实例用于传输中的消息抑或返回 null 值,防止之后的环节处理。 preReceive()允许返回 false 值,从而避免接收者继续进行操作。)
Note |
---|
Keep in mind thatreceive()calls are only relevant(相应的) forPollableChannels. In fact theSubscribableChannelinterface does not even define areceive()method. The reason for this is that when a Message is sent to aSubscribableChannelit will be sent directly to one or more subscribers depending on the type of channel (e.g. a PublishSubscribeChannel sends to all of its subscribers). Therefore()因此, thepreReceive(..)andpostReceive(..)interceptor methods are only invoked when the interceptor is applied to aPollableChannel. (PS:切记,receive()方法只能被PollableChannels 接口调用。原因是当消息发送给 SubscribableChannel 是,会被直接 发送到一个或者多个订阅者。因侧 preReceive(..) 与 postReceive(..) 这两个拦截方法及仅仅作用于 PollableChannel 调用 receive 方法前后进行拦截。 ) |
Spring Integration also provides an implementation of the Wire Tap(窃取) pattern. It is a simple interceptor that sends the Message to another channel without otherwise altering the existing flow(不影响当前流程). It can be very useful for debugging and monitoring(监控). An example is shown in the section called “Wire Tap”.
(PS: Spring Integration 同样也提供了 窃听 模式的拦截机制,它是一种简单的拦截实现,当发送的消息被他拦截时, 他会发送一个消息到指定的 channel中,而不应喜爱那个原先的 channle处理流程。 这个对于程序调试以及监控是很有用的,可以异步执行哦)
Because it is rarely(很少) necessary to implement all of the interceptor methods, aChannelInterceptorAdapterclass is also available for sub-classing. It provides no-op methods (thevoidmethod is empty, theMessagereturning methods return the Message as-is, and thebooleanmethod returnstrue). Therefore, it is often easiest to extend that class and just implement the method(s) that you need as in the following example.
(PS:由于在实际情况下,很少需要实现全部的interceptor接口定义的方法。所以系统默认提供了 ChannelInterceptorAdapter 适配器,他实现了全部接口的空方法 ...... 在实际开发的过程中,开发者按需重载指定的方法,从而实现拦截的功能。)
public class CountingChannelInterceptor extends ChannelInterceptorAdapter {
private final AtomicInteger sendCount = new AtomicInteger();
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
sendCount.incrementAndGet();
return message;
}
}
Tip |
---|
The order of invocation for the interceptor methods depends on the type of channel. As described above, the queue-based channels are the only ones where the receive method is intercepted in the first place. Additionally(另外), the relationship between send and receive interception depends on the timing of separate(分开,分离) sender and receiver threads. For example, if a receiver is already blocked while waiting for a message the order could be: preSend, preReceive, postReceive, postSend. However, if a receiver polls after the sender has placed(放置) a message on the channel and already returned, the order would be: preSend, postSend, (some-time-elapses) preReceive, postReceive. The time that elapses in such a case depends on a number of factors and is therefore generally unpredictable (in fact, the receive may never happen!). Obviously(明显), the type of queue also plays a role (e.g. rendezvous vs. priority). The bottom line is that you cannot rely on(依靠) the order beyond the fact that preSend will precede postSend and preReceive will precede(优先于) postReceive. (PS: 拦截器调用的拦截方法 依赖于不同类型的channel,如上所述, 基于队列模式channels 唯一的receive 会在第一时间被拦截。 另外,发送与接受时的拦截调用取决不各自操作的线程。例如,当一个接收者已经被阻塞 而等待一个消息的到达,其调用顺序可能是: preSend, preReceive, postReceive, postSend。 而另外一种场景:当一个消息到达channel并且已经已经返回给sender,这时如果一个消费者轮询,则调用顺序 如下: preSend, postSend, 有时也会有例外:preReceive, postReceive。 中间很费解,我也不是很清楚。 ) |
3.1.4 MessagingTemplate
As you will see when the endpoints and their various configuration options are introduced, Spring Integration provides a foundation for messaging components that enables non-invasive invocation of your application code from the messaging system. However, sometimes it is necessary to invoke the messaging system from your application code. For convenience(方便) when implementing such use-cases, Spring Integration provides aMessagingTemplatethat supports a variety of operations across the Message Channels, including request/reply scenarios. For example, it is possible to send a request and wait for a reply.
(PS: 到目前位置,大致介绍了endpoints 以及 它涉及的各种配置,Spring Integration 提供一种非侵入式的基础的消息传输组件。有一种这样的情况,那就是你不得不在你的应用 代码中调用消息系统的功能。为方便起见(方便)在实施这样的使用情况下, Spring集成提供了一个MessagingTemplate支持在消息渠道的各种操作, 包括请求/应答的情况。例如,它可以发送一个请求,并等待答复。)
MessagingTemplate template = new MessagingTemplate();
Message reply = template.sendAndReceive(someChannel, new GenericMessage("test"));
In that example, a temporary anonymous channel would be created internally by the template. The ''sendTimeout'' and ''receiveTimeout'' properties may also be set on the template, and other exchange types are also supported.
(PS:在该示例中,系统默认创建了一个临时的匿名的channel被 MessagingTemplate。 其中 sendTimeout 与 receiveTimeout 属性同样被 MessagingTemplate 赋值。获取其他类型的修改支持。)
public boolean send(final MessageChannel channel, final Message<?> message) { ... }
public Message<?> sendAndReceive(final MessageChannel channel, final Message<?> request) { .. }
public Message<?> receive(final PollableChannel<?> channel) { ... }
Note
A less invasive approach that allows you to invoke simple interfaces with payload and/or header values instead of Message instances is described in Section 7.2.1, “Enter the GatewayProxyFactoryBean”.
(PS: 还有一种更加简介的途径来通过调允许你访问 有效负载 或者 消息头的的简单接口 而不是消息实体)
关于#翻译NO.3# --- Spring Integration Framework和#springfield翻译的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于#翻译NO.1# --- Spring Integration Framework、#翻译NO.2# --- Spring Integration Framework、#翻译NO.4# --- Spring Integration Framework、#翻译NO.6# --- Spring Integration Framework等相关知识的信息别忘了在本站进行查找喔。
本文标签: