- Apache Camel 教程
- Apache Camel - 首页
- Apache Camel - 简介
- Apache Camel - 概述
- Apache Camel - 特性
- Apache Camel - 架构
- Apache Camel - CamelContext
- Apache Camel - 端点
- Apache Camel - 组件
- Apache Camel - 消息队列
- Apache Camel - 项目
- 使用 Camel 与 Spring
- Apache Camel 有用资源
- Apache Camel - 快速指南
- Apache Camel - 有用资源
- Apache Camel - 讨论
Apache Camel - 与 Spring 结合使用
现在我们将使用 Spring 重新创建上一章中的应用程序。这将使我们了解如何在 XML 中创建 Camel 路由,而不是使用 DSL。
创建新项目
创建一个新的**Maven**项目并指定以下内容 -
GroupId: BasketWithSpring ArtifactId: BasketWithSpring
选择项目默认位置,或者如果需要,指定您选择的目录。
添加依赖项
除了您在早期应用程序中使用的核心依赖项之外,您还需要添加一些其他依赖项才能使用 Spring。这些依赖项添加到 pom.xml 中。现在,打开 pom.xml 并添加以下依赖项 -
<dependencies>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.2</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.15.1</version>
</dependency>
</dependencies>
为 Spring 创建 Java DSL
现在让我们创建一个名为**DistributeOrderXML**的新 Java 类。将以下代码添加到其中 -
public class DistributeOrderXML {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"SpringRouteContext.xml");
CamelContext camelContext = SpringCamelContext.springCamelContext(appContext, false);
try {
camelContext.start();
ProducerTemplate orderProducerTemplate = camelContext.createProducerTemplate();
InputStream orderInputStream = new FileInputStream(ClassLoader.getSystemClassLoader()
.getResource("order.xml").getFile());
orderProducerTemplate.sendBody("direct:DistributeOrderXML", orderInputStream);
} finally {
camelContext.stop();
}
}
}
在**main**方法中,首先我们创建**ApplicationContext**的实例,它是 Spring 应用程序中的核心接口。在其构造函数中,我们指定包含路由和过滤信息的 XML 文件的名称。
ApplicationContext appContext = new ClassPathXmlApplicationContext( "SpringRouteContext.xml");
接下来,我们创建**CamelContext**,并在其参数中指定上面创建的**ApplicationContext**。
CamelContext camelContext = SpringCamelContext.springCamelContext(appContext, false);
此时,我们的路由和过滤已设置完成。因此,我们使用其**start**方法启动**CamelContext**。与之前的情况一样,我们定义用于加载 order.xml 文件的端点并启动处理。现在,让我们了解如何在 XML 中定义路由。
创建应用程序上下文
向项目中添加一个新的 XML 文件,并将其命名为**SpringRouteContext.xml**。将以下内容复制粘贴到此文件中。
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd ">
<camelContext xmlns = "http://camel.apache.org/schema/spring">
<route>
<from uri = "direct:DistributeOrderXML"/>
<log message = "Split by Distribute Order"/>
<split>
<xpath>//order[@product = 'Oil']/items</xpath>
<to uri = "file:src/main/resources/order/"/>
<to uri = "stream:out"/>
</split>
</route>
</camelContext>
</beans>
在这里,我们定义 xpath 查询如下,请注意,我们现在选择所有“oil”的订单。
<xpath>//order[@product = 'Oil']/items</xpath>
输出端点是多个。第一个端点指定**order**文件夹,第二个端点指定控制台。
<to uri = "file:src/main/resources/order/"/> <to uri = "stream:out"/>
运行应用程序。
测试结果
运行应用程序时,您将在屏幕上看到以下输出。
<items>
<item>
<Brand>Cinthol</Brand>
<Type>Original</Type>
<Quantity>4</Quantity>
<Price>25</Price>
</item>
<item>
<Brand>Cinthol</Brand>
<Type>Lime</Type>
<Quantity>6</Quantity>
<Price>30</Price>
</item>
</items>
查看您指定路径中的**order**文件夹。您会发现一个新创建的文件,其中包含上述 XML 代码。
结论
Camel 提供了一个现成的框架,该框架实现了 EIP 以简化您的集成项目。它支持使用特定领域语言进行编码,也支持使用 XML。