首页 > 解决方案 > Apache Camelquartz2 cron 失火

问题描述

我正在使用带有 spring boot 和 camel-config.xml 文件的 Apache Camel。我创建了一个每秒运行的简单路由并运行一个类方法:

<camelContext xmlns="http://camel.apache.org/schema/spring" id="myContext" trace="true" streamCache="true" useMDCLogging="true">       
    <route id="testCron">
        <from uri="quartz2://TestCron?cron=0/1 * * * * ?" />
        <to uri="bean:folder.MyClass?method=test" />
    </route>
</camelContext>

该类只是有一个计数器 int 递增并显示:

package folder;

public class MyClass {

    private static int count = 0;

    public static void test(Exchange exchange) throws Exception {
        count = count + 1;
        System.out.println(count);
    }    

}

我有另一段代码(与显示无关)可以启动和停止上述路线。我遇到的问题是停止路线时,等待 5 秒钟然后重新开始。

它不是继续它离开计数的地方,而是赶上路线停止时它没有做的每一次迭代。

我读了很多书试图解决这个问题。我学到的是以下内容:

我没有运气的尝试:

我完全没有选择:x 将不胜感激任何帮助 :)

标签: javacronapache-camelquartzmisfire-instruction

解决方案


我无法找到修改失火指令的方法,但我找到了解决方法。

我现在停止端点,而不是使用 context.stopRoute(routeId) 停止路由:

public static void stopRoute(Exchange exchange) throws Exception {

    String beanId = (String) exchange.getIn().getHeader("beanId");
    String routeId = (String) exchange.getIn().getHeader("routeId");

    SpringCamelContext context = (SpringCamelContext) exchange.getContext().getRegistry().lookupByName(beanId);

    for (Route route : context.getRoutes()) {
        if (route.getId().equals(routeId)) {
            route.getEndpoint().stop();
        }
    }

}

推荐阅读