How to trigger a scheduled Spring Batch Job?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A scheduled Spring Batch job is usually triggered by combining Spring scheduling with a JobLauncher. The important detail is not just starting the job on a timer, but giving each execution unique JobParameters so Spring Batch treats each run as a new job instance.
The Basic Scheduling Pattern
Spring Batch does not schedule jobs by itself. It provides the batch model, while Spring's scheduling support provides the clock.
The usual setup is:
- define a
Job - inject a
JobLauncher - create a method annotated with
@Scheduled - call
jobLauncher.run(job, params)from that method
First enable scheduling:
Then create the scheduler component:
This example runs at the top of every hour.
Why Unique Parameters Matter
Spring Batch identifies a job instance by its job name plus identifying parameters. If you run the same job again with the same identifying parameters and the previous instance completed successfully, Spring Batch throws JobInstanceAlreadyCompleteException.
That is why a timestamp, run id, or business date is usually added.
For example, this is risky for recurring scheduling:
Those parameters never change, so after the first successful execution, later scheduled runs are duplicates from Spring Batch's perspective.
A timestamp solves that for generic periodic jobs. A business date is often better when reruns need to target a specific input window explicitly.
A Minimal Job Configuration
Here is a simple job with one tasklet step:
With this in place, the scheduler can launch the job repeatedly.
Cron Versus Fixed Delay
Use cron when the job must align with wall-clock time, such as every day at 02:00. Use fixedDelay or fixedRate when you care about elapsed intervals.
Examples:
For business batch jobs, cron is more common because the schedule is usually tied to reporting windows or data arrival times.
Prevent Overlapping Runs
A scheduled job can start while the previous run is still executing. That may be acceptable, or it may corrupt processing semantics.
Common ways to avoid overlap are:
- check the
JobExplorerfor running executions before launching - use a distributed lock if multiple application instances can schedule the same job
- offload scheduling to Quartz or an external orchestrator for clustered deployments
A simple in-process scheduler is fine for one application instance. For multiple nodes, you need coordination.
When Quartz Or An External Scheduler Is Better
@Scheduled is lightweight and usually enough for one service instance. If you need persistence, clustering, missed-trigger recovery, or calendar-style scheduling rules, Quartz is a stronger choice.
In some systems, the cleanest design is to let an external scheduler such as Kubernetes CronJob, Airflow, or a platform scheduler call an endpoint or launch the application at the right time. That keeps scheduling concerns outside the batch service.
Common Pitfalls
- Reusing the same
JobParametersfor every scheduled run. - Forgetting that
@Scheduledruns in every application instance unless you add coordination. - Allowing a new run to start before the previous run finishes.
- Treating Spring Batch as if it included scheduling automatically.
- Using fixed-rate scheduling when the real requirement is a wall-clock cron schedule.
Summary
- Schedule a Spring Batch job by combining
@ScheduledwithJobLauncher. - Always pass unique identifying parameters for recurring executions.
- Use cron for calendar-based schedules and fixed delay for interval-based schedules.
- Prevent overlapping runs when job duration can exceed the schedule interval.
- For clustered or highly managed scheduling, consider Quartz or an external scheduler.
Related reading
- How to turn off debug log messages in spring boot
- How to turn off output from shutdown hooks in gradle boot tests?
- How to turn off the Eclipse code formatter for certain sections of Java code?
- How to understand when shedlock was acquired and released?
- How to unit test a Spring Boot MongoRepository?
- How to update a value, given a key in a hashmap?
- How to update Gradle in Android Studio?
- How to update Truststore dynamically?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.