1. 概述

这篇文章将演示如何在Spring中配置并使用 Apache Camel

Apache Camel 提供了大量实用的组件,支持多种技术栈,比如 JPAHibernateFTPApache-CXFAWS-S3 等等,方便你将不同的系统进行集成。

举个例子,你可以通过 Hibernate 和 Apache CXF 组件从数据库中读取数据,再通过 REST API 发送给其他系统。

在本文中,我们会构建一个简单的 Camel 示例:读取一个文件,将其内容转换为大写,然后再转为小写。我们会使用 Camel 的 File 组件 和 Spring 4.2 来完成这个任务。

示例流程如下:

  1. 从源目录读取文件
  2. 使用自定义 Processor 将内容转为大写
  3. 将转换后的内容写入目标目录
  4. 使用 Camel Translator 将内容转为小写
  5. 将再次转换后的内容写入另一个目标目录

2. 添加依赖

要在 Spring 中使用 Apache Camel,需要在 pom.xml 中添加如下依赖:

<properties>
    <camel.version>4.3.0</camel.version>
    <spring.version>5.3.25</spring.version>
</properties>

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

依赖说明:

camel-core —— Apache Camel 的核心依赖
camel-spring —— 用于在 Spring 中集成 Camel
camel-stream —— 可选组件,可用于在路由运行时输出日志或控制台信息
spring-context —— Spring 标准依赖,用于在 Spring 上下文中运行 Camel 路由

3. Spring Camel 上下文配置

首先我们创建一个 Spring 配置文件,用于定义 Camel 路由。

注意文件中包含了 Apache Camel 和 Spring 所需的命名空间和 schema 位置:

<?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>

其中 <camelContext> 元素代表 Camel 的上下文,功能类似于 Spring 的 ApplicationContext。现在我们可以在该上下文中定义 Camel 路由了。

3.1. 使用自定义 Processor 的 Camel 路由

接下来我们编写第一个路由,用于将文件内容转为大写。

我们需要定义一个输入源(这里使用文件),一个处理逻辑(自定义 Processor),以及一个输出目标(也使用文件)。

路由定义如下:

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

同时需要定义 myFileProcessor 这个 Bean:

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

3.2. 自定义大写转换 Processor

现在我们来实现前面定义的 FileProcessor 类。该类需要实现 Camel 的 Processor 接口,重写 process 方法,方法接收一个 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);
    }
}

这个 process 方法会在每次从输入源读取到数据时被调用。

3.3. 小写转换 Processor

接下来我们再添加一个输出流程,将同样的输入内容转换为小写。这次我们不使用自定义 Processor,而是使用 Camel 的 Message Translator 特性

更新后的路由如下:

<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. 启动应用

要让 Camel 路由生效,只需将配置文件加载到 Spring 上下文中即可:

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

启动后,Camel 会自动处理文件,最终会在两个目标目录中分别生成一个大写和一个小写的文件。

5. 总结

如果你在做系统集成相关的工作,Apache Camel 绝对值得你了解。它提供了大量开箱即用的组件,让你能更专注于业务逻辑,而不是重复造轮子。

如果想深入学习 企业集成模式(Enterprise Integration Patterns),建议阅读由 Gregor Hohpe 和 Bobby Woolf 编写的 《Enterprise Integration Patterns》 这本书,它对 EIP 的概念进行了非常清晰的阐述。

本文中的完整示例代码可以在 GitHub 上找到:项目地址


原始标题:Using Apache Camel with Spring

« 上一篇: JsonPath 介绍