第 8 章 消息路由

摘要

消息路由模式描述了将消息通道链接到一起的各种方法。这包括可应用于消息流的各种算法(无需修改消息的正文)。

8.1. 基于内容的路由器

概述

一个 基于内容的路由器 (在 图 8.1 “基于内容的路由器模式” 中)可让您根据消息内容将信息路由到适当的目的地。

图 8.1. 基于内容的路由器模式

基于内容的路由器模式

Java DSL 示例

以下示例演示了如何根据各种 predicate 表达式评估从输入、seda:a、端点路由到 seda:bqueue:cseda:d 的请求:

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("seda:a").choice()
          .when(header("foo").isEqualTo("bar")).to("seda:b")
          .when(header("foo").isEqualTo("cheese")).to("seda:c")
          .otherwise().to("seda:d");
    }
};

XML 配置示例

以下示例演示了如何在 XML 中配置相同的路由:

<camelContext id="buildSimpleRouteWithChoice" xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="seda:a"/>
    <choice>
      <when>
        <xpath>$foo = 'bar'</xpath>
        <to uri="seda:b"/>
      </when>
      <when>
        <xpath>$foo = 'cheese'</xpath>
        <to uri="seda:c"/>
      </when>
      <otherwise>
        <to uri="seda:d"/>
      </otherwise>
    </choice>
  </route>
</camelContext>