Sam
Sam

Reputation: 798

Task scheduling using cron expression from properties file

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.

Upvotes: 36

Views: 82801

Answers (5)

Oleh Tatsiun
Oleh Tatsiun

Reputation: 929

For me works in that way:

"#{${cronExpression}}

Upvotes: 0

Rajeev
Rajeev

Reputation: 1840

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.

    Upvotes: 34

  • karthik akinapelli
    karthik akinapelli

    Reputation: 720

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

    Upvotes: 46

    Gulfam Suleman
    Gulfam Suleman

    Reputation: 71

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

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

    Upvotes: 4

    sitakant
    sitakant

    Reputation: 1878

    Try something like

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

    For more, click here

    Upvotes: 2

    Related Questions