Create Batch to Derive Data (Chunk Step)

This section explains the Chunk step batch architecture, which calculates from existing data and derives new data based on the Example application.

Overview of the function to be created
Communication confirmation procedure
  1. Delete data in the table (bonus table) to be registered

    Execute the following SQL from the console of H2 and delete the data in the bonus table.

  2. Execute bonus calculation batch

    Execute the bonus calculation batch from the command prompt.

$cd {nablarch-example-batch-ee system repository}
$mvn exec:java -Dexec.mainClass=nablarch.fw.batch.ee.Main ^
  1. Check the status after batch execution

Execute the following SQL from the console of H2 and confirm that the bonus information is registered.

SELECT * FROM BONUS; Deriving data

A method to implement a batch that calculates from existing data and derives new data will be described in the following order.

  1. Reads data from the input data source
  2. Execute business logic
  3. Persistence process
  4. Create a configuration file for JOB

For the process flow, see process flow of Chunk step batch. For responsibility assignment, see responsibility assignment of the Chunk step.

Batch process is configured by a listener that provides common processes such as transaction control in addition to the implementation of the interface specified in JSR352 (external site). For details of the listener, see listener used in the batch application, and how to specify the listener. Reads data from the input data source

Implements the process to fetch the data required for calculation.

  1. Create a form
  2. Create an ItemReader
Create a form

In the Chunk step, use form to link data with ItemReader and ItemProcessor.

public class EmployeeForm {

    //Partial excerpt

    /** Employee ID */
    private Long employeeId;

     *Returns employee ID.
     * @return Employee ID
    public Long getEmployeeId() {
        return employeeId;

     * Sets the employee ID.
     * @param employeeId Employee ID
    public void setEmployeeId(Long employeeId) {
        this.employeeId = employeeId;
Create an ItemReader

Inherits AbstractItemReader and reads data.

Interface Name Obligation

Reads data.

Inherits AbstractItemReader, which provides a empty implementation.

  • ItemReader#open
  • ItemReader#readItem
  • ItemReader#close
public class EmployeeSearchReader extends AbstractItemReader {

    /** List of employee information */
    private DeferredEntityList<EmployeeForm> list;

    /** Iterator holding employee information */
    private Iterator<EmployeeForm> iterator;

    public void open(Serializable checkpoint) throws Exception {
        list = (DeferredEntityList<EmployeeForm>) UniversalDao.defer()
                .findAllBySqlFile(EmployeeForm.class, "SELECT_EMPLOYEE");
        iterator = list.iterator();

    public Object readItem() {
        if (iterator.hasNext()) {
            return iterator.next();
        return null;

    public void close() throws Exception {
Key points of this implementation Execute business logic

Implements the business logic of bonus calculation.

Create ItemProcessor

Implements ItemProcessor and carries out the business logic (since the persistence process is a duty of ItemWriter, it is not executed).

Interface Name Obligation

Performs the business process on one line of data.

  • ItemProcessor#processItem
public class BonusCalculateProcessor implements ItemProcessor {

    public Object processItem(Object item) {

        EmployeeForm form = (EmployeeForm) item;
        Bonus bonus = new Bonus();

        return bonus;

     * Calculate bonus based on employee information.
     * @param form Employee Information Form
     * @return Bonus
    private static Long calculateBonus(EmployeeForm form) {
        if (form.getFixedBonus() == null) {
            return form.getBasicSalary() * form.getBonusMagnification() / 100;
        } else {
            return form.getFixedBonus();
Key points of this implementation Persistence process

Implements the persistence process for DB update, etc.

Create ItemWriter

Implements ItemWriter and makes data persistence.

Interface Name Obligation

Persistence of data

  • ItemWriter#writeItems
public class BonusWriter extends AbstractItemWriter {

    public void writeItems(List<Object> items) {
Key points of this implementation
  • Uses UniversalDao#batchInsert to batch register entity list.
  • The transaction is committed after execution of the writeItems method and a new transaction is started.
  • After execution of the writeItems method, the batch process is repeated from the execution of readItem method. Create a configuration file for JOB

Create a file with the job execution configuration.

<job id="bonus-calculate" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
    <listener ref="nablarchJobListenerExecutor" />

  <step id="step1">
      <listener ref="nablarchStepListenerExecutor" />
      <listener ref="nablarchItemWriteListenerExecutor" />

    <chunk item-count="1000">
      <reader ref="employeeSearchReader" />
      <processor ref="bonusCalculateProcessor" />
      <writer ref="bonusWriter" />
Key points of this implementation
  • The job definition file is located under /src/main/resources/META-INF/batch-jobs/.
  • Specify the job name in the id attribute of the job element.
  • Configure the number of writeItems processed each time by the item-count attribute of the chunk element.
  • Refer to JSR352 specification (external site) for detailed description method of the configuration file.