Using Apache Camel with Spring – 在Spring中使用Apache Camel

最后修改: 2016年 2月 22日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

This article will demonstrate how to configure and use Apache Camel with Spring.

本文将演示如何将Apache Camel与Spring进行配置和使用。

Apache Camel provides quite a lot of useful components that support libraries such as JPA, Hibernate, FTP, Apache-CXF, AWS-S3 and of course many others – all to help integrating data between two different systems.

Apache Camel提供了相当多的有用组件,支持诸如JPA, Hibernate, FTP, Apache-CXF, AWS-S3,当然还有许多其他系统 – 所有这些都有助于在两个不同系统之间整合数据。

For example, using the Hibernate and Apache CXF components, you could pull data from a database and send it to another system over REST API calls.

例如,使用Hibernate和Apache CXF组件,你可以从数据库中提取数据并通过REST API调用将其发送到另一个系统。

In this tutorial, we’ll go over a simple Camel example – reading a file and converting its contents to uppercase and then back to lowercase. We’re going to use Camel’s File component and Spring 4.2.

在本教程中,我们将讨论一个简单的Camel例子–读取一个文件并将其内容转换为大写字母,然后再转换为小写字母。我们将使用Camel的文件组件和Spring 4.2。

Here are the full details of the example:

以下是该例子的全部细节。

  1. Read file from source directory
  2. Convert file content to uppercase using a custom Processor
  3. Write converted output to a destination directory
  4. Convert file content to lowercase using Camel Translator
  5. Write converted output to a destination directory

2. Add Dependencies

2.添加依赖关系

To use Apache Camel with Spring, you will need the following dependencies in your POM file:

要在Spring中使用Apache Camel,你需要在你的POM文件中加入以下依赖项。

<properties>
    <env.camel.version>2.16.1</env.camel.version>
    <env.spring.version>4.2.4.RELEASE</env.spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>${env.camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring</artifactId>
        <version>${env.camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-stream</artifactId>
        <version>${env.camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${env.spring.version}</version>
    </dependency>
</dependencies>

So, we have:

因此,我们有。

  • camel-core – the main dependency for Apache Camel
  • camel-spring – enables us use Camel with Spring
  • camel-stream – an optional dependency, which you can use (for example) to display some messages on the console while routes are running
  • spring-context – the standard Spring dependency, required in our case as we are going to run Camel routes in a Spring context

3. Spring Camel Context

3.Spring的骆驼的背景

First, we’ll create the Spring Config file where we will later define our Camel routes.

首先,我们将创建Spring配置文件,以后我们将在这里定义我们的Camel路由。

Notice how the file contains all required Apache Camel and Spring namespaces and schema locations:

注意该文件包含了所有需要的Apache Camel和Spring命名空间和模式位置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:camel="http://camel.apache.org/schema/spring"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-4.2.xsd	
          http://camel.apache.org/schema/spring 
          http://camel.apache.org/schema/spring/camel-spring.xsd
          http://www.springframework.org/schema/util 
          http://www.springframework.org/schema/util/spring-util-4.2.xsd">

	<camelContext xmlns="http://camel.apache.org/schema/spring">
            <!-- Add routes here -->
	</camelContext>

</beans>

The <camelContext> element represents (unsurprisingly) the Camel context, which can be compared to a Spring application context. Now your context file is ready to start defining Camel routes.

<camelContext>元素代表(毫不奇怪)Camel上下文,它可以被比作Spring应用程序的上下文。现在你的上下文文件已经准备好开始定义Camel路由了。

3.1. Camel Route With Custom Processor

3.1.使用自定义处理器的骆驼路线

Next we’ll write our first route to convert file content to uppercase.

接下来,我们将编写我们的第一个路由,将文件内容转换为大写字母。

We need to define a source from which the route will read data. This can be a database, file, console, or any number of other sources. In our case, it will be file.

我们需要定义一个源,路由将从它那里读取数据。这可以是一个数据库、文件、控制台或任何其他来源。在我们的例子中,它将是文件。

Then we need to define the processor of the data that will be read from the source. For this example, we are going to write a custom processor class. This class will be a Spring bean which will implement the standard Camel Processor Interface.

然后我们需要定义将从源头读取的数据的处理器。在这个例子中,我们将编写一个自定义的处理器类。这个类将是一个Spring Bean,它将实现标准的Camel处理器接口

Once the data is processed, we need to tell the route to where to direct the processed data. Once again, this could be one of a wide variety of outputs, such as a database, file, or the console. In our case, we are going to store it in a file.

一旦数据被处理,我们需要告诉路由将处理后的数据指向哪里。再一次,这可能是多种多样的输出之一,如数据库、文件或控制台。在我们的例子中,我们将把它存储在一个文件中。

To set up these steps, including the input, processor, and output, add the following route to the Camel context file:

要设置这些步骤,包括输入、处理器和输出,请在Camel上下文文件中添加以下路由。

<route>
    <from uri="file://data/input" /> <!-- INPUT -->
    <process ref="myFileProcessor" /> <!-- PROCESS -->
    <to uri="file://data/outputUpperCase" /> <!-- OUTPUT -->
</route>

Additionally, we must define the myFileProcessor bean:

此外,我们必须定义myFileProcessorBean。

<bean id="myFileProcessor" class="org.apache.camel.processor.FileProcessor" />

3.2. Custom Uppercase Processor

3.2.自定义大写字母处理器

Now we need to create the custom file processor we defined in our bean. It must implement the Camel Processor interface, defining a single process method, which takes an Exchange object as its input. This object provides the details of the data from the input source.

现在我们需要创建我们在Bean中定义的自定义文件处理器。它必须实现Camel Processor接口,定义一个单一的process方法,该方法接收一个Exchange对象作为其输入。这个对象提供了来自输入源的数据的详细信息。

Our method must read the message from the Exchange, uppercase the content, and then set that new content back into the Exchange object:

我们的方法必须从Exchange中读取消息,将内容大写,然后将新内容设置回Exchange对象中。

public class FileProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {
        String originalFileContent = (String) exchange.getIn().getBody(String.class);
        String upperCaseFileContent = originalFileContent.toUpperCase();
        exchange.getIn().setBody(upperCaseFileContent);
    }
}

This process method will be executed for every input received from the source.

这个过程方法将为从源头收到的每一个输入执行。

3.3. Lowercase Processor

3.3.小写字母处理器

Now we will add another output to our Camel route. This time, we will convert the same input file’s data into lowercase. This time, we will not use a custom processor, however; we will use Apache Camel’s Message Translator feature. This is the updated Camel route:

现在我们将为我们的Camel路由添加另一个输出。这一次,我们将把同一个输入文件的数据转换成小写字母。不过,这次我们将不使用自定义处理器;我们将使用Apache Camel的Message Translator功能。这就是更新后的Camel路线。

<route>
    <from uri="file://data/input" />
    <process ref="myFileProcessor" />
    <to uri="file://data/outputUppperCase" />
    <transform>
        <simple>${body.toLowerCase()}</simple>
    </transform>
    <to uri="file://data/outputLowerCase" />
</route>

4. Running the Application

4.运行应用程序

In order to have our routes be processed, we simply need to load the Camel context file into a Spring application context:

为了让我们的路由得到处理,我们只需要将Camel上下文文件加载到Spring应用上下文中。

ClassPathXmlApplicationContext applicationContext = 
  new ClassPathXmlApplicationContext("camel-context.xml");

Once the route has been run successfully, two files will have been created: one with uppercase content, and one with lowercase content.

一旦路线运行成功,将创建两个文件:一个是大写内容,一个是小写内容。

5. Conclusion

5.结论

If you’re doing integration work, Apache Camel can definitely make things easier. The library provides plug-and-play components that will help you reduce boilerplate code and focus on the main logic of processing data.

如果你在做集成工作,Apache Camel肯定能让事情变得更容易。该库提供了即插即用的组件,可以帮助你减少模板代码,把注意力放在处理数据的主要逻辑上。

And if you want to explore the Enterprise Integration Patterns concepts in detail, you should have a look at this book written by Gregor Hohpe and and Bobby Woolf, who conceptualize the EIPs very cleanly.

如果你想详细探讨企业集成模式的概念,你应该看看这本书,作者是Gregor Hohpe和和Bobby Woolf,他们把EIP的概念说得非常清楚。

The example described in this article is available in a project on GitHub.

本文所述的例子可在GitHub上的项目中找到。