C H A P T E R 6
Running a Job
Normally, you don't have to think about how to run an application in Java. If you have a web application,
you run it in some form of container. To run the application, you start the container, which starts the
application. If you want to run a stand-alone Java program, you either create an executable jar file or call
the class directly. In either case you might write a shell script to launch the process.
However, running a batch job is different. This is partially because a batch job can be run either as a
thread within an existing process (as it has been up to now) or within the main execution thread. It can
be run within a container or as a stand-alone process. You can start a JVM with each execution, or you
can have a JVM loaded and call into it via something like JMX to launch the job (as you did in Chapter 5).
You also have the consideration of what should happen when things go wrong and your job stops.
Does the entire job need to be rerun, or can you start at the step where it left off? If the step is processing
a million rows, do they all need to be reprocessed, or can you restart at the chunk where the error
With all this to think about when running a batch job, this chapter covers how to start a job in a
variety of environments. It discusses the different job runners provided with the Spring Batch
framework, as well as integrating the starting and running of jobs with a container like Tomcat and a
scheduler like Quartz.
Running a job isn't all you learn about here. You also see how to programmatically stop a job once it
has begun in a way that allows it to be restarted. Finally, you finish this chapter by seeing what it takes to
be able to restart a job.
Starting a Job
In the chapters up to now, you've almost exclusively run a job each time you start a JVM. However, when
you execute a job as you have been with SimpleJobLauncher , things are a little more complex than meets
the eye. This section looks at what happens when you launch a job via the SimpleJobLauncher . You then
take a detailed look at all the job runners and launchers Spring Batch provides. You see how to execute
jobs in a variety of environments, including from within a servlet container, using the Spring Batch
Admin administration application and via the open source scheduler Quartz.
When you think about launching a batch job in Spring Batch, you may think that what is happening is
Spring Batch executing the job as part of the main execution thread. When it finishes, the process ends.
However, it isn't that simple. The org.springframework.batch.core.launch.JobLauncher interface, which
is responsible for the work of starting a job, can be implemented in a number of ways, exposing any
number of execution options (web, JMX, command line, and so on).