How to parameterize @Scheduled(fixedDelay) with Spring 3.0 expression language?

前端 未结 5 1406
谎友^
谎友^ 2020-11-28 21:21

When using the Spring 3.0 capability to annotate a scheduled task, I would like to set the fixedDelay as parameter from my configuration file, instead of hard-w

相关标签:
5条回答
  • 2020-11-28 21:44

    In Spring Boot 2, we can use Spring Expression Language (SpPL) for @Scheduled annotation properties:

    @Scheduled(fixedRateString = "${fixed-rate.in.milliseconds}")
    public void fixedRate() {
        // do something here
    }
    
    @Scheduled(fixedDelayString = "${fixed-delay.in.milliseconds}")
    public void fixedDelay() {
        // do something here
    }
    
    @Scheduled(cron = "${cron.expression}")
    public void cronExpression() {
        // do something here
    }
    

    The application.properties file will look like this:

    fixed-rate.in.milliseconds=5000
    fixed-delay.in.milliseconds=4000
    cron.expression=0 15 5 * * FRI
    

    That's it. Here is an article that explains task scheduling in detail.

    0 讨论(0)
  • 2020-11-28 21:45

    I guess the @Scheduled annotation is out of question. So maybe a solution for you would be to use task-scheduled XML configuration. Let's consider this example (copied from Spring doc):

    <task:scheduled-tasks scheduler="myScheduler">
        <task:scheduled ref="someObject" method="readLog" 
                   fixed-rate="#{YourConfigurationBean.stringValue}"/>
    </task:scheduled-tasks>
    

    ... or if the cast from String to Long didn't work, something like this would:

    <task:scheduled-tasks scheduler="myScheduler">
        <task:scheduled ref="someObject" method="readLog"
                fixed-rate="#{T(java.lang.Long).valueOf(YourConfigurationBean.stringValue)}"/>
    </task:scheduled-tasks>
    

    Again, I haven't tried any of these setups, but I hope it might help you a bit.

    0 讨论(0)
  • 2020-11-28 21:46

    Spring v3.2.2 has added String parameters to the original 3 long parameters to handle this. fixedDelayString, fixedRateString and initialDelayString are now available too.

    @Scheduled(fixedDelayString = "${my.fixed.delay.prop}")
    public void readLog() {
            ...
    }
    
    0 讨论(0)
  • 2020-11-28 21:57

    You can use the @Scheduled annotation, but together with the cron parameter only:

    @Scheduled(cron = "${yourConfiguration.cronExpression}")
    

    Your 5 seconds interval could be expressed as "*/5 * * * * *". However as I understand you cannot provide less than 1 second precision.

    0 讨论(0)
  • 2020-11-28 22:01

    I guess you can convert the value yourself by defining a bean. I haven't tried that, but I guess the approach similar to the following might be useful for you:

    <bean id="FixedDelayLongValue" class="java.lang.Long"
          factory-method="valueOf">
        <constructor-arg value="#{YourConfigurationBean.stringValue}"/>
    </bean>
    

    where:

    <bean id="YourConfigurationBean" class="...">
             <property name="stringValue" value="5000"/>
    </bean>
    
    0 讨论(0)
提交回复
热议问题