问题
Is there an idea of a while loop in Camel? We are using Camel for doing batch processing (not really the remit of an ESB I know). I want to keep checking on the status of something else whilst I am processing messages in the ESB. I can only find a loop that loops for a defined number of times, i.e. for testing or a quartz timer that will check every x seconds. Neither of these are really suitable.
Any suggestions, or am I asking for something simply outside of the remit of an ESB?
回答1:
What about doing something like this:
<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:
How about the camel timer:
?
E.g.
from("timer://foo?fixedRate=true&period=1000")
.to("bean:myBean?method=someMethodName");
Reference: Camel Timer Component
回答3:
Try using DynamicRouter.
It uses an Expression class to determine the next route to dispatch the exchange. If the expression returns null it means that it will stop routing.
This way you can evaluate the exchange contents and continue routing to the same route until you decide is time to stop, and then return 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
}
});
来源:https://stackoverflow.com/questions/20240882/is-there-a-while-loop-in-camel