0

I am using Apache Camel with spring boot and a camel-config.xml file. 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:

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.

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.
  • According to the Apache Camel documentation, the misfire will only be recorded if quartz is in clustered mode.
  • 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. I am not sure if it was picked up though (put it in src/resources and created a bean that points to it). 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 * * * * ?"

I am completely out of options :x Would appreciate any help :)

Jerebenz
  • 45
  • 7

1 Answers1

0

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 :

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();
        }
    }

}
Jerebenz
  • 45
  • 7