Apache Camel - 与 Spring 一起使用
我们现在将使用 Spring 重新创建上一章中的应用程序。 这将使我们了解如何在 XML 而不是 DSL 中创建 Camel 路由。
创建新项目
创建一个新的 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。

