How to dynamically set the quartz cron expression during runtime in camel?

Viewed 404

I need to dynamically set the quartz cron expression during runtime in my camel route. How can this be achieved?

Sample code for current implementation below:

<camel:route id="splitFileRoute">
    <camel:from uri="file:{{file.input.dir}}?
        preMove={{file.inprogress.dir}}&amp;
        move={{file.processed.dir}}&amp;
        scheduler=quartz2&amp;
        scheduler.cron={{file.split.cron.expression}}&amp;
        filter=#fileFilter" />
    <camel:split streaming="true">
        <camel:tokenize token="\n" group="{{file.split.size}}"/>
        <camel:setHeader headerName="channel">
            <camel:simple>{{so.filewrite.channel}}</camel:simple>
        </camel:setHeader>
        <camel:bean ref="fileProcessor" id="spiltFile" method="spiltFile" />
        <camel:removeHeaders pattern="CamelFile*"></camel:removeHeaders>
        <camel:setHeader headerName="CamelFileName">
            <camel:simple>${header.fileName}</camel:simple>
        </camel:setHeader>
        <toD uri="file:${in.header.filePath}?
            doneFileName=${file:name.noext}.eot&amp;
            fileExist=Append" />
    </camel:split>
    <camel:bean ref="fileProcessor" id="resetIndex" method="resetIndex" />
</camel:route>

In the above code, file.split.cron.expression=0+10+0+?+++* is defined in the properties file. Currently it is set to a predefined value, which is set during application startup. What I need to do is to dynamically set this value during runtime.

I need to set the value for cron expression in camel route, with respect to the input from an API call. By setting the value dynamically, I was pointing to this aspect.

1 Answers

You could use a CronScheduledRoutePolicy, to trigger the firing of your route:

CronScheduledRoutePolicy startPolicy = new CronScheduledRoutePolicy();
startPolicy.setRouteStartTime( this.getDynamicExpression() ); 

from("...")
    .routePolicy(startPolicy)
    .noAutoStartup()
    .to("...");

More info at: https://camel.apache.org/manual/latest/cronscheduledroutepolicy.html

Related