Scheduling background jobs with Spring @Scheduled and Quartz
Most Java applications eventually grow a second clock. Alongside handling HTTP requests, a Spring Boot service usually needs to sweep up yesterday's transactions, refresh a third-party feed, or rebuild a search index while the team is having dinner in Melbourne. Those recurring chores are background jobs, and Spring gives you two dependable ways to run them: the annotation-driven @Scheduled method and the full Quartz scheduler.
@Scheduled gets you moving with a single annotation, a cron expression, and no external dependency. Quartz adds persistence, misfire recovery and clustered execution, at the price of extra configuration. Choosing the wrong tool usually means a job that quietly stops firing, so it pays to know where each one stops being useful.
Local context sharpens those decisions. Australian servers commonly run on AEST or AEDT, and daylight saving moves the wall clock twice a year — forward on the first Sunday in October, back on the first Sunday in April. A job pinned to "02:00" will therefore fire at 03:00 or 01:00 depending on the week, which matters when it has to land before the big four banks in Sydney and Melbourne cut over their overnight settlement files.
What follows builds both approaches in a Spring Boot project: configurable triggers, a sensible thread pool, and monitoring hooks so you can see when a job last ran. It also covers the operational habits you need when software touches money and compliance — predictable run times, reporting periods that close cleanly against the 30 June financial year, and audit trails that survive a regulator's questions.
Getting started with @Scheduled
Enable scheduling with @EnableScheduling on your configuration class, then annotate a public method. Spring ships with a single-threaded scheduler by default, and that detail trips up plenty of first attempts: one slow job blocks every other job on the classpath. Configure a ThreadPoolTaskScheduler with an explicit pool size and a thread name prefix so logs remain readable.
Three attributes cover most needs. fixedRate runs the job relative to the start of the previous run, fixedDelay waits a gap after each run finishes, and cron takes a six-field expression. For anything that calls a remote API, prefer a delay over a rate, because a fixed rate will queue up back-to-back invocations when the job overruns.
Keep configuration in properties rather than in the annotation body. fixedDelayString = "${app.reconcile.delay-ms:900000}" lets you retune the schedule per environment without touching code, and zone = "Australia/Melbourne" stops the cron expression drifting with the host's default locale.
When Quartz earns its keep
Quartz separates the job from its schedule. A JobDetail holds the logic, a Trigger holds the timing, and JobDataMap carries parameters between them. That separation is the real advantage: you can attach several triggers to one job, pause a trigger while you patch a bug, and inspect or alter schedules at runtime rather than at redeploy.
Because triggers live in a database, scheduling becomes an API concern. Exposing endpoints that add, reschedule or suspend a job fits naturally alongside creating a RESTful API controllers, and an operations team in Brisbane can disable a failing feed without waiting on a deployment window. Wiring Quartz into Spring is straightforward too: register SpringBeanJobFactory with an autowire-capable bean factory and your job beans receive constructor injection like any other component.
The JDBC-backed job store also unlocks clustering. When several instances run behind a load balancer, Quartz coordinates through shared tables so a trigger fires exactly once across the cluster, rather than once per instance.
Time zones, misfires and the Australian calendar
Time zone handling is where schedulers earn or lose trust. Tasmania skips daylight saving entirely, while Lord Howe Island shifts by only thirty minutes, and a naive assumption of one Australian offset breaks quietly across the country. Store timestamps in UTC, format them for display in the target zone, and set the trigger's zone explicitly instead of inheriting the JVM default.
Misfires deserve the same attention. When a job is skipped — a server reboots, a deployment overruns — Quartz decides what to do with the missed fire based on the trigger's misfire instruction. The default for cron triggers is to skip the run, which suits a nightly settlement job better than firing three catch-up runs at once. Choose MISFIRE_INSTRUCTION_DO_NOTHING deliberately rather than by accident, and log the outcome so you can prove the run happened.
Operations teams here are generally relaxed about a job slipping a few minutes — no worries, as they say — but strict about hard cut-offs. A report that misses the 07:00 ANZ reconciliation window is a genuine problem, so pick deadlines from the business, not from the code.
Comparing the two approaches
| Aspect | @Scheduled | Quartz |
|---|---|---|
| Setup effort | Annotation and a bean | Job classes, triggers, job store |
| Persistence | None, state lives in the JVM | JDBC job store survives restarts |
| Trigger flexibility | Cron, fixed rate, fixed delay | Multiple triggers, dynamic changes |
| Cluster safety | Needs your own locking | Native Quartz clustering |
| Misfire handling | Silent, depends on your code | Configurable misfire policies |
| Best fit | Local, repeatable, low-risk jobs | Business-critical and runtime-managed jobs |
A hybrid setup is often the sanest result. Let @Scheduled handle housekeeping — cache warming, hourly refreshes, cleanup sweeps — and reserve Quartz for anything with a deadline, a dynamic schedule, or a requirement to run across several instances. Keep the split visible in your code so a new developer can see which bucket a job belongs to.
Practical habits for reliable job execution
Whichever scheduler you choose, the surrounding engineering decides whether it is trustworthy. Wrap job logic so it can be re-run safely, and record start, end, duration and outcome for every execution.
- Give the scheduler a dedicated thread pool with an explicit size and named threads.
- Make each job idempotent, guarded by a database lock or a unique constraint.
- Log start time, end time and outcome, then expose duration through Micrometer metrics.
- Store timestamps in UTC and convert for display, so daylight saving never shifts a report.
- Trigger alerts when a job has not run inside its expected window, rather than waiting for someone to notice.
- Shut down gracefully: stop accepting new triggers, let running jobs finish, and drain the pool.
Done properly, a scheduled task stops being a hidden risk and becomes a documented, observable part of the service — the kind of reliability a bank in Melbourne, a retailer in Perth or a resource operator in Kalgoorlie can all sign off on.