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.
*/5 * * * * Execute a cron job every 5 minutes. 0 * * * * Execute a cron job every hour.
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.
@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.
Just add an expression @Scheduled(cron = "${some.profile.cron}")
to swap the cron
depending on selected profile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With