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显示了许多新的概念,所以让我们花点时间逐一讨论它们。我们将从CamelContext开始,它是Camel的运行时。
CAMELCONTEXT
You may have guessed that the Camel Context 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. 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 Camel Context 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.
您可能已经猜到了Camel上下文是某种容器,从图1.6可以看出。您可以将其视为Camel的运行时系统,它将所有部分保持在一起。图1.7显示了CamelContext保持在一起的最核心的服务。如图1.7所示,Camel上下文有很多服务需要跟踪。如表1.1所述。每一项服务的细节将在本书中讨论。现在让我们看看路由和Camel的路由引擎。
Components:
Contains the components used. Camel is capable of loading components on the fly either by autodiscovery on the classpath or when a new bundle is activated in an OSGi container. In chapter 7 we’ll discuss components in more detail.
组件
包含使用的组件。Camel能够通过类路径上的自动发现或在OSGi容器中激活新的bundle来动态加载组件。在第7章中,我们将更详细地讨论组件。
Endpoints
Contains the endpoints that have been created.
Routes
Contains the routes that have been added. We’ll cover routes in chapter 2.
Type converters
Contains the loaded type converters. Camel has a mechanism that allows you to manually or automatically convert from one type to another. Type converters are covered in chapter 3.
Data formats
Contains the loaded data formats. Data formats are covered in chapter 3.
Registry
Contains a registry that allows you to look up beans. By default, this will be a JNDI registry. If you’re using Camel from Spring, this will be the Spring ApplicationContext. It can also be an OSGi registry if you use Camel in an OSGi container. We’ll cover registries in chapter 4.
注册表
包含允许您查找bean的注册表。默认情况下,这将是一个JNDI注册表。如果您使用的是Camel from Spring,这将是Spring应用程序上下文。如果在OSGi容器中使用Camel,它也可以是OSGi注册表。我们将在第四章讨论注册。
Languages
Contains the loaded languages. Camel allows you to use many different languages to create expressions. You’ll get a glimpse of the XPath language in action when we cover the DSL. A complete reference to Camel’s own Simple expression language is available in appendix A.
语言文字
包含加载的语言。Camel允许您使用多种不同的语言来创建表达式。当我们讨论DSL时,您将看到XPath语言的实际应用。有关Camel自己的简单表达式语言的完整参考,请参见附录A。
### 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的核心抽象。定义路由的最简单方法是将其定义为处理器链。在消息传递应用程序中使用路由器有很多原因。通过将客户端与服务器、生产者与消费者分离,路由可以
■动态决定客户端将调用什么服务器
■提供灵活的方式添加额外处理
■允许独立开发客户端和服务器
■允许服务器的客户机被剔除(使用模拟)以进行测试
■通过连接能很好地完成一件事的不同系统,培养更好的设计实践
■增强某些系统(如消息代理)的特性和功能
Camel中的每个路由都有一个唯一的标识符,用于记录、调试、监视以及启动和停止路由。路由也只有一个消息输入源,因此它们有效地绑定到一个输入端点。为了定义路由,使用DSL。
### DOMAIN-SPECIFIC LANGUAGE (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")
领域特定语言(DSL)
为了将处理器和端点连接在一起形成路由,Camel定义了一个DSL。术语DSL在这里使用得有点松散。在Camel中,DSL意味着一个流畅的javaapi,包含以EIP术语命名的方法。考虑这个例子:
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 totest whether the message is a test order or not. If a message passes the test, it’s forwarded to the JMS endpoint. Messages failing the filter test will be dropped. Camel provides multiple DSL languages, so you could define the same route using the Spring DSL, like this:
<route>
<from uri="file:data/inbox"/>
<filter>
<xpath>/order[not(@test)]</xpath>
<to uri="jms:queue:order"/>
</filter>
</route>
在这里,在一个Java语句中,您定义了一个使用来自文件端点的文件的路由。然后消息被路由到filter EIP,它将使用XPath谓词来测试消息是否是测试顺序。如果消息通过了测试,它将被转发到JMS端点。未通过筛选测试的消息将被丢弃。Camel提供多种DSL语言,因此您可以使用Spring DSL定义相同的路由,如下所示:
<route>
<from uri="file:data/inbox"/>
<filter>
<xpath>/order[not(@test)]</xpath>
<to uri="jms:queue:order"/>
</filter>
</route>
The DSLs provide a nice abstraction for Camel users to build applications with. Under the hood, though, a route is actually composed of a graph of processors. Let’s take a moment to see what a processor really is.
dsl为Camel用户构建应用程序提供了一个很好的抽象。不过,在引擎盖下,路由实际上是由处理器组成的。让我们花点时间看看处理器到底是什么。
### 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.
处理器
processor是一个核心概念,它表示能够使用、创建或修改传入交换的节点。在路由过程中,exchanges从一个处理器流到另一个处理器;因此,您可以将路由视为一个以专用处理器为节点的图,以及将一个处理器的输出连接到另一个处理器的输入的线。许多处理器都是eip的实现,但是可以很容易地实现自己的定制处理器并将其插入路由。那么,exchange是如何进出这个处理器的呢?要找到答案,我们需要同时查看组件和端点。
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.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.
ENDPOINT
端点是一种Camel的抽象,它对系统可以发送或接收消息的通道末端进行建模。这如图1.8所示。在Camel中,您可以使用uri配置端点,例如file:data/inbox?delay=5000,也可以这样引用端点。在运行时,Camel将根据URI表示法查找端点。图1.9显示了它的工作原理。
The scheme @1 denotes which Camel component handles that type of endpoint. In this case, the scheme of file selects the FileComponent.The FileComponent then works as a factory creating the FileEndpoint based on the remaining parts of the URI. The context path data/ inbox @2 tells the FileComponent that the starting folder is data/inbox. The option, delay=5000 @3 indicates that files should be polled at a 5 second interval. There’s more to an endpoint than meets the eye. Figure 1.10 shows how an endpoint works together with an exchange, producers, and consumers. At first glance, figure 1.10 may seem a bit overwhelming, but it will all make sense in a few minutes. In a nutshell, an endpoint acts as a factory for creating consumers and producers that are capable of receiving and sending messages to a particular endpoint. We didn’t mention producers or consumers in the high-level view of Camel in figure 1.6, but they’re important concepts. We’ll go over them next.
scheme@1表示哪个Camel组件处理该类型的端点。在这种情况下,文件方案选择文件组件FileComponent然后作为工厂,基于URI的其余部分创建FileEndpoint。上下文路径data/inbox@2告诉FileComponent起始文件夹是data/inbox。选项delay=5000@3表示应该以5秒的间隔轮询文件。一个终点的意义远不止眼前所见。图1.10显示了端点如何与exchange、生产者和消费者协同工作。乍一看,图1.10似乎有点吊,但几分钟后就完全有意义了。简言之,端点充当工厂,用于创建能够接收消息并将消息发送到特定端点的使用者和生产者。在图1.6中,我们没有在Camel的高级视图中提到生产者或消费者,但它们是重要的概念。我们下一步再看一遍。
### 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的一个重要特性,因为它隐藏了与特定传输相互作用的复杂性。你所需要做的就是将一个message路由到一个端点,然后生产者完成繁重的工作。
### 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
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
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的经历,看看到底发生了什么。