apache-camel 快速入门

原文链接: apache-camel 快速入门

快速开始

camel套路

  1. 构建CamelContext
  2. 使用DSL构建RouteBuilder对象, 并将该对象添加到CamelContext
  3. CamelContext会使用RouteBuilder构建Route对象
  4. 调用camel.start()方法启动CamelContext
  5. 一旦CamelContext监测到符合Routefrom规则的消息, 就好执行路由.

camel使用姿势

  1. 查用户手册, 看有哪些开箱即用的component, 对于消息处理, 自定义processor.

https://camel.apache.org/components/3.14.x/index.html

  1. 查阅EIP模式文档, 找到需要的模式, 复制相关代码

https://camel.apache.org/components/3.16.x/eips/enterprise-integration-patterns.html

示例代码

public class MainApp {

    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.configure().addRoutesBuilder(new MyRouteBuilder());
        main.run(args);
    }
}
public class MyRouteBuilder extends RouteBuilder {

    /**
     * Let's configure the Camel routing rules using Java code...
     */
    public void configure() {

        // here is a sample which processes the input files
        // (leaving them in place - see the 'noop' flag)
        // then performs content based routing on the message using XPath
        from("file:src/data?noop=true")
            .choice()
                .when(xpath("/person/city = 'London'"))
                    .log("UK message")
                    .to("file:target/messages/uk")
                .otherwise()
                    .log("Other message")
                    .to("file:target/messages/others");
    }
}
public class CamelBasic {
    public static void main(String[] args) throws Exception {
        try (DefaultCamelContext camel = new DefaultCamelContext()){
            camel.addRoutes(createBasicRoute());

            camel.start();

            Thread.sleep(10_000);

        }
    }

    private static RoutesBuilder createBasicRoute() {

        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("timer:foo")
                        .log("Hello Camel");
            }
        };
    }
}

Camel核心概念

Endpoint

信息的输入/输出点:

  • JMS
  • REST
  • 文件
  • FTP
  • 邮件地址
  • POJO对象

URI

Camel中的每个Endpoint使用URI来描述, URI / URL的区别:

CamelContext

CamelContext 对象代表 Camel 运行时系统。您通常在应用程序中有一个 CamelContext 对象。

CamelTemplate

类似JDBCTemplate, RESTTemplate, 对CamelContext进行简单包装, 提供往Endpoint中发送消息货Exchange的方法.

Component

Camel中, 组件用来创建Endpoint. 一般当CamelContext调用getEndpoint()方法时, 会根据Endpoint的URI前缀(schema)来寻找对于的Component, 然后调用相应的方法.

Message & Exchange

Message是消息的抽象, 包括请求,响应,异常信息.
Exchange是消息路由的抽象.

Processor

处理器总是在消息路由之间生效, 它处理输入消息, 输入响应消息或异常消息.
通常Camel中自定义大量的处理器用以实现业务逻辑, Camel官方提供了很多处理器用以实现EIP中的很多模式.

package org.apache.camel;
public interface Processor {
    void process(Exchange exchange) throws Exception;
}

Route & RouteBuilder & Java DSL

路由是消息从输入队列到目标队列的一步步的移动.
Camel 提供了3中配置路由的方法, 使用xml, yml以及Java DSL

Java DSL

RouteBuilder builder = new RouteBuilder() {
    public void configure() {
        from("queue:a").filter(header("foo").isEqualTo("bar")).to("queue:b");

        from("queue:c").choice()
                .when(header("foo").isEqualTo("bar")).to("queue:d")
                .when(header("foo").isEqualTo("cheese")).to("queue:e")
                .otherwise().to("queue:f");
    }
};
CamelContext myCamelContext = new DefaultCamelContext();
myCamelContext.addRoutes(builder);

The first line in the above example creates an object which is an instance of an anonymous subclass of RouteBuilder with the specified configure() method.
The CamelContext.addRoutes(RouterBuilder builder) method invokes builder.setContext(this) – so the RouteBuilder object knows which CamelContext object it is associated with – and then invokes builder.configure(). The body of configure() invokes methods such as from(), filter(), choice(), when(), isEqualTo(), otherwise() and to().
The RouteBuilder.from(String uri) method invokes getEndpoint(uri) on the CamelContext associated with the RouteBuilder object to get the specified Endpoint and then puts a FromBuilder wrapper around this Endpoint. The FromBuilder.filter(Predicate predicate) method creates a FilterProcessor object for the Predicate (that is, condition) object built from the header(“foo”).isEqualTo(“bar”) expression. In this way, these operations incrementally build up a Route object (with a RouteBuilder wrapper around it) and add it to the CamelContext object associated with the RouteBuilder.

REST DSL

https://camel.apache.org/manual/rest-dsl.html

  rest("/customers/")
      .get("/{id}").to("direct:customerDetail")
      .get("/{id}/orders")
        .param().name("verbose").type(RestParamType.query).defaultValue("false").description("Verbose order details").endParam()
          .to("direct:customerOrders")
      .post("/neworder").to("direct:customerNewOrder");

参考

https://camel.apache.org/
http://www.masterspringboot.com/camel/getting-started-with-camel-3/

https://camel.apache.org/manual/dsl.html
代码参考:
https://github.com/fmarchioni/masterspringboot/tree/master/camel

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值