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消息模型的图例:

左图:《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。

 

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更多信息。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Apache Camel和JBang是两个不同的工具,但可以配合使用。Apache Camel是一个源的集成框架,用于构建面向企业的集成应用程序,而JBang是一个基于Java的脚本引擎。 如果你想在使用Apache Camel时结合JBang进行脚本编写和执行,可以按照以下步骤操作: 1. 首先,确保你已经安装了Java和Apache Camel。你可以从官方网站下载和安装最新版本的Apache Camel。 2. 接下来,你需要安装JBang。JBang可以通过其官方网站或GitHub仓库进行下载和安装。 3. 安装完成后,你可以创建一个新的JBang脚本文件,用于编写和执行Apache Camel的集成路由。你可以使用任何文本编辑器打开该文件,并在其中编写你的Camel路由逻辑。 4. 在脚本文件的开头,添加`#!/usr/bin/env jbang`这一行,以告诉系统使用JBang来执行该脚本。 5. 在脚本文件中,导入所需的Apache Camel类和其他必要的依赖项。你可以使用JBang的内置依赖管理功能来自动下载和管理这些依赖项。 6. 编写你的Apache Camel路由逻辑。你可以使用Camel的Java DSL或XML DSL来定义和配置路由。在脚本中,你可以使用Java代码来编写这些路由逻辑。 7. 最后,保存脚本文件,并在命令行中执行它。你可以使用`jbang <脚本文件名>`命令来运行该脚本。 通过这种方式,你可以使用JBang来编写和执行Apache Camel的集成路由,从而实现各种企业集成场景的需求。注意,这只是一种使用JBang和Apache Camel的方法,你还可以根据自己的需求进行灵活调整和扩展。希望对你有所帮助!如果你有更多问题,可以继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值