0

I have a Spring Boot app, and there are a few processes that run every hour. I'm using @Scheduled with a cron expression for that.
Now I've got a change request, which consists in allowing the UI change to change the frequency of the processes in runtime, which is specified in a database table like the one below.

Table:
Id,  name,   cronexp,      some_foreing_key
1,  "John",  "0 0 1 * * *",  4
2,  "Steve", "0 0 2 * * *",  8

For now, what I imagine is maybe having a method annotated with @Scheduled that executes every number of minutes, which then somehow checks the time that has passed against those expressions. Any ideas?

CCC
  • 2,642
  • 7
  • 40
  • 62
  • 1
    I am getting 100% what you want to achieve but I have to tell you that `@Scheduled` annotation parameters cannot be changed dynamically once you've set them. I would also suggest to re-structure the question to provide some more information about your use case. – akortex Mar 23 '19 at 22:57
  • Take a look at Quartz library, it can help you accomplish requirements needed. – lzagkaretos Mar 24 '19 at 15:13
  • CHeck this question https://stackoverflow.com/questions/41186633/spring-update-scheduler – Dehan Mar 24 '19 at 15:36

1 Answers1

1

I used @Scheduled along with Quartz.
@Scheduled checks the cron expression every minute by using CronExpression.isStatisfiedBy.
An expression can be like this: * 0/30 * * * ?, which means that the check will be done by minute 0 and minute 30, and that's the reason why we need to check every minute: because it needs to match exactly those particular minutes. Also for the same reason, the cron expression from the DB should exclude seconds or start with *.

@Scheduled(cron = "0 */1 * * * ?")
public checkSchedules() {
...
CronExpression cronExpression = new CronExpression(cronExpressionStringFromDB);
  if (cronExpression.isSatisfiedBy(new Date())) {
     //run stuff
  } 
}
CCC
  • 2,642
  • 7
  • 40
  • 62