Apache Camel是个什么玩意?

低功耗蓝牙项目,需要一块懂省电的板

思澈 SF32LB52 芯片,BLE 协议栈深度优化,上手即开发

初探Apache Camel

Apache Camel 是基于EIP(Enterprise Integration Patterns)的一款开源框架。适用于异构系统间的集成和处理数据。

camel示意

核心

  • Camel Context: Camel 上下文
    • 为了运行和管理你的路由,Camel 有一个名为 Camel Context 的容器。您的路线在此引擎内运行。 你几乎可以把它想象成一个迷你应用服务器。
    • 当 Camel启动时,它会读取您的路由定义(在 Java 或 XML 中),创建路由,将它们添加到 Camel 上下文,并启动 Camel 上下文。
    • 当 Camel终止时,它会关闭您的路由,并关闭 Camel 上下文。
  • Endpoint:用于收发消息的终端。
    • Endpoint是Camel与其他系统进行通信的设定点。
    • 使用URI来识别endpoint位置
  • Exchange:消息之间通信的抽象的会话。
    • Properties:Exchange对象贯穿整个路由执行过程中的控制端点、处理器甚至还有表达式、路由条件判断。为了让这些元素能够共享一些开发人员自定义的参数配置信息,Exchange以K-V结构提供了这样的参数配置信息存储方式。
    • Message IN/OUT:当Endpoint和Processor、Processor和Processor间的Message在Exchange中传递时,Exchange会自动将上一个元素的输出作为这个元素的输入使用。
  • Processor:消息处理器。
    • org.apache.camel.Processor 是一个消息接受者和消息通信的处理器。
  • Routing:路由规则。
    • Routing用于处理Endpoint和Processor之间、Processor和Processor之间的路由跳转。
  • components:组件库,也可视作endpoint工厂
    -你可以在这里查看所有组件

如何创建Camel程序

  • java整合Apache Camel

    • `public class Test2 {
      public static void main(String[] args) {
      final CamelContext camelContext = new DefaultCamelContext();
      try {
      camelContext.addRoutes(new RouteBuilder() {
      @Override
      public void configure() throws Exception {
      from(“timer:initial//start?period=10000&repeatCount=7”)
      .routeId(“timer–test”)
      .to(“log:executed”);
      }
      });
      } catch (Exception e) {
      e.printStackTrace();
      }
      camelContext.setTracing(true);
      camelContext.start();

      try {
      Thread.sleep(60000);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      camelContext.stop();

    }
    }`

  • spring boot整合Apache Camel

    • 添加maven配置
      • <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>3.15.0</version> </dependency>
        Camel自动配置会为您创建一个SpringCamelContext并负责该上下文的正确初始化和关闭。
        Camel自动配置还从Spring上下文中收集所有RouteBuilder实例,并将它们自动注入到提供的CamelContext中。
    • 设置自己的路由
      • `package com.shuinfo.cameltest.route;

import com.shuinfo.cameltest.process.Process1;
import org.apache.camel.builder.RouteBuilder;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyCamelRoute3 extends RouteBuilder {

String say(){
    int i = RandomUtils.nextInt(1, 4);
    if (i == 1) {
        return "foo";
    }
    if (i == 2) {
        return "aaa";
    }
    if (i == 3) {
        return "bbb";
    }
    return "other";
}

@Override
public void configure() throws Exception {

    from("timer:hello?period=10000").routeId("hello")
            .transform().method("myCamelRoute3", "say")
            .choice()
            .when(simple("${body} contains 'foo'"))
            .to("log:foo")
            .when(simple("${body} contains 'aaa'"))
            .to("log:aaa")
            .otherwise()
            .to("log:other")
            .end();
}

}
`

使用实例

从文件夹1移动文件到文件夹2,并将文件内容记录日志

低功耗蓝牙项目,需要一块懂省电的板

思澈 SF32LB52 芯片,BLE 协议栈深度优化,上手即开发

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值