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;
}