Sub topic 5
Moving schedules into configuration
This chapter assumes you can write a @Scheduled method with fixedRate, fixedDelay or cron, and that you know how application.yml and Spring profiles work.
Why hard-coded schedules cause trouble
A literal like cron = "0 0 3 * * *" is fixed at compile time. The cleanup job that should run nightly in production would also run on every developer's machine, and a test environment might need it every minute. Moving the schedule into configuration lets each environment set its own value without a rebuild.
Placeholders in @Scheduled
The cron, zone, fixedDelayString, fixedRateString and initialDelayString attributes accept ${...} placeholders. Spring resolves them from the Environment when it creates the scheduled task.
@Component
public class CleanupJob {
@Scheduled(cron = "${cleanup.cron:0 0 3 * * *}", zone = "${cleanup.zone:UTC}")
public void cleanup() {
// delete expired records
}
@Scheduled(fixedDelayString = "${cleanup.retry-delay:PT30S}")
public void retry() {
// retry failed deletions
}
}The text after the first : is the default. It applies when no property sets the key. The cron default here is 0 0 3 * * *, which is 03:00 every day.
The numeric attributes such as fixedDelay cannot take placeholders because they are long values. Use the String variants instead, which accept:
- Plain milliseconds, such as
5000. - ISO-8601 durations, such as
PT30S(30 seconds) orPT5M(5 minutes).
Overriding per environment
Each profile can then set its own values:
# application.yml
cleanup:
cron: "0 0 3 * * *"
zone: Europe/Berlin
retry-delay: PT30S# application-test.yml
cleanup:
cron: "0 */1 * * * *"
retry-delay: PT5SEnvironment variables also work. Setting CLEANUP_CRON overrides cleanup.cron, which is convenient in containers.
Disabling a task with a property
You can switch a task off in one environment by giving cron the special value -. Spring defines it as the constant Scheduled.CRON_DISABLED. The task is registered but never triggers.
# application-local.yml
cleanup:
cron: "-"Quote the dash in YAML. An unquoted - is parsed as a list marker, not as the string -.
- only works for cron. To disable a fixed-rate task, you need a different approach, such as @ConditionalOnProperty on the bean that holds it.
Common errors
- Missing key and no default.
@Scheduled(cron = "${cleanup.cron}")with no property set fails at startup withCould not resolve placeholder. This is useful, because the application refuses to start with a missing schedule. Add a default only when a fallback is acceptable. - A number where a string is needed.
fixedDelay = "${...}"does not compile. UsefixedDelayString. - Wrong cron field count. The value is not checked until startup. A five-field Unix expression such as
0 3 * * *in a property fails then with anIllegalStateException, because Spring needs six fields. Keep the example in the property's comment. - Spaces in defaults. A cron default contains spaces. That is fine inside the placeholder, but quote the value in YAML if it contains
*at the start, because*begins an alias in YAML.
Exercise
A job should run every 10 minutes by default, and operators must be able to change the interval through the report.interval property. Which annotation attribute do you use, and how do you write it?
Show answer
Use fixedDelayString (or fixedRateString) because the numeric attributes cannot take placeholders. For example, @Scheduled(fixedDelayString = "${report.interval:PT10M}"). The default PT10M is an ISO-8601 duration meaning 10 minutes, and setting report.interval in any property source overrides it.