camle dynamic(动态转发)

1 最近公司紧急需求要求实现camel 到动态转发功能 由于以前没有接触过camle 然后也不太了解 camel 所以通宵研究 记录一些 方法希望能帮助请您
2 官网 camel dynamic 由于我们公司jdk 是1.8 所以就最高选择这个版本
3 看官方给的示例 注意看我 解释和标注的地方

/**
 * Use this method to compute dynamic where we should route next.
 *
 * @param body the message body
 * @param properties the exchange properties where we can store state between invocations
 * @return endpoints to go, or <tt>null</tt> to indicate the end
 */
public String slip(String body, @ExchangeProperties Map<String, Object> properties) {
    bodies.add(body);

    // get the state from the exchange properties and keep track how many times 
  1  body
  这个body 就是我们接受的数据本体
  2  properties
  这个properties 就是一个临时存储器 你可以看作一个你跳出循环的一个 开关的保存的地方

当数据-》camel-kafka->(dynamic)的时候当前一条数据 会无限的循环的执行
-return direct:a(他会一直发送当前的一条) 什么时候提交偏移量 而消费 下一条呢 就是当你return null的时候
所有上面这句的翻译是 从properties获得一个状态 他决定着你循环多少次
    // we have been invoked
    int invoked = 0;
    Object current = properties.get("invoked");
    if (current != null) {
        invoked = Integer.valueOf(current.toString());
    }
    invoked++;
    // and store the state back on the properties
    properties.put("invoked", invoked);

    if (invoked == 1) {
        return "mock:a";
    } else if (invoked == 2) {
        return "mock:b,mock:c";
    } else if (invoked == 3) {
        return "direct:foo";
    } else if (invoked == 4) {
        return "mock:result";
    }

    // no more so return null
    return null;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值