Spring Book – Chapter 21 – Spring Batch

Job

A Job is an entity that encapsulates an entire batch process and exists in the top of an overall hierarchy in Spring Batch. It resides in the package org.springframework.batch.core of Spring Batch project. A Job is wired together via an XML configuration file referred to as the “job configuration” file. A Job acts as a container holding the various steps inside.

The typical job configuration file contains:

  • Name of the job
  • Definition and ordering of Steps
  • Whether or not the job is capable of “restart”

Spring Batch provides default simple implementation of the Job interface in the form of the org.springframework.batch.core.job.SimpleJob class. Listing 23-1 below shows declaration of a simple job in the so called Spring configuration file.

Listing 23-1. Declaration of a simple job in the Spring configuration file

There are three instances which constitutes a typical Job. They are:

  • org.springframework.batch.core.JobInstance
  • org.springframework.batch.core.JobParameter
  • org.springframework.batch.core.JobExecution

The relationship between job and these instances is pictorially represented as shown in Figure 23-5 below.

 Figure 23-5. Instances constituting a typical Job

Figure 23-5. Instances constituting a typical Job

A Job constitutes more than one JobInstance. Each JobInstance can have more than one JobExecution and has one JobParameter as shown in Figure 23-2 above. We will now see each of these instances in detail in the following section.

JobInstance

A JobInstance refers to the concept of a logical job run and can only be executed once to a successful completion. Since a JobInstance is identified by the job name and parameters passed in, you can only run a job once with the same parameters. If you run the same job more than once with the same parameters you receive an org.springframework.batch.core.launch.JobInstanceAlreadyCompleteException telling you that if you’d like to run the job again, you need to change the parameters passed to it in the JobInstance.

JobParameters

JobParameter is unique to each JobInstance. Spring Batch allows you not only to pass parameters to your jobs but also to automatically increment them5 or validate them before your job runs. JobParameters isn’t much more than a wrapper for a java.util.Map<String, JobParameter> object. It is a good idea to validate JobParameters passed onto the JobInstance. Fortunately, Spring makes it very easy to validate job parameters by implementing the org.springframework.batch.core.JobParametersValidator interface as shown in Listing 23-2 below in which JobParametersValidation implementation class checks if the passed in parameters are alphabets.

Listing 23-2. Sample JobParametersValidator implementation

JobExecution

A JobExecution is information about a single run of the job. Each JobInstance would be considered complete when it has an attempt or JobExecution that has successfully completed. If a job runs from start to finish the first time, there is only one JobExecution related to a given JobInstance. If a job ends in an error state after the first run, a new JobExecution is created each time an attempt is made to run the JobInstance by passing in the same parameters to the same job.

Page Visitors: 25939

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 21 – Spring Batch

  1. Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?

  2. Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.

    Thanks
    Laxmi

Leave a Reply

Your email address will not be published. Required fields are marked *