The JobLauncher interface is the entry point for starting a Spring Batch job. Its single method, run(Job, JobParameters), validates the parameters, asks the JobRepository to create a JobExecution and then executes the job, either on the calling thread or on a separate one depending on its TaskExecutor.
Whenever a job is started from a REST endpoint, a scheduler, a command line runner or a test, a JobLauncher is what does the actual launching.
The interface is deliberately small:
On each call the default implementation, TaskExecutorJobLauncher, does the following:
JobRepository for an existing JobInstance with the same identifying parameters.JobParametersValidator.JobExecution and runs the job.TaskExecutorJobLauncher replaced the older SimpleJobLauncher in Spring Batch 5, which is deprecated. With Spring Boot you get one as an auto-configured bean.
By default the launcher uses a SyncTaskExecutor, so run blocks until the job ends and returns a finished JobExecution. That suits command line jobs. For a web request you normally want to return immediately, so configure an asynchronous executor.
With this launcher, run returns a JobExecution in STARTING or STARTED state and the caller polls the repository for the final status.
Parameters identify a run. Two launches with the same identifying parameters refer to the same JobInstance, and a completed instance cannot be run again. A common approach is to add a changing parameter such as a timestamp.
The endpoint returns 202 Accepted with the execution ID so the client can query progress later. A non-identifying parameter can be added with addString("note", "x", false), so it does not affect the job instance identity.
| Exception | Meaning |
|---|---|
JobExecutionAlreadyRunningException | An execution of this instance is still running |
JobInstanceAlreadyCompleteException | This instance already finished successfully |
JobRestartException | The job cannot be restarted, for example not restartable |
JobParametersInvalidException | Parameters failed validation |
Catch these and map them to useful HTTP responses, for example 409 Conflict for the first two and 400 Bad Request for the last.
Spring Boot runs all Job beans at startup through JobLauncherApplicationRunner. Turn that off when jobs should only run on demand:
To run a single job at startup, set spring.batch.job.name=importJob. For scheduled runs, call the launcher from a method annotated with @Scheduled. If you need to stop, restart or abandon executions, use JobOperator, which builds on the launcher. Newer Spring Batch 6 releases put more launching responsibility on JobOperator and deprecate JobLauncher, so check the reference documentation for the version you use before building new code around it.
Spring Batch treats identical identifying parameters as the same JobInstance, and a completed instance cannot be re-run. Add a unique identifying parameter, such as a timestamp or run ID.
Give the TaskExecutorJobLauncher an asynchronous TaskExecutor such as SimpleAsyncTaskExecutor. The call then returns right away while the job runs in another thread.
Spring Boot auto-configures one when Spring Batch is on the classpath. Inject it by type, or define your own with a qualifier if you need the asynchronous variant.
Yes. Use JobLauncherTestUtils from spring-batch-test, which wraps a launcher and lets you call launchJob() or launchStep("name").
JobLauncher starts jobs: it validates parameters, creates the JobExecution through the JobRepository and runs the job synchronously or asynchronously. Use TaskExecutorJobLauncher, add a unique parameter for repeatable runs, handle its exceptions explicitly, and disable automatic startup when jobs should only run on demand.