Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you implement cron jobs in Spring Boot applications?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 17, 2024
UpdatedNov 16, 2026
6 min read

To implement a cron job in Spring Boot, add @EnableScheduling to a configuration class and annotate a no-argument method of a Spring bean with @Scheduled(cron = "..."). Spring uses a six-field cron expression (second, minute, hour, day of month, month, day of week) and runs the method on its task scheduler, with no external cron daemon needed.

The default scheduler is single-threaded and every application instance runs its own jobs, so a few settings matter once you go beyond a demo: the time zone, the thread pool size, and what happens when you run several instances.

Table of Contents

  • How Spring cron expressions work
  • Enabling scheduling and writing a job
  • Cron, fixedRate and fixedDelay compared
  • Externalizing the schedule
  • Thread pool, overlapping runs and virtual threads
  • Running cron jobs on multiple instances
  • Testing scheduled methods
  • Frequently Asked Questions
    • Why is my @Scheduled method never called?
    • Does Spring cron support five fields like Linux crontab?
    • How do I run a job only once across several servers?
    • Can I change the schedule at runtime?
  • Summary

How Spring cron expressions work

Spring parses cron expressions with CronExpression and expects six fields, separated by spaces. This differs from the five-field Unix crontab, which has no seconds field.

PositionFieldAllowed values
1Second0-59
2Minute0-59
3Hour0-23
4Day of month1-31
5Month1-12 or JAN-DEC
6Day of week0-7 (0 and 7 are Sunday) or MON-SUN

Common special characters are * (every value), , (a list), - (a range), / (a step) and ? (no specific value). Spring 5.3 and later also accept L (last day), W (nearest weekday) and # (nth weekday of the month), plus the macros @hourly, @daily, @weekly, @monthly and @yearly.

ExpressionMeaning
0 0 2 * * *Every day at 02:00:00
0 */5 * * * *Every 5 minutes
0 30 9 * * MON-FRI09:30 on weekdays
0 0 0 1 * *Midnight on the first day of every month
@dailyEvery day at midnight

You can check an expression before shipping it:

java

CronExpression.parse throws IllegalArgumentException for an invalid expression, which is useful for validating user-supplied schedules.

Enabling scheduling and writing a job

Spring Boot needs no extra dependency, because scheduling lives in spring-context. Enable it once, then add scheduled methods to any bean.

java
java

The annotated method must return void (any return value is ignored) and take no parameters. Always set zone for jobs that must fire at a business-relevant local time; without it the JVM default time zone is used, which differs between a laptop and a container running in UTC. Daylight saving changes can skip or repeat a local time, so avoid scheduling important jobs at 02:00 to 03:00 in zones that change clocks.

Cron, fixedRate and fixedDelay compared

@Scheduled supports three trigger styles. Pick the one that matches the requirement.

AttributeWhen it runsTypical use
cronAt calendar timesNightly reports, month-end jobs
fixedRateEvery N ms measured from the start of the previous runHeartbeats, metrics
fixedDelayN ms after the previous run finishedPolling, queue draining
java

The ...String variants accept ISO-8601 durations such as PT30S or property placeholders, so the value can live in configuration.

Externalizing the schedule

Hard-coded expressions are hard to change per environment. Read them from properties instead:

properties
java

To switch a job off in an environment, set the property to the special value -. Spring treats it as a disabled trigger (the constant Scheduled.CRON_DISABLED), and the method is never called.

Thread pool, overlapping runs and virtual threads

By default Spring Boot auto-configures a ThreadPoolTaskScheduler with a pool size of 1. One slow job therefore delays every other scheduled job. Raise the pool size with a property:

properties

On Java 21 or later with Spring Boot 3.2 or newer, you can run scheduled work on virtual threads with spring.threads.virtual.enabled=true. Scheduling then uses a SimpleAsyncTaskScheduler, which suits blocking I/O jobs.

A single @Scheduled method is not started again while its previous run is still going on the same scheduler. If you add @Async to the method, runs can overlap, so use it only for jobs that are safe to run concurrently.

Running cron jobs on multiple instances

@Scheduled has no cluster awareness. If you run three replicas, the job fires three times. Options:

  1. Run the scheduler in a single dedicated instance or a Kubernetes CronJob.
  2. Use a distributed lock library such as ShedLock, which stores a lock row in your database or Redis and lets only one node run each execution.
  3. Use Quartz with a JDBC job store (spring-boot-starter-quartz) when you need persistent, clustered, programmatically managed schedules.

Keep jobs idempotent anyway, because a retry or a restart can always run one twice.

Testing scheduled methods

Do not wait for the clock in tests. Keep the method body thin, move the logic into a service, and test the service directly. To check the schedule itself, validate the expression:

java

For an integration test that needs the scheduler to fire, Awaitility lets you wait for a side effect with a timeout instead of sleeping.

Frequently Asked Questions

Why is my @Scheduled method never called?

The usual causes are a missing @EnableScheduling, a class that is not a Spring bean, a method that takes parameters, or a disabled expression (-). Check that the class is picked up by component scanning.

Does Spring cron support five fields like Linux crontab?

No. Spring requires six fields with seconds first. A five-field expression fails with an IllegalArgumentException at startup.

How do I run a job only once across several servers?

Use a distributed lock such as ShedLock, or move the job to a single instance or a Kubernetes CronJob.

Can I change the schedule at runtime?

Not with @Scheduled. Implement SchedulingConfigurer and register a trigger that reads the expression from a database or property source, or use Quartz.

Summary

Add @EnableScheduling, annotate a void, no-argument bean method with @Scheduled(cron = "..."), and remember that Spring cron has six fields starting with seconds. Set zone explicitly, move expressions into properties, increase the scheduler pool size beyond the default of one, and use a lock when you run more than one instance.

Share