How to config cron value of @Scheduled in application.properties

John picture John · Jun 13, 2017 · Viewed 11.3k times · Source

I am using spring-schedule like this.

@Component
@EnableScheduling
public class ScheduledTasks {

    @Autowired
    private ISomeJob someJob;

    /**
     * do a Job every 5 minutes.
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void foo(){
        someJob.doSomething();
    }
}

It worked. But there is a problem.
I have two profiles named debug and release.
I want do this job every 5 minutes in debug but per hour in release.
So is there any way to config the value of cron in application.properties.

Answer

StanislavL picture StanislavL · Jun 13, 2017

Just add an expression @Scheduled(cron = "${some.profile.cron}") to swap the cron depending on selected profile.