Camel使用多个from()定义RouteDefinition
程序员文章站
2022-05-02 17:25:50
...
Camel使用多个from定义RouteDefinition
1. 使用多个from创建路由
在一个RouteDefinition里可以定义多个from(),相当于创建多个消费者。
示例:创建一个路由,消费三个阻塞队列a, b, c的消息,并将消息内容输出到日志。
public class MultiFromRouteTest extends CamelTestSupport {
@Test
public void testMultiFrom() {
template.sendBody("seda:a", "a");
template.sendBody("seda:b", "b");
template.sendBody("seda:c", "c");
template.sendBody("seda:c", "cccc");
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("seda:a")
.from("seda:b")
.from("seda:c")
.log("receive message body ${body}");
}
};
}
}
执行结果
2. 使用一个from创建多消费者的路由
可以使用一个from多个参数的形式实现上述功能,其路由定义如下:
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("seda:a", "seda:b", "seda:c").log("receive message body ${body}");
}
};
}
3. 多个from之间插入其他处理情形下的处理逻辑
如果在多个from()处理之间加入了其他处理,这些处理将作用于所有的from()。
下面的例子在 1. 的基础上稍微做了改造,在from(“seda:b”)后增加了日志输出处理,该日志输出同样适用于from(“seda:c”)。
public class MultiFromRouteTest extends CamelTestSupport {
@Test
public void testMultiFrom() throws InterruptedException {
template.sendBody("seda:a", "a");
template.sendBody("seda:b", "b");
template.sendBody("seda:c", "c");
template.sendBody("seda:c", "cccc");
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("seda:a").from("seda:b")
.log("receive message body ${body}")
.from("seda:c")
.log("receive message body ${body}");
}
};
}
}
执行结果:阻塞队列 c 中的消息也被输出了2次。
参考
Is it possible to define a Camel route with multiple from(), i.e. consume from multiple endpoints?
推荐阅读
-
vue 使用element-ui中的Notification自定义按钮并实现关闭功能以及如何处理多个通知
-
vue 使用element-ui中的Notification自定义按钮并实现关闭功能及如何处理多个通知
-
zabbix:使用自定义模板监控服务器中的多个节点
-
Word最经典的工具格式刷之一次定义多个格式刷并一直使用
-
vue 使用element-ui中的Notification自定义按钮并实现关闭功能以及如何处理多个通知
-
Camel使用多个from()定义RouteDefinition
-
vue 使用element-ui中的Notification自定义按钮并实现关闭功能及如何处理多个通知
-
使用json来定义函数,在里面可以定义多个函数的实现方法
-
MyBatis——dao代理的使用、深入理解参数(传递一个参数、传递多个参数、使用entity实体类传递、使用自定义类传递、按位置传递、使用Map传递)
-
使用json来定义函数,在里面可以定义多个函数的实现方法