34

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.

Will
  • 11,276
  • 9
  • 68
  • 76
Sam
  • 728
  • 1
  • 9
  • 26

5 Answers5

45

it is working in spring boot.

@Scheduled(cron="${cronExpression}")
private void testSchedule()  {
    System.out.println("Helloooo");
}

in application.properties I have a property like this as below:

cronExpression=* * * ? * *
Dherik
  • 17,757
  • 11
  • 115
  • 164
karthik akinapelli
  • 710
  • 1
  • 7
  • 14
33

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.

  • Rajeev
    • 1,730
    • 3
    • 20
    • 33
    • 6
      As of Spring 3.2.2 you now have: [fixedDelayString](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html#fixedDelayString--) and [fixedRateString](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html#fixedRateString--) as well. – Håvard Geithus Jul 09 '15 at 13:59
    4

    You can assign value directly from property file, i'm using spring boot BTW

    @Scheduled(cron = "${com.oracle.fusion.cron}")
    public void getInvoiceInterfaceHeader() {
    
    }
    
    2

    Try something like

    @Configuration
    @PropertySource("/path/to/file")
    public class LoadPropertiesFile{
       //Other project configurations
    }
    

    For more, click here

    sitakant
    • 1,766
    • 2
    • 18
    • 38
    0

    For me works in that way:

    "#{${cronExpression}}
    
    Oleh Tatsiun
    • 749
    • 6
    • 7
    • Very curious as to that syntax and as I am sure others would be benefited by it could you break down the "what/why"? – JGlass Sep 16 '22 at 20:40
    • This gives error: Caused by: org.springframework.expression.spel.SpelParseException: Expression [* 1 * * * *] @0: EL1070E: Problem parsing left operand – Amit Chaurasia Jul 10 '23 at 20:31