【问题标题】:How to dynamically set the quartz cron expression during runtime in camel?如何在骆驼运行时动态设置石英cron表达式?
【发布时间】:2021-04-20 06:11:27
【问题描述】:

我需要在我的骆驼路线中在运行时动态设置石英 cron 表达式。如何实现?

当前实现的示例代码如下:

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

在上面的代码中, file.split.cron.expression=0+10+0+?+++* 在属性文件中定义。目前它被设置为一个预定义的值,该值是在应用程序启动期间设置的。我需要做的是在运行时动态设置这个值。

我需要根据 API 调用的输入设置骆驼路由中的 cron 表达式的值。通过动态设置值,我指向了这个方面。

【问题讨论】:

    标签: cron apache-camel quartz


    【解决方案1】:

    您可以使用CronScheduledRoutePolicy 来触发您的路由:

    CronScheduledRoutePolicy startPolicy = new CronScheduledRoutePolicy();
    startPolicy.setRouteStartTime( this.getDynamicExpression() ); 
    
    from("...")
        .routePolicy(startPolicy)
        .noAutoStartup()
        .to("...");
    

    更多信息请访问: https://camel.apache.org/manual/latest/cronscheduledroutepolicy.html

    【讨论】:

    • 我需要在骆驼路由中设置 cron 表达式的值,相对于 API 调用的输入。通过动态设置值,我指向了这个方面。之前没有在问题中添加此详细信息。
    猜你喜欢
    • 2020-10-22
    • 2016-05-10
    • 2021-01-30
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多