Apache Camel - 5 - Camel消息模型

Apache Camel消息模型

Apache Camel相关代码已经上传GitHub,需要的自取:GitHub - Apache Camel 完整Demo

如果觉得还行,麻烦点个Star。

 

上面一篇文章中,主要引用、学习的是《架构设计:系统间通信(36)——Apache Camel快速入门(上)》中的内容。

我们再来看看Camel消息模型:

 

Camel’s message model

In Camel, there are two abstractions for modeling messages, both of which we’ll cover in this section.

■ org.apache.camel.Message — The fundamental  entity  containing  the  data being carried and routed in Camel

■ org.apache.camel.Exchange — The Camel abstraction for an exchange of messages. This exchange of messages has an “in” message and as a reply, an “out” message

在Camel中,有两个用于传递消息的抽象模型,我们将在本节中介绍这两个抽象。

■org.apache.camel.Message - 包含在Camel传输和路由数据中的基本实体

■org.apache.camel.Exchange - 用于交换消息的Camel抽象。 这种消息交换具有“输入”消息和作为回复的“输出”消息的作用。

我们先看下Camel消息模型的图例:

Apache Camel - 5 - Camel消息模型

左图:《Mastering Apache Camel》

右图:《Camel in Action》

 

Exchange :

Camel中用于交换消息的抽象模型,可以把它理解为一个消息块。

在路由消息传递的过程中,Exchange 是最大的消息容器。

An exchange in  Camel  is  the  message’s  container  during  routing.  An  exchange  also provides support for the various types of interactions between systems, also known as message exchange patterns (MEPs). MEPs are used to differentiate between one-way and request-response messaging styles. The Camel exchange holds a pattern property that can be either

 

■ InOnly—A one-way message (also known as an Eventmessage). For example, JMS messaging is often one-way messaging.

■ InOut—A  request-response  message. For example, HTTP-based transports are often request  reply,  where  a  client  requests  to retrieve a web page, waiting for the reply from the server.

Camel中的Exchange是整个路由期间消息的容器。 Exchange还为系统之间的各种类型的交互提供支持,也称为消息交换模式(MEP)。 MEP用于区分单向和请求-响应消息传递样式。 Camel Exchange 持有一个模式属性,可以是

■InOnly-A 单向消息(也称为Eventmessage)。 例如,JMS消息传递通常是单向消息传递。

■InOut-A 请求 - 响应消息。 例如,基于HTTP的传输通常是请求回复,其中客户端请求检索网页,等待来自服务器的回复。

 

Exchange ID :

A  unique ID that  identifies the exchange. Camel will generate a default unique ID, if you don’t explicitly set one.

标识交换的唯一ID。 如果您没有明确设置,则Camel将生成默认的唯一ID。

 

MEP :

A  pattern  that  denotes  whether you’re using the InOnlyor InOutmessaging style. When the pattern is InOnly, the exchange  contains  an  in  message.  For InOut, an out message also exists that contains the reply message for the caller.

表示您是否使用InOnlyor InOutmessaging样式的模式。 当模式为InOnly时,交换包含一条消息。

对于InOut,还存在包含呼叫者的回复消息的输出消息。

 

Exception :

If an error occurs at any time during routing, an Exceptionwill be set in the exception field.

如果在路由期间的任何时候发生错误,则会在异常字段中设置Exception。

 

Properties :

Similar to  message headers,  but  they  last  for  the  duration  of  the entire  exchange.  Properties  are  used  to  contain  global-level  information, whereas message headers are specific to a particular message. Camel itself will add various properties to the exchange during routing. You, as a developer, can store and retrieve properties at any point during the lifetime of an exchange.

与 message headers类似,但它们持续整个交换的持续时间。 Properties用于包含全局级信息,而message headers特定于特定消息。

Camel本身将在路由期间向交换添加各种属性。 作为开发人员,您可以在交换的生命周期内的任何时刻存储和检索属性。

 

In Message :

This is the input message, which is mandatory. The in message contains the request message

这是输入消息,这是必需的。 in message包含请求消息

 

Out Message :

This is an optional message that only exists if the MEPis InOut. The out message contains the reply message.

这是一个可选消息,仅在MEPis InOut时才存在。 out message包含回复消息。

 

Message :

Messages are the entities used by systems to communicate with each other when using messaging channels. Messages flow in one  direction from a sender to a receiver, as illustrated in figure 1.3.

Messages have a body (a payload), headers, and optional attachments, as illustrated in figure 1.4.

Messages are uniquely identified with an identifier of type java.lang.String.  The  identifier’s  uniqueness  is  enforced and  guaranteed  by  the  message  creator,  it’s  protocol  dependent, and it doesn’t have a guaranteed format. For protocols that  don’t  define  a  unique  message  identification  scheme, Camel uses its own UIDgenerator.

Messages 是系统在使用消息传递通道时用于彼此通信的实体。 消息从发送方向接收方向流动,如图1.3所示。
消息具有正文(有效负载),标题和可选附件,如图1.4所示。

消息使用java.lang.String类型的标识符唯一标识。 标识符的唯一性由消息创建者强制执行并保证,它依赖于协议,并且没有保证格式。 对于未定义唯一消息标识方案的协议,Camel使用自己的UID生成器。

注意:这里的Message并没有区分In Message / Out Message。

Apache Camel - 5 - Camel消息模型

 

Headers AND Attachments :

Headers are values associated with the message, such as sender identifiers, hints about content encoding, authentication information, and so on. Headers are name-value pairs; the name is a unique, case-insensitive string, and the value is of type java.lang.Object. This means that Camel imposes no constraints on the type of the headers. Headers are stored as a map within the message. A message can also have optional attachments, which are typically used for the web service and email components.

Headers是与消息相关的值,例如发送者标识符,关于内容编码的提示,认证信息等等。

Headers是名称 - 值对; 该名称是唯一的,不区分大小写的字符串,其值是java.lang.Object类型。

这意味着 对Headers的类型没有限制。

Headers以消息内的映射形式存储。

消息还可以具有可选的ATTACHMENTS,这些附件通常用于Web服务和电子邮件组件。

Headers 可以理解为HTTP中的请求头。

Attachments 用来存放附属信息

 

Body :

The body is of type java.lang.Object. That means that a message can store any kind of content. It also means that it’s up to the application designer to make sure that the receiver can understand the content of the message. When the sender and receiver use different body formats, Camel provides a number of mechanisms to transform the data into an acceptable format, and in many cases the conversion happens automatically with type converters, behind the scenes.

Body

body是java.lang.Object类型。

这意味着一条消息可以存储任何类型的内容。

这也意味着应用程序设计者应该确保接收者能够理解消息的内容。

当发送者和接收者使用不同的主体格式时,Camel提供了一些将数据转换为可接受的格式的机制,并且在很多情况下,转换在后台自动进行。

 

希望你对Camel的消息有一个很好的理解。毕竟,Camel最重要的方面是路由消息。现在,已经准备好了解有关Camel更多信息。