【问题标题】:Is there a while loop in Camel?骆驼中有一个while循环吗?
【发布时间】:2013-12-13 00:01:46
【问题描述】:
在 Camel 中有一个 while 循环的想法吗?
我们正在使用 Camel 进行批处理(不是我所知道的 ESB 的职权范围)。当我在 ESB 中处理消息时,我想继续检查其他东西的状态。我只能找到一个循环定义次数的循环,即用于测试或每 x 秒检查一次的石英计时器。这两个都不合适。
有什么建议吗,还是我要求的只是 ESB 职权范围之外的东西?
【问题讨论】:
标签:
while-loop
apache-camel
esb
do-while
【解决方案1】:
做这样的事情怎么样:
<camelContext id="myContext">
<route id ="initializer">
<!--This will be created only once -->
<from uri="timer://foo?repeatCount=1"/>
<to uri="seda:mySedaQueue"/>
</route>
<route id ="myRoute">
<from uri="seda:mySedaQueue"/>
<choice>
<when>
<simple>{your condition if you want to continue}</simple>
...
<to uri="seda:mySedaQueue" />
</when>
<otherwise>
...
</otherwise>
</choice>
</route>
</camelContext>
【解决方案2】:
骆驼timer:呢?
例如
from("timer://foo?fixedRate=true&period=1000")
.to("bean:myBean?method=someMethodName");
参考:Camel Timer Component
【解决方案3】:
尝试使用 DynamicRouter。
它使用一个 Expression 类来确定下一个调度交换的路由。如果表达式返回 null 则意味着它将停止路由。
这样您可以评估交换内容并继续路由到相同的路由,直到您决定停止,然后返回 null。
from("direct:start")
.dynamicRouter(new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
if (<your condition>) return (T) "direct:whileRoute";
return null;
}
})
.to("mock:finish");
from("direct:whileRoute")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// Do whatever you want
}
});