- 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 项目
我们将使用 Maven 来构建 Camel 项目。不过,我们更建议使用 IntelliJ IDE 进行开发。您可以选择任何您喜欢的 IDE 来完成此项目。
创建新项目
创建一个新的Maven项目并指定以下内容:
GroupId: Basket ArtifactId: Basket
选择项目默认位置,或者如果您愿意,可以指定您选择的目录。
添加依赖项
您需要添加一些依赖项才能使用 Camel。这些依赖项添加到pom.xml文件中。因此,打开 pom.xml 并添加以下两个依赖项:
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
注意 - 我们只需要应用程序的最低限度依赖项。当您从其库中使用更多 Camel 组件时,您需要在此 pom.xml 文件中添加相应的依赖项。
创建 Java DSL
接下来,您将在 Java DSL 中编写过滤和路由代码。创建一个名为DistributeOrderDSL的新 Java 类。向其中添加以下代码:
public class DistributeOrderDSL {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
try {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:DistributeOrderDSL")
.split(xpath("//order[@product='soaps']/items")).to("stream:out");
// .to("file:src/main/resources/order/");
}
});
context.start();
ProducerTemplate orderProducerTemplate = context.createProducerTemplate();
InputStream orderInputStream = new FileInputStream(ClassLoader.getSystemClassLoader()
.getResource("order.xml").getFile());
orderProducerTemplate.sendBody("direct:DistributeOrderDSL", orderInputStream);
} finally {
context.stop();
}
}
}
在main方法中,我们首先通过实例化DefaultCamelContext类中提供的默认实现来创建CamelContext。
CamelContext context = new DefaultCamelContext();
接下来,我们通过创建一个匿名的RouteBuilder实例来添加一条路由:
context.addRoutes(new RouteBuilder() {
我们重写configure方法,以添加从直接 URI DistributeOrderDSL到系统控制台的路由。我们使用 xpath 查询提供一些过滤。
public void configure() throws Exception {
from("direct:DistributeOrderDSL")
.split(xpath("//order[@product = 'soaps']/items")).to("stream:out");
// .to("file:src/main/resources/order/");
}
添加路由后,我们启动上下文:
context.start();
接下来,我们添加创建直接 URI - DistributeOrderDSL的代码。
ProducerTemplate orderProducerTemplate = context.createProducerTemplate();
InputStream orderInputStream = new FileInputStream(ClassLoader.getSystemClassLoader()
.getResource("order.xml").getFile());
最后,我们启动处理:
orderProducerTemplate.sendBody("direct:DistributeOrderDSL", orderInputStream);
现在,您的 Java DSL 代码已完成,在测试应用程序之前唯一剩下的事情是将order.xml文件添加到您的项目中。您可以为此目的使用介绍章节中显示的示例 XML。
测试结果
运行应用程序时,您将看到以下输出:
<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>
请注意,这里只列出了肥皂订单。如果您希望将其存储到本地文件,只需注释掉stream.out行,并在您的configure方法中取消注释以下行:
// .to("file:src/main/resources/order/");
在接下来的部分中,我们将学习如何将 Camel 与 Spring 集成使用。