`

Camel Routes

阅读更多

Routes

Camel supports the definition of routing rules using a Java DSL (domain specific language) which avoids the need for cumbersome XML using a RouteBuilder.

For example a simple route can be created as follows.

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a").to("direct:b");
    }
};

As you can see from the above Camel uses URIs to wire endpoints together.

URI String formatting

Available as of Camel 2.0

If you have endpoint URIs that accept options and you want to be able to substitute the value, e.g. build the URI by concat the strings together, then you can use the java.lang.String.format method. But in Camel 2.0 we have added two convenient methods in the Java DSL so you can do fromF and toF that uses String formatting to build the URI.

from("direct:start").toF("file://%s?fileName=%s", path, name);

fromF("file://%s?include=%s", path, pattern).toF("mock:%s", result);

Filters

You can combine simple routes with filters which can be arbitrary Predicate implementations.

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .filter(header("foo").isEqualTo("bar"))
                .to("direct:b");
    }
};

Choices

With a choice you provide a list of predicates and outcomes along with an optional default otherwise clause which is invoked if none of the conditions are met.

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .choice()
                .when(header("foo").isEqualTo("bar"))
                    .to("direct:b")
                .when(header("foo").isEqualTo("cheese"))
                    .to("direct:c")
                .otherwise()
                    .to("direct:d");
    }
};

Using a custom processor

Here is an example of using a custom Processor

myProcessor = new Processor() {
    public void process(Exchange exchange) {
        log.debug("Called with exchange: " + exchange);
    }
};

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .process(myProcessor);
    }
};

You can mix and match custom processors with filters and choices.

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .filter(header("foo").isEqualTo("bar"))
                .process(myProcessor);
    }
};

Interceptors

Here is an example of adding a few custom InterceptorProcessor objects to a processing pipeline:

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        errorHandler(deadLetterChannel("mock:error"));

        from("direct:a")
            .filter(header("foo").isEqualTo(123))
                .to("direct:b");
    }
};

When you start defining and interceptor stack with intercept(), you must follow up with the subsequent .target() so that the target of the interceptor stack is properly registered.

分享到:
评论

相关推荐

    Mastering.Apache.Camel

    This book will provide you with the skills you need to efficiently create routes using Apache Camel. After briefly introducing the key features and core concepts of Camel, the book will take you ...

    cxfrs-routing:在JBoss Fuse平台上创建Apache Camel Routes的示例项目

    JBoss保险丝中的骆驼布线 这是一个示例项目,用于在通过使用CXFRS公开的JBoss Fuse平台上创建Apache骆驼路由。 入门 本指南假定您已安装以下设备: 创建OSGI包 可以通过在项目的根目录下运行以下maven命令来创建...

    Camel in action(camel实战)

    Apache Camel is a Java framework that lets you implement the standard enterprise integration patterns in a few lines of code. With a concise but sophisticated DSL you snap integration logic into your ...

    [Camel实战].(Camel.in.Action).Claus.Ibsen&Jonathan;.Anstey.文字版

    中文名: Camel 实战 原名: Camel in Action 作者: Claus Ibsen Jonathan Anstey 资源格式: PDF 版本: 英文文字版/更新源代码 出版社: Manning书号: 9781935182368发行时间: 2010年12月 地区: 美国 语言: 英文 简介: ...

    Camel_Camel3Camel6函数_

    Camel3 Camel6函数等matlab源代码

    ApacheCamel-JDBC

    ApacheCamel-JDBC Apache Camel JDBC组件 代码样例Demo

    Camel服务集成,服务编排操作文档

    Camel服务集成,服务编排操作文档

    Apache Camel中文开发使用指南.zip

    Apache Camel 开发使用指南中文版

    Camel in Action ch1

    1: Meet Camel - FREE 2: Routing with Camel - AVAILABLE Part 2 Core Camel 3: Transforming Data with Camel - AVAILABLE 4: Using Beans with Camel - AVAILABLE 5: Error Handling - AVAILABLE 6:...

    Camel实战中文版第四章.pdf

    Camel In Action一书第四章的中文版。

    camel, Apache camel 镜像.zip

    camel, Apache camel 镜像 Apache camel 是基于已知企业集成模式的强大开放源代码集成框架,它具有强大的Bean集成。简介flex允许你创建企业集成模式,以基于基于Java的域特定语言( 或者 Fluent API ) 或者基于 Sc

    ApacheCamel-Timer

    09-ApacheCamel-Timer Apache Camel Timer组件 定时器 代码Demo

    Camel in action PDF和源码

    Camel in action 一书的PDF电子书 和源代码 The book is divided into three parts: ■ Part 1—First steps ■ Part 2—Core Camel ■ Part 3—Out in the wild

    ApacheCamel-FTP

    ApacheCamel-FTP ApacheCamel-FTP Apache Camel FTP组件 Demo 样例

    Camel in Action.zip

    Apache Camel 作为集成项目的利器,针对应用集成场景的抽象出了一套消息交互模型,通过组件的方式进行第三方系统的接入,目前Apache Camel已经提供了300多种组件能够接入HTTP,JMS,TCP,WS-*,WebSocket 等多种传输...

    Apache Camel 源码分析.rar

    camel direct http jdbc mybatis 等等组件 骆驼 camel部分源码分析

    camel-manual-2.0

    camel-manual-2.0 camel ESB EIP EAI

    CamelJAXBExample:骆驼和 JAXB 的简单例子

    CamelJAXB示例 骆驼和 JAXB 的简单例子 此应用程序是在本地主机上运行 Apache Camel 并使用 Camel Routes 和 JAXB 数据格式的示例。 享受

    apache-camel-demo

    apache-camel 开发sample Apache Camel是一个基于规则路由和中介引擎,提供企业集成模式的Java对象(POJO)的实现,通过应用程序接口(或称为陈述式的Java领域特定语言(DSL))来配置路由和中介的规则。领域特定语言...

    camel-cxf调用和发布webservice例子

    用camel-cxf调用webservice和发布一个webservice接口例子,首先启动QueryServiceMain主函数启动webservice接口,然后启动测试类TestWebservice。例子主要是实现java代码实现camel调用webservice接口

Global site tag (gtag.js) - Google Analytics