简体   繁体   English

Apache Camelquartz2 cron失火

[英]Apache Camel quartz2 cron misfire

I am using Apache Camel with spring boot and a camel-config.xml file. 我正在将Apache Camel与Spring Boot和camel-config.xml文件一起使用。 I created a simple route that runs every second and runs a class method: 我创建了一条简单的路由,该路由每秒钟运行一次并运行一个类方法:

<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>

The class simply has a counter int that is incremented and displayed: 该类仅具有一个递增和显示的计数器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);
    }    

}

I have another piece of code (irrelevant to show) that can start and stop the above route. 我还有另一段代码(与显示无关)可以启动和停止上述路线。 The issue I am having is when stopping the route, waiting 5 seconds and start it back. 我遇到的问题是停止路线,等待5秒钟并重新启动路线。

Instead of continuing where it left the count, it catches up every iteration that it did not do while the route was stopped. 它没有继续它离开计数的地方,而是追赶了路线停止时未执行的每次迭代。

I've read a lot trying to solve that. 我已经读过很多文章试图解决这个问题。 What I learnt was the bellow: 我学到的是波纹管:

  • What happends is called "misfire" 发生的事情称为“失火”
  • There is a parameter that allows to configure the misfire instructions 有一个参数可以配置断火指令
  • According to the Apache Camel documentation, you cannot use trigger.XXX options (that would allow to configure the misfire instructions) if you are using a cron expression. 根据Apache Camel文档,如果您使用的是cron表达式,则不能使用trigger.XXX选项(允许配置断火指令)。
  • According to the Apache Camel documentation, the misfire will only be recorded if quartz is in clustered mode. 根据Apache Camel文档,仅当石英处于群集模式时才会记录失火。
  • You can configure the quartz properties to disable the clustered mode (I don't need it). 您可以配置石英属性以禁用群集模式(我不需要它)。

What I tried without luck: 我没有运气的尝试:

  • Created a quartz property file with org.quartz.jobStore.isClustered: false. 使用org.quartz.jobStore.isClustered创建了一个石英属性文件:false。 I am not sure if it was picked up though (put it in src/resources and created a bean that points to it). 我不确定是否将其拾取(将其放入src / resources并创建一个指向它的bean)。 It did not solve the issue. 它没有解决问题。
  • Tried to set the misfireInstruction as a trigger option in the route quartz2://TestCron?trigger.misfireInstruction=2&cron=0/1 * * * * ?" 试图将misfireInstruction设置为路由solar2:// TestCron?trigger.misfireInstruction = 2&cron = 0/1 * * * *?的触发选项。

I am completely out of options :x Would appreciate any help :) 我完全没有选择余地:x将不胜感激:)

I was not able to find a way to modify the misfire instructions but I found a workaround. 我找不到修改失火指令的方法,但找到了解决方法。

Instead of stopping the route with context.stopRoute(routeId), I am now stopping the endpoint : 我现在不再使用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();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM