Class CommandLineJobRunner

  extended by

public class CommandLineJobRunner
extends Object

Basic launcher for starting jobs from the command line. In general, it is assumed that this launcher will primarily be used to start a job via a script from an Enterprise Scheduler. Therefore, exit codes are mapped to integers so that schedulers can use the returned values to determine the next course of action. The returned values can also be useful to operations teams in determining what should happen upon failure. For example, a returned code of 5 might mean that some resource wasn't available and the job should be restarted. However, a code of 10 might mean that something critical has happened and the issue should be escalated.

With any launch of a batch job within Spring Batch, a Spring context containing the Job and the 'Execution Environment' has to be created. This command line launcher can be used to load that context from a single location. It can also load the job as well All dependencies of the launcher will then be satisfied by autowiring by type from the combined application context. Default values are provided for all fields except the JobLauncher and JobLocator. Therefore, if autowiring fails to set it (it should be noted that dependency checking is disabled because most of the fields have default values and thus don't require dependencies to be fulfilled via autowiring) then an exception will be thrown. It should also be noted that even if an exception is thrown by this class, it will be mapped to an integer and returned.

Notice a property is available to set the SystemExiter. This class is used to exit from the main method, rather than calling System.exit() directly. This is because unit testing a class the calls System.exit() is impossible without kicking off the test within a new Jvm, which it is possible to do, however it is a complex solution, much more so than strategizing the exiter.

The arguments to this class are roughly as follows:

java jobPath jobName jobLauncherPath jobParameters...

The combined application context must only contain one instance of a JobLauncher. The job parameters passed in to the command line will be converted to Properties by assuming that each individual element is one parameter that is separated by an equals sign. For example, "". Below is an example arguments list: "

java testJob.xml testJob

Once arguments have been successfully parsed, autowiring will be used to set various dependencies. The for example, will be loaded this way. If none is contained in the bean factory (it searches by type) then a BeanDefinitionStoreException will be thrown. The same exception will also be thrown if there is more than one present. Assuming the JobLauncher has been set correctly, the jobName argument will be used to obtain an actual Job. If a JobLocator has been set, then it will be used, if not the beanFactory will be asked, using the jobName as the bean id.

Dave Syer, Lucas Ward

Field Summary
protected static org.apache.commons.logging.Log logger
Constructor Summary
Method Summary
 void exit(int status)
          Delegate to the exiter to (possibly) exit the VM gracefully.
static void main(String[] args)
          Launch a batch job using a CommandLineJobRunner.
 void setExitCodeMapper(ExitCodeMapper exitCodeMapper)
          Injection setter for the ExitCodeMapper.
 void setJobLocator(JobLocator jobLocator)
 void setLauncher(JobLauncher launcher)
          Injection setter for the JobLauncher.
 void setSystemExiter(SystemExiter systemExitor)
          Injection setter for the SystemExiter.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Detail


protected static final org.apache.commons.logging.Log logger
Constructor Detail


public CommandLineJobRunner()
Method Detail


public void setLauncher(JobLauncher launcher)
Injection setter for the JobLauncher.

launcher - the launcher to set


public void setExitCodeMapper(ExitCodeMapper exitCodeMapper)
Injection setter for the ExitCodeMapper.

exitCodeMapper - the exitCodeMapper to set


public void setSystemExiter(SystemExiter systemExitor)
Injection setter for the SystemExiter.

systemExitor -


public void exit(int status)
Delegate to the exiter to (possibly) exit the VM gracefully.

status -


public void setJobLocator(JobLocator jobLocator)


public static void main(String[] args)
Launch a batch job using a CommandLineJobRunner. Creates a new Spring context for the job execution, and uses a common parent for all such contexts. No exception are thrown from this method, rather exceptions are logged and an integer returned through the exit status in a JvmSystemExiter (which can be overridden by defining one in the Spring context).

args -

  • jobPath: the xml application context containing a Job
  • jobName: the bean id of the job.
  • jobLauncherPath: the xml application context containing a JobLauncher
  • jobParameters: 0 to many parameters that will be used to launch a job.

Copyright © 2008 SpringSource. All Rights Reserved.