Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring @scheduled cron with variable

in our system we use a settings class to point to the properties file, depends on which eve it loads different property file. To access specific property, we call 'Settings.getString('property_name_here')'.

In my code, i loaded the @scheduled cron expression to a variable and try to passed to the @scheduled annotation, but it won't work,

here is my code: in properties file:

cron.second=0
cron.min=1
cron.hour=14

in constructor i have:

this.cronExpression = new StringBuilder()
            .append(settings.getString("cron.second"))
            .append(" ")
            .append(settings.getString("cron.min"))
            .append(" ")
            .append(settings.getString("cron.hour"))
            .append(" ")
            .append("*").append(" ").append("*").append(" ").append("*")
            .toString();

which create a String of "0 1 14 * * *", it's a valid con expression

in the scheduled task i have:

@Scheduled(cron = "${this.cronExpression}")
public void scheduleTask() throws Exception {
         ....
 }

when I ran the code it complaint: Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'scheduleTask': Cron expression must consist of 6 fields (found 1 in "${this.cronExpression}")

then I changed this.cronExpression to a list of String:

    this.cronExpression = Lists.newArrayList();
    this.cronExpression.add(settings.getString("cron.second"));
    this.cronExpression.add(settings.getString("cron.min"));
    this.cronExpression.add(settings.getString("cron.hour"));
    this.cronExpression.add("*");
    this.cronExpression.add("*");
    this.cronExpression.add("*");

but still got the same error, so what exactly is the cron expression supposed to be?

like image 766
user468587 Avatar asked Jan 19 '18 00:01

user468587


People also ask

How does @scheduled work 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.

How do I run multiple scheduler in Spring boot?

We can easily schedule tasks in spring boot by using @Scheduled annotation. And then we need to enable scheduling by adding @EnableScheduling annotation to a spring configuration class. Spring uses ThreadPoolTaskScheduler for scheduled tasks, which internally delegates to a ScheduledExecutorService.

What is fixed delay in Spring scheduler?

Schedule a Task at Fixed Delay In this case, the duration between the end of the last execution and the start of the next execution is fixed. The task always waits until the previous one is finished. This option should be used when it's mandatory that the previous execution is completed before running again.


1 Answers

What I did and it worked for me:

In properties file:

my.cron.expression=0 3 * * * ?

In code:

@Scheduled(cron = "${my.cron.expression}")
public void scheduleTask(){
  ...
}
like image 147
Joanna Kotuła Avatar answered Oct 12 '22 23:10

Joanna Kotuła