Figure 4-2. Flow of data through a batch process
As you can see in Figure 4-2, a batch process is executed with all of the input available for it as it
runs. There are no user interactions. Each step is executed to completion against a dataset before the
next step is executed. Before you dig deeply into how to configure the various features of a job in Spring
Batch, let's talk about a job's execution lifecycle.
Tracing a Job's Lifecycle
When a job is executed, it goes through a lifecycle. Knowledge of this lifecycle is important as you
structure your jobs and understand what is happening as they run. When you define a job in XML, what
you're really doing is providing the blueprint for a job. Just like writing the code for a Java class is like
defining a blueprint for the JVM from which to create an instance, your XML definition of a job is a
blueprint for Spring Batch to create an instance of your job.
The execution of a job begins with a job runner. The job runner is intended to execute the job
requested by name with the parameters passed. Spring Batch provides two job runners:
CommandLineJobRunner : This job runner is intended to be used from a script or
directly from the command line. When used, the CommandLineJobRunner bootstraps
Spring and executes the job requested with the parameters passed.
• JobRegistryBackgroundJobRunner : When using a scheduler like Quartz or a JMX
hook to execute a job, typically Spring is bootstrapped and the Java process is live
before the job is to be executed. In this case, a JobRegistry is created when Spring
is bootstrapped containing the jobs available to run. The
JobRegistryBackgroundJobRunner is used to create the JobRegistry .
CommandLineJobRunner and JobRegistryBackgroundJobRunner (both located in the
org.springframework.batch.core.launch.support package) are the two job runners provided by the
framework. You used CommandLineJobRunner in Chapter 2 to run the “Hello, World!” job, and you
continue to use it through out the topic.
Although the job runner is what you use to interface with Spring Batch, it's not a standard piece of
the framework. There is no JobRunner interface because each scenario would require a different
implementation (although both of the two job runners provided by Spring Batch use main methods to
start). Instead, the true entrance into the framework's execute is an implementation of the
Spring Batch provides a single JobLauncher , the
org.springframework.batch.core.launch.support.SimpleJobLauncher . This class uses the TaskExecutor
interface from Core Spring to execute the requested job. You see in a bit at how this is configured, but it's
important to note that there are multiple ways to configure the
org.springframework.core.task.TaskExecutor in Spring. If an
org.springframwork.core.task.SyncTaskExecutor is used, the job is executed in the same thread as the
JobLauncher . Any other option executes the job in its own thread.