Apache Camel - 6 - Camel结构

本文深入介绍了Apache Camel的核心架构,包括CamelContext、路由引擎、路由、域特定语言、处理器、组件、端点、生产者和消费者等关键概念。通过详细的解释和示例,帮助读者理解Camel的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Camel’s architecture(Camel的结构)

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

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

At a high level, Camel is composed of processors, components, and routes. All of these are contained within the CamelContext.

在高层次上,Camel是由处理器,组件和路由组成的。 所有这些都包含在CamelContext中。

 

Routing engine A DSL wires endpoints and processors together to form routes.

路由引擎由DSL将终端和处理器连接在一起形成路由。

 

Processors Handle things in between endpoints like(处理器处理终点之间的事情)

The routing engine uses routes as specifications for where messages are routed.

Routes are defined using one of Camel’s domain-specific languages (DSLs). Processors are used to transform and manipulate messages during routing and also to implement all the EIP patterns, which have corresponding keywords in the DSL languages. Components are the extension points in Camel for adding connectivity to other systems. To expose these systems to the rest of Camel, components provide an endpoint interface.

With that high-level view out of the way, let’s take a closer look at the individual concepts in figure 1.6.

路由引擎使用路由作为消息路由的规范。

路由使用Camel的域特定语言(DSL)之一来定义。 处理器用于在路由期间转换和操作消息,并实现所有的EIP模式,这些模式在DSL语言中都有相应的关键字。组件是Camel中用于向其他系统添加连接的扩展点。 为了将这些系统公开给Camel的其他部分,组件提供了一个端点接口。

有了这个高层次的观点,让我们仔细看看图1.6中的各个概念。

 

 

1. Camel concepts(Camel 概念)

The CamelContext provides

access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages.

CamelContext提供

访问许多有用的服务,最值得注意的是组件,类型转换器,注册表,端点,路由,数据格式和语言。

 

Figure 1.6 revealed many new concepts, so let’s take some time to go over them one by one. We’ll start with the CamelContext, which is Camel’s runtime.

图1.6显示了许多新的概念,所以让我们花一些时间逐一地去看看它们。 我们将从Camel的运行时开始。

 

 

CAMELCONTEXT(Camel上下文)

You may have guessed that the CamelContext is a container of sorts, judging from figure 1.6. You can think of it as Camel’s runtime system, which keeps all the pieces together.

您可能已经猜到,从图1.6来看,CamelContext是一个容器。 你可以把它看作Camel的运行时系统,它把所有的东西放在一起。

 

The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages.

CamelContext提供了许多有用的服务,最值得注意的是组件,类型转换器,注册表,端点,路由,数据格式和语言。

 

Figure 1.7 shows the most notable services that the CamelContext keeps together.

As you can see from figure 1.7, there are a lot of services for the CamelContext to keep track of. These are described in table 1.1.

The details of each of these services will be discussed throughout the book. Let’s now take a look at routes and Camel’s routing engine.

图1.7显示了CamelContext保持在一起的最显着的服务。

从图1.7可以看出,CamelContext有很多服务可以跟踪。 这些在表1.1中描述。

这些服务的细节将在本书中讨论。 现在我们来看看路由和Camel的路由引擎。

 

 

ROUTING ENGINE(路由引擎)

Camel’s routing engine is what actually moves messages under the hood. This engine isn’t exposed to the developer, but you should be aware that it’s there and that it does all the heavy lifting, ensuring that messages are routed properly

Camel的路由引擎是实际上将消息转移到底层的东西。这个引擎不会暴露给开发者,但是你应该知道它在那里,并且它完成了所有繁重的工作,确保消息的路由正

 

 

ROUTES(路由)

Routes are obviously a core abstraction for Camel. The simplest way to define a route is as a chain of processors. There are many reasons for using routers in messaging applications. By decoupling clients from servers, and producers from consumers, routes can

■ Decide dynamically what server a client will invoke

■ Provide a flexible way to add extra processing

■ Allow for clients and servers to be developed independently

■ Allow for clients of servers to be stubbed out (using mocks) for testing purposes

■ Foster better design practices by connecting disparate systems that do one thing well

■ Enhance features and functionality of some systems (such as message brokers and ESBs)

Each route in Camel has a unique identifier that’s used for logging, debugging, monitoring, and starting and stopping routes. Routes also have exactly one input source for messages, so they’re effectively tied to an input endpoint. 

To define a route, a DSL is used.

 

路由显然是Camel的核心抽象。定义路由最简单的方法就是作为一个处理器链。 在消息应用程序中使用路由器有很多原因。 通过解耦客户端与服务器,以及消费者的生产者,路由可以

■动态决定客户端将调用什么服务器

■提供灵活的方式来添加额外的处理

■允许客户和服务器独立开发

■允许服务器的客户端被删除(使用mock)进行测试

■通过连接完成一件好事的不同系统来培养更好的设计实践

■增强某些系统(如消息代理和ESB)的功能和特性

Camel中的每条路线都有唯一的标识符,用于记录,调试,监控以及启动和停止路线。 路由也只有一个消息输入源,所以它们被有效地绑定到一个输入端点。

为了定义路由,使用DSL。

 

 

DOMAIN-SPECIFIC LANGUAGE (DSL)(域特定语言(DSL))

To wire processors and endpoints together to form routes, Camel defines a DSL. The term DSL is used a bit loosely here. In Camel, DSL means a fluent Java API that contains methods named for EIP terms.

Consider this example:

from("file:data/inbox").filter().xpath("/order[not(@test)]").to("jms:queue:order")

Here, in a single Java statement, you define a route that consumes files from a file endpoint. Messages are then routed to the filter EIP, which will use an XPath predicate to Registry Contains a registry that allows you to look up beans. By default, this will be a JNDI registry.

为了将处理器和终端连接起来形成路由,Camel定义了一个DSL。 DSL这个术语在这里有点松散。 在Camel中,DSL意味着流畅的Java API,其中包含为EIP术语命名的方法。

考虑这个例子:

from("file:data/inbox").filter().xpath("/order[not(@test)]").to("jms:queue:order")

在这里,在一个Java语句中,您可以定义一个从文件端点使用文件的路由。 邮件然后路由到过滤器EIP,它将使用XPath谓词注册表包含注册表,允许您查找豆。 默认情况下,这将是一个JNDI注册表。

 

 

PROCESSOR(处理器)

The processor is a core Camel concept that represents a node capable of using, creating, or modifying an incoming exchange. During routing, exchanges flow from one processor to another; as such, you can think of a route as a graph having specialized processors as the nodes, and lines that connect the output of one processor to the input of another. Many of the processors are implementations of EIPs, but one could easily implement their own custom processor and insert it into a route.

So how do exchanges get in or out of this processor graph? To find out, we’ll need to look at both components and endpoints.

处理器是一个核心Camel概念,代表了一个能够使用,创建或修改传入交换的节点。 在路由期间,交换从一个处理器流向另一个; 因此,您可以将路由视为具有专用处理器作为节点的图形,以及将一个处理器的输出连接到另一个处理器的输入的线路。 许多处理器都是EIP的实现,但是可以轻松实现自己的定制处理器并将其插入到路由中。

那么交易所如何进出这个处理器图形呢? 为了弄清楚,我们需要看看组件和端点。

 

 

COMPONENT(组件)

Components are the main extension point in Camel. To date, there are over 80 components in the Camel ecosystem that range in function from data transports, to DSLs, data formats, and so on. You can even create your own components for Camel—we’ll discuss this in chapter 11.

From a programming point of view, components are fairly simple: they’re associated with a name that’s used in a URI, and they act as a factory of endpoints. For example, a FileComponent is referred to by file in a URI, and it creates FileEndpoints.

The endpoint is perhaps an even more fundamental concept in Camel.

 

组件是Camel的主要扩展点。到目前为止,Camel生态系统中有80多个组件,其功能从数据传输到DSL,数据格式等等。 你甚至可以为Camel创建自己的组件,我们将在第11章讨论这个组件。

从编程角度来看,组件非常简单:它们与URI中使用的名称相关联,它们作为端点的工厂。 例如,FileComponent通过URI中的文件引用,并创建FileEndpoints。

端点也许是Camel更基本的概念。

 

 

ENDPOINT(端点)

An endpoint is the Camel abstraction that models the end of a channel through which a system can send or receive messages. This is illustrated in figure 1.8.

端点是模拟系统可以通过其发送或接收消息的通道的末端的Camel抽象。 如图1.8所示。

An endpoint acts as a neutral interface allowing systems to integrate.

一个端点作为一个中立的接口,允许系统集成。

 

Endpoint URIs are divided into three parts: a scheme, a context path, and options.

端点URI分为三部分:方案,上下文路径和选项。

 

In Camel, you configure endpoints using URIs, such as file:data/inbox?delay=5000, and you also refer to endpoints this way. At runtime, Camel will look up an endpoint based on the URI notation. Figure 1.9 shows how this works.

在Camel中,您使用URI配置端点,例如file:data/inbox?delay=5000,您也可以用这种方式引用端点。在运行时,Camel将根据URI表示法查找端点。图1.9显示了这是如何工作的。

 

The scheme B denotes which Camel component handles that type of endpoint. In this case, the scheme of file selects the FileComponent.

图1.9中的①表示哪个Camel组件处理那种类型的端点。在这种情况下,文件的方案选择FileComponent。

 

The FileComponent then works as a factory creating the FileEndpoint based on the remaining parts of the URI. The context path data/inbox C tells the FileComponent that the starting folder is data/inbox. The option, delay=5000 D indicates that files should be polled at a 5 second interval.

FileComponent然后作为一个工厂创建基于URI的剩余部分的FileEndpoint。上下文路径data/inbox②通知FileComponent起始文件夹是data/inbox。选项delay=5000 图1.9中③表示应该以5秒的间隔轮询文件。

 

 

PRODUCER(生产者)

A producer is the Camel abstraction that refers to an entity capable of creating and sending a message to an endpoint. Figure 1.10 illustrates where the producer fits in with other Camel concepts. 

When a message needs to be sent to an endpoint, the producer will create an exchange and populate it with data compatible with that particular endpoint. 

For example, a FileProducer will write the message body to a file. A JmsProducer, on the other hand, will map the Camel message to a javax.jms.Message before sending it to a JMS destination. This is an important feature in Camel, because it hides the complexity of interacting with particular transports. All you need to do is route a message to an endpoint, and the producer does the heavy lifting

生产者是Camel抽象,是指能够创建消息并将消息发送到端点的实体。 图1.10说明了生产者适合其他Camel概念的地方。

当需要将消息发送到端点时,生产者将创建一个交换并使用与该特定端点兼容的数据来填充它。

例如,FileProducer会将消息正文写入文件。 另一方面,JmsProducer会将Camel消息映射到javax.jms.Message,然后将其发送到JMS目标。 这是Camel的一个重要特征,因为它隐藏了与特定运输交互的复杂性。 所有你需要做的就是把消息路由到一个端点,生产者做繁重的工作

 

 

CONSUMER(消费者)

A consumer is the service that receives messages produced by a producer, wraps them in an exchange, and sends them to be processed. Consumers are the source of the exchanges being routed in Camel.

Looking back at figure 1.10, we can see where the consumer fits in with other Camel concepts. To create a new exchange, a consumer will use the endpoint that wraps the payload being consumed. A processor is then used to initiate the routing of the exchange in Camel using the routing engine.

In Camel there are two kinds of consumers: event-driven consumers and polling consumers. 

The differences between these consumers are important, because they help solve different problems.

 

消费者是接收生产者产生的消息的服务,将其包装在交换中并发送给他们进行处理。 消费者是Camel交易的来源。

回顾图1.10,我们可以看到消费者与其他Camel概念适合的位置。 为了创建新的交换,消费者将使用包装所消耗的有效载荷的端点。 然后使用处理器来使用路由引擎启动Camel交换机的路由。

在Camel中有两种消费者:事件驱动的消费者和轮询的消费者。

这些消费者之间的差异是重要的,因为他们帮助解决不同的问题。

 

 

EVENT-DRIVEN CONSUMER(事件驱动的消费者)

An event-driven consumer waits idle until a message arrives, at which point it wakes up and consumes the message.

一个事件驱动的消费者等待空闲,直到消息到达,然后唤醒并消费该消息。

The most familiar consumer is probably the event-driven consumer, which is illustrated in figure 1.11.

This kind of consumer is mostly associated with client-server architectures and web services. It’s also referred to as an asynchronous receiver in the EIP world. An event-driven consumer listens on a particular messaging channel, usually a TCP/IP port or a JMS queue, and waits for a client to send messages to it. When a message arrives, the consumer wakes up and takes the message for processing.

 

最熟悉的消费者可能是事件驱动的消费者,如图1.11所示。

这种消费者大多与客户端 - 服务器体系结构和Web服务相关联。 它在EIP世界中也被称为异步接收器。 

事件驱动的使用者在特定的消息通道(通常是TCP / IP端口或JMS队列)上侦听,并等待客户端向其发送消息。 当消息到达时,消费者醒来并把消息处理。

 

 

POLLING CONSUMER(轮询消费者)

A polling consumer actively checks for new messages.
轮询消费者主动检查新消息。

 

The other kind of consumer is the polling consumer illustrated in figure 1.12.

In contrast to the event-driven consumer, the polling consumer actively goes and fetches messages from a particular source, such as an FTP server. The polling consumer is also known as a synchronous receiver in EIP lingo, because it won’t poll for more messages until it has finished processing the current message. A common flavor of the polling consumer is the scheduled polling consumer, which polls at scheduled intervals. File, FTP, and email transports all use scheduled polling consumers.

We’ve now covered all of Camel’s core concepts. With this new knowledge, you can revisit your first Camel ride and see what’s really happening.

 

另一种类型的消费者是图1.12所示的轮询消费者。

与事件驱动的消费者形成对比的是,轮询消费者主动从特定源(例如FTP服务器)获取消息。 轮询消费者也被称为EIP术语中的同步接收者,因为它在处理完当前消息之前不

轮询更多的消息。 轮询消费者的常见口味是排定的轮询消费者,其按预定的时间间隔进行轮询。 文件,FTP和电子邮件传输都使用计划的轮询使用者。

我们现在已经涵盖了Camel的所有核心概念。 有了这个新的知识,你可以重新访问你的第一次Camel,看看到底发生了什么。

 

 

Your first Camel ride, revisited

我们回头看《Apache Camel - 1 - Your first Camel ride》中的Demo例子,你从一个目录中读取文件,并将结果写入另一个目录。

现在咱们知道了Camel的核心概念,咱们重新审视一下这个例子:

 

package com.test.camel.file;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

public class FileCopierWithCamel {

        public static void main(String[] args) throws Exception {

        CamelContext context = new DefaultCamelContext();

        context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
                from("file:inbox?noop=true").to("file:outbox");
        }
        });

        context.start();

        // 通用没有具体业务意义的代码,只是为了保证主线程不退出
        synchronized (FileCopierWithCamel.class) {
                FileCopierWithCamel.class.wait();
        }

        }

}


In this example, you first create a CamelContext, which is the Camel runtime. 
You then add the routing logic using a RouteBuilder and the Java DSL(from("file:data/inbox?noop=true").to("file:data/outbox")). 
By using the DSL, you can cleanly and concisely let Camel instantiate components, endpoints, consumers, producers, and so on. 
All you have to focus on is defining the routes that matter for your integration projects. 
Under the hood, though, Camel is accessing the FileComponent, and using it as a factory to create the endpoint and its producer. 
The same FileComponent is used to create the consumer side as well.


在这个例子中,你首先创建一个CamelContext,它是Camel的运行时。
然后使用RouteBuilder和Java DSL(from("file:data/inbox?noop=true").to("file:data/outbox"))添加路由逻辑。
通过使用DSL,您可以干净而简洁地让Camel实例化组件,端点,消费者,生产者等等。
所有你需要关注的是为你的集成项目定义重要的路线。
然而,Camel正在访问FileComponent,并将其用作工厂来创建端点及其生产者。同样的FileComponent也用于创建消费者端。

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值