Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 613
John Avatar asked Jun 13 '17 06:06

John


People also ask

What is cron expression 0 * * * *?

*/5 * * * * Execute a cron job every 5 minutes. 0 * * * * Execute a cron job every hour.

What is @scheduled in Spring?

We can turn any method in a Spring bean for scheduling by adding the @Scheduled annotation to it. The @Scheduled is a method-level annotation applied at runtime to mark the method to be scheduled. It takes one attribute from cron , fixedDelay , or fixedRate for specifying the schedule of execution in different formats.

What is @scheduled in Java?

@Target(value=METHOD) @Retention(value=RUNTIME) public @interface Schedule. Schedule a timer for automatic creation with a timeout schedule based on a cron-like time expression. The annotated method is used as the timeout callback method. All elements of this annotation are optional.


1 Answers

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

like image 115
StanislavL Avatar answered Oct 25 '22 11:10

StanislavL