The Job configuration is an important part of your batch application, but it needs to be started to do anything meaningful. A Job can be started in a variety of ways: most commonly programmatically, manually from the command line, through scripts, or even from an HTTP controller in a web container.
JobLauncher
At the most basic level, Spring Batch provides the JobLauncher interface with a run method as a way to programmatically launch a Job. The run method accepts a Job instance representing the Job to launch and the JobParameters that are used to define the instance of the Job that is to be launched. The implementation of the JobLauncher provided by Spring Batch is the SimpleJobLauncher and by default a Job is launched synchronously. With your Job configuration ready to go, a programmatic invocation of the Job via a simple main method could look something like:
public class JobRunner {
private static final String CONFIG
= "classpath:launch-context.xml";
public static void main(final String[] args) throws Exception {
ApplicationContext ctx = new
ClassPathXmlApplicationContext(CONFIG);
Job job = ctx.getBean("personJob", Job.class);
JobLauncher jobLauncher =
ctx.getBean("jobLauncher", JobLauncher.class);
JobParameters jobParameters = new JobParametersBuilder()
.addString("lastName","Smith")
.toJobParameters();
jobLauncher.run(job, jobParameters);
}
}
Command line execution
Jobs can be launched manually via the command line or kicked off via a scheduler of your choice such as a Cron, Quartz, etc. Spring Batch does not provide nor dictate any specific scheduler. Out of the box, Spring Batch provides a CommandLineJobRunner class with a main method that can be executed from a script or manually on the command line. The CommandLineJobRunner arguments are as follows:
Argument |
Purpose |
jobPath |
The required XML file location that will be used to create the Spring ApplicationContext and all collaborating configuration required to run the Job. |
jobName |
The required name of the Job to run (typically the ID attribute on the Job configuration you would like to execute). |
jobParameters |
Any trailing arguments will be considered as JobParameters. JobParameters are specified in a key/value format. Values of date, string, long or double can be appended to the key to indicate the key’s value type. For example to indicate that the value of the run.date key is a Date, use: run.date(date)=2013/05/01. |
It is also worth noting that the CommandLineJobRunner provides support for optionally passing Job control options.
Argument |
Purpose |
-restart |
Optional flag to restart the last failed execution. |
-stop |
Optional flag to stop a running execution. |
-abandon |
Optional flag to mark a stopped execution as abandoned. |
-next |
Optional flag to start a Job using the configuredJobParametersIncrementer. |
jobPath |
The required XML file location that will be used to create the Spring ApplicationContext with the Job definition and related configuration. |
jobName |
The required name of the Job to run. |
job
Parameters |
Any trailing arguments will be considered as JobParameters. JobParameters are specified in a key/value format. Values of date, string, long or double can be appended to the key
to indicate the key's value type. For example to indicate that the value of the run.date key is a Date, use: run. date(date)=2013/05/01. |
As of Spring Batch 2.2, JobParameter's can now be optionally configured to contribute to the identity of a JobInstance. Previously, all JobParameters contributed to its identity. Prefixing a key with a hyphen will tell the framework not to consider this parameter as identifying.
Execution in a Web Container
Jobs can also be started within a web container using the same programmatic method of calling the run method of the JobLauncher. A simple SpringMVC controller used to launch a Job could look like the following:
public class JobController {
private Job job;
private JobLauncher jobLauncher;
public JobController(Job job, JobLauncher jobLauncher) {
this.job = job;
this.jobLauncher = jobLauncher;
}
@RequestMapping("/runJob")
public void runJob() throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("lastName", "Smith")
.toJobParameters();
jobLauncher.run(job, jobParameters);
}
}
One key difference when running in a web container is that launching a Job should most likely be executed in an asynchronous fashion to avoid having the HTTP request wait until the batch job is complete. This can be accomplished by providing a TaskExecutor implementation of your choice to the configured JobLauncher bean that will be used to execute the job.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}