使用属性文件中的cron表达式进行任务调度 [英] Task scheduling using cron expression from properties file

查看:180
本文介绍了使用属性文件中的cron表达式进行任务调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一份cron作业:

  @Scheduled(cron = $ {process.virtual.account.start} )
公共无效的ecomProcessVirAccOrderPaymentsScheduler(){
LOGGER.info(开始---> + this.getClass()。getCanonicalName()+ .ecomProcessVirAccOrderPaymentsScheduler()方法);
schedulerJobHelper.ecomProcessVirAccOrderPaymentsScheduler();
LOGGER.info( End ---> + this.getClass()。getCanonicalName()+ .ecomProcessVirAccOrderPaymentsScheduler()方法);
}

我想将cron属性与 @一起使用已计划的批注将从外部属性文件填充。目前,我正在从应用程序范围内的属性文件中获取它。
我可以获取该值,但不能与 @Schedule 批注一起使用。

解决方案

您正在使用哪个版本的spring框架?如果小于3.0.1,则无法使用。


在3.0.0春季发布的错误报告,并已在3.0.1版中进行了修复。您必须在cron表达式中使用



  • 在applicationContext.xml中为PropertyPlaceHolderConfigurer类输入一个条目,该类为

     < bean id =" placeholderConfig" 
    class = org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。
    <属性名称=位置>
    < list>
    < value> classpath:ApplicationProps.properties< / value>
    < / list>
    < / property>
    < / bean>



    之后在@Scheduled方法中使用它,例如


    更新:如果您使用的是 spring boot ,则无需执行任何操作,下面的代码摘录应该可以使用。



      @Scheduled(cron = $ {instructionSchedularTime})
    public void load(){
    }

    注意:固定延迟和固定利率无法从占位符获取财产价值,因为它们需要长期价值。 Cron属性将参数设为String,因此您可以使用占位符。


    I have written a cron job:

    @Scheduled(cron="${process.virtual.account.start}")
    public void ecomProcessVirAccOrderPaymentsScheduler() {
        LOGGER.info("Start --->" + this.getClass().getCanonicalName() + ".ecomProcessVirAccOrderPaymentsScheduler() Method");
        schedulerJobHelper.ecomProcessVirAccOrderPaymentsScheduler();
        LOGGER.info("End --->" + this.getClass().getCanonicalName() + ".ecomProcessVirAccOrderPaymentsScheduler() Method");
    }
    

    I want to get the cron attribute used with @Scheduled annotation to be populated from a external properties file. Currently I am fetching it from a property file inside the application scope. I am able to fetch the value, but not able to use it with @Schedule annotation.

    解决方案

    Which version of spring framework are you using? This won't work if it is less than 3.0.1.

    Bug Report here in Spring 3.0.0 and it has been fixed in 3.0.1.

    So if you are using Spring 3.0.1 or greater then following things you have to do to use in cron expression

  • Make an entry in applicationContext.xml for PropertyPlaceHolderConfigurer class that is

    <bean id="placeholderConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:ApplicationProps.properties</value>
            </list>
        </property>
    </bean>
    

    After That Use it in using the @Scheduled method like

    Update: In case if you are using spring boot no need to do anything, below code excerpt should work.

    @Scheduled(cron="${instructionSchedularTime}")
    public void load(){
    }
    

    Note: fixed delay and fixed-rate cann't take property value from placeholder because they take long value. Cron attribute take argument as String so you can use placeholder for that.

    这篇关于使用属性文件中的cron表达式进行任务调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆