Is there a while loop in Camel?

空扰寡人 提交于 2019-12-08 09:08:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!