Task scheduling using cron expression from properties file

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

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

}

Try something like

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

For more, click here


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=* * * ? * *

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.

  • Tags:

    Cron