- Apache Camel 教程
- Apache Camel - 首页
- Apache Camel - 简介
- Apache Camel - 概述
- Apache Camel - 功能
- Apache Camel - 架构
- Apache Camel - CamelContext
- Apache Camel - 端点
- Apache Camel - 组件
- Apache Camel - 消息队列
- Apache Camel - 项目
- 使用 Spring 集成 Camel
- Apache Camel 有用资源
- Apache Camel - 快速指南
- Apache Camel - 有用资源
- Apache Camel - 讨论
Apache Camel - 组件
Camel 提供了多个预构建的组件。
本章将讨论camel-core模块中的一些重要组件。
Bean
Bean组件将Bean绑定到Camel消息交换。创建端点的URI指定为bean:beanID,其中beanID是注册表中指定的Bean的名称。
JndiContext jndiContext = new JndiContext();
jndiContext.bind("MilkOrder", new MilkOrderProcessor());
CamelContext camelContext = new DefaultCamelContext(jndiContext);
camelContext.addRoutes(new RouteBuilder() {
public void configure() {
from("direct:bigBasket")
.to("bean:MilkOrder?method=placeOrder");
}
});
请注意端点是如何使用bean:协议指定的。您可以选择性地指定要调用的Bean方法;在这种情况下,在评估端点表达式时将调用名为placeOrder的方法。MilkOrder是注册在代码片段前两行的MilkOrderProcessor JavaBean的JNDI名称。为了简洁起见,这里省略了MilkOrderProcessor本身的定义。
Direct
您可能已经注意到我们在之前的示例中使用了Direct。要将订单发送给石油供应商,我们在端点规范中使用了direct:oil。使用Direct组件允许您同步调用端点。以下我们之前示例中的两段代码说明了Direct的使用:
.when(header("order").isEqualTo("oil"))
.to("direct:oil")
以及:
from("direct:DistributeOrderDSL")
.process(myProcessor);
File
File组件提供对您机器上文件系统的访问。使用此组件,您可以将来自其他组件的消息保存到本地磁盘。此外,它还允许其他Camel组件处理本地文件。使用File组件时,您可以使用file:directoryName[?options]或file://directoryName[?options]作为URI格式。您之前已经看到过此组件的使用:
from ("file:/order").to("jms:orderQueue");
请注意,File组件默认情况下采用目录名称。因此,将order目录的内容作为输入内容。要在order目录中指定特定文件,请使用以下语句:
from ("file:/order?fileName = order.xml").to("jms:orderQueue");
Log
Log组件允许您将消息记录到底层的日志记录机制。Camel使用Simple Logging Facade for Java (SLF4J)作为对各种日志记录框架的抽象。您可以使用java.util.logging, logback, log4j进行日志记录。此代码片段说明了Log组件的使用:
from("direct:DistributeOrderDSL")
.to("bean:MilkOrder?method = placeOrder")
.to("log:com.example.com?level = INFO&showBody = true");
SEDA
SEDA组件允许您异步调用同一CamelContext中的另一个端点。如果要跨CamelContext实例调用,则需要使用VM组件。此处说明了SEDA的使用:
from("direct:DistributeOrderDSL")
// send it to the seda queue that is async
.to("seda:nextOrder")
在此路由中,我们将简单地将订单路由到nextOrder异步队列。订阅此队列的客户端将从此队列中获取消息。
Timer
Timer组件用于定期发送消息,因此在测试Camel应用程序时非常有用。此处的代码片段每两秒向控制台发送一条测试消息:
from("timer://testTimer?period = 2000")
.setBody()
.simple("This is a test message ${header.timer}")
.to("stream:out");