run spring batch job from the controller
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
You can start a Spring Batch job from a web controller, but you should do it in a controlled way. The controller should trigger the job and return a clear execution result, not contain the batch logic itself.
How Spring Batch Launching Works
Spring Batch separates job definition from job execution. A Job describes the steps, and a JobLauncher starts it with a set of JobParameters. Those parameters matter because Spring Batch uses them to identify job instances.
A minimal controller-based launch flow looks like this:
- inject
JobLauncher - inject the
Jobyou want to run - build unique parameters
- call
jobLauncher.run(job, params) - return the execution id or status
Example Configuration
This example defines a small job that logs a message. In a real application, the step would read, process, and write records.
Launching From A REST Controller
The controller should create unique parameters so repeated requests do not collide with an already completed job instance.
Using System.currentTimeMillis() makes each request produce a new job instance. If you need idempotent behavior, use business keys instead and decide how reruns should work.
Synchronous Versus Asynchronous Launching
By default, the launcher may execute the job on the request thread, depending on configuration. That is acceptable for very small jobs, but long-running jobs should usually run asynchronously. Otherwise the HTTP request may time out while the batch is still working.
A common production pattern is:
- controller starts the job
- controller returns
202 Accepted - client polls a status endpoint
- job execution details come from
JobExploreror the metadata tables
This keeps the web layer responsive and avoids tying batch duration to request timeouts.
Tracking Status
If you want to expose execution status, query the job repository rather than storing ad hoc flags.
That approach stays aligned with how Spring Batch already models executions and restarts.
Common Pitfalls
The main mistake is launching the same job with the same parameters and expecting it to run again. Spring Batch treats that as the same job instance and may reject it once completed. Add a unique parameter when the goal is a fresh execution.
Another issue is putting heavy batch logic directly in the controller. The controller should only trigger work. Business logic belongs in the job, step, reader, processor, and writer components.
Be careful with long-running synchronous requests. If the job takes minutes, the browser or reverse proxy may give up before the batch ends. Prefer asynchronous execution and status tracking.
Finally, secure the endpoint. A batch trigger is an operational action, not a public API. Limit who can start jobs and consider rate limits or audit logging.
Summary
- Use a controller to trigger a Spring Batch
Job, not to implement the batch logic itself. - Start jobs through
JobLauncherand pass well-definedJobParameters. - Use unique parameters when each request should create a new job instance.
- Prefer asynchronous execution for long-running jobs and return
202 Accepted. - Expose status from Spring Batch metadata instead of inventing a parallel tracking system.
Related reading
- Runnable with a parameter?
- Running a MVC app using Spring Boot Hibernate MySql
- Running chron job on a single instance of spring service deployed on aws
- Running code after Spring Boot starts
- Running code after Spring Boot starts
- Running each Spring Scheduler in its own thread
- Running into an error after upgrading springboot from 2.3.9 to 2.6.3
- Running JAR file on Windows

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.