路由
路由
在 Apache Camel 中,路由(route)是一组处理步骤,用于在消息从源传递到目的地的过程中对其进行处理。一条路由通常由一系列按线性顺序连接的处理步骤组成。
Camel 的路由是定义集成流程的地方。例如,你可以编写一条 Camel 路由来指定如何集成两个系统,也可以指定如何在系统之间对数据进行操作、路由或调解。
这些路由通常使用简单、声明式的语法定义,易于阅读和理解。Camel 支持使用你所选择的不同 DSL 来编写路由。
例如,你可以编写一条路由,从 FTP 服务器消费文件并将其发送到 ActiveMQ 消息系统。实现这一功能的路由如下所示:
- Java
- XML
- YAML
from("ftp:myserver/folder")
.to("activemq:queue:cheese");<route>
<from uri="ftp:myserver/folder"/>
<to uri="activemq:queue:cheese"/>
</route>- route:
from:
uri: ftp:myserver/folder
steps:
- to:
uri: activemq:queue:cheese为路由命名
你可以在 Java DSL 中使用 routeId 为路由指定名称:
- Java
- XML
- YAML
在 Java DSL 中,使用 routeId 来声明路由的名称/ID。
from("ftp:myserver/folder").routeId("myRoute")
.to("activemq:queue:cheese");在 XML DSL 中,你在 <route> 元素中使用 id 来声明路由的名称/ID。
<route id="myRoute">
<from uri="ftp:myserver/folder"/>
<to uri="activemq:queue:cheese"/>
</route>在 YAML DSL 中,你在 - route 节点中使用 id 来声明路由的名称/标识。
- route:
id: myRoute
from:
uri: ftp:myserver/folder
steps:
- to:
uri: activemq:queue:cheese路由描述与备注
你可以为路由添加描述,例如提供一段简短的人类可读的摘要,说明该路由的作用。
- Java
- XML
- YAML
from("ftp:myserver/folder").routeDescription("Routes files from the FTP server to internal messaging system")
.to("activemq:queue:cheese");<route description="Routes files from the FTP server to internal messaging system">
<from uri="ftp:myserver/folder"/>
<to uri="activemq:queue:cheese"/>
</route>- route:
description: "Routes files from the FTP server to internal messaging system"
from:
uri: ftp:myserver/folder
steps:
- to:
uri: activemq:cheese一条注释(需要 Apache Camel 4.6.0 及更高版本),则类似于开发者的代码注释,或者你想保留的其他某种信息。注释和描述在运行时都没有任何影响,仅作为只读信息存在。
- Java
- XML
- YAML
from("ftp:myserver/folder").routeDescription("Routes files from the FTP server to internal messaging system")
.routeNote("The FTP server can be offline from time to time, and if so then just restart Camel")
.to("activemq:queue:cheese");<route description="Routes files from the FTP server to internal messaging system"
note="The FTP server can be offline from time to time, and if so then just restart Camel">
<from uri="ftp:myserver/folder"/>
<to uri="activemq:queue:cheese"/>
</route>- route:
description: "Routes files from the FTP server to internal messaging system"
note: "The FTP server can be offline from time to time, and if so then just restart Camel"
from:
uri: ftp:myserver/folder
steps:
- to:
uri: activemq:cheese| 你也可以为 EIP 模式添加描述和注释 |
|---|
使用 Java DSL 用 Java 编写路由
你可以通过继承 RouteBuilder 类并实现 configure 方法,使用 Java 语言创建路由。
下面是一个示例:
纯 Java:匿名 RouteBuilder 类
RouteBuilder builder = new RouteBuilder() {
public void configure() {
from("direct:a").to("direct:b");
}
};上面的代码是一个匿名类,而在使用 Java DSL 时,更常见的做法是创建自己的类。例如,我们可以创建一个名为 MyRoute 的类,然后像下面这样实现路由:
纯 Java:命名的 RouteBuilder 类
import org.apache.camel.builder.RouteBuilder;
public class MyRoute extends RouteBuilder {
@Override
public void configure() {
from("direct:a").to("direct:b");
}
}从上面的代码片段可以看出,Camel 使用 URI 来连接各个端点。
我们把这种编写路由的方式称为使用 Java DSL。
路由前置条件
路由可以根据测试结果决定是否被包含。你可以使用 simple 语言来表达测试条件。Camel 仅在初始化阶段对该条件求值一次。
当路由因前置条件而未被包含时,Camel 会跳过该路由,并且无法在运行时再将其包含进来。如果你想包含该路由但不立即启动它,可以在路由上设置 autoStartup=false,这样就能在运行时稍后启动该路由。 |
|---|
下面的示例仅在参数 format 被设置为 xml 时才包含该路由:
- Java
- XML
- YAML
from("direct:in").precondition("'{{format}}' == 'xml'")
.unmarshal().jaxb()
.to("direct:out");<route precondition="'{{format}}' == 'xml'">
<from uri="direct:in"/>
<unmarshal><jaxb contextPath="com.foo.model"/></unmarshal>
<to uri="direct:out"/>
</route>- route:
precondition: "'{{format}}' == 'xml'"
from:
uri: "direct:in"
steps:
- unmarshal:
jaxb:
contextPath: com.foo.model
- to:
uri: direct:out更多信息
查看受支持的语言列表,了解可用于编写 Camel 路由的语言。
评论
登录后参与评论
KnowForge