Configuring Scheduled Tasks with Annotation Metadata

To make your application’s tasks configurable, you can use annotation metadata and property placeholder configuration. For example:

Allow tasks to be configured

  1. @Scheduled(fixedRate = "${my.task.rate:5m}",
  2. initialDelay = "${my.task.delay:1m}")
  3. void configuredTask() {
  4. System.out.println("Executing configuredTask()");
  5. }

Allow tasks to be configured

  1. @Scheduled(fixedRate = '${my.task.rate:5m}',
  2. initialDelay = '${my.task.delay:1m}')
  3. void configuredTask() {
  4. println "Executing configuredTask()"
  5. }

Allow tasks to be configured

  1. @Scheduled(fixedRate = "\${my.task.rate:5m}",
  2. initialDelay = "\${my.task.delay:1m}")
  3. internal fun configuredTask() {
  4. println("Executing configuredTask()")
  5. }

The above example allows the task execution frequency to be configured with the property my.task.rate, and the initial delay to be configured with the property my.task.delay.