Saturday, October 26, 2013

Getting started with Spring Batch 2.0

Spring batch 2.0 simpilified and improvised the batching framework. We have so many frameworks for MVC, Web, JDBC etc but batching frameworks are very rare. Spring batch is a lightweight and robust batch framework to process these big data sets. Spring offer Tasklet oriented and Chunk Oriented processing. In this post, we will see Simple Tasklet oriented processing.

Key Concepts:
  • Job - Job is a sequence of steps, each has an exit status. Execution of the next step depends on the exit status of previous step.
  • JobRepository - An interface which contains the meta data and corresponding entities of the Job. 
  • JobLauncer - Which launches a job by exposing the method to run.
  • Tasklet - An interface, can be instance of job which exposes a method called execute and returns the execution status. A tasklet will execute repeatedly until it returns FINSIHED.
Step to define (A simple job):
  • Define one job repository
  • Define one job launcher using the job repository.
  • Define Job(s) under the job launcher. Multiple jobs can be defined under one job launcher.
  • Create steps under the job. We can add multiple steps under the job with relations. By default, job launcher executes the job based on the steps defined. We can create dependency between each step. Literally, each step is a java class (Either Tasklet - a simple task or a combination of reader, writer and executiors for chunk oriented processing).
The below example shows how to define a job with single task (or step) using Tasklet.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="" xmlns:beans="" xmlns:xsi="" xsi:schemaLocation="">

  <beans:bean id="transactionManager"
    class="" />
  <beans:bean id="jobRepository"
    <beans:property name="transactionManager" ref="transactionManager" />

  <beans:bean id="jobLauncher"
      <beans:property name="jobRepository" ref="jobRepository" />

  <job id="sampleJob" job-repository="jobRepository">
    <step id="step1">
       <tasklet ref="sampleTasklet" />

  <beans:bean name="sampleTasklet" class="com.test.springbatch.SampleTasklet" />
The SampleTasklet class should be a type of Tasklet, which provides one execute. The execute method returns RepeatStatus to know the status of the task. The tasks executes multiple times if it returns CONTINUABLE otherwise stops after execution.
package com.test.springbatch;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class SampleTasklet implements Tasklet
    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception
        System.out.println("Do Something; ");
        // Return RepeatStatus.CONTINUABLE if something goes wrong so that it
        // repeats; Otherwise Return FINISHED to complete
        return RepeatStatus.FINISHED;

How to run the job:
Spring provides you with CommandLineJobRunner which runs the job using the two parameters. One the spring context file and the other is the batch name.
CommandLineJobRunner.main(new String[] { "spring-batch.xml", "sampleJob" });
In the next post, we will see "Chunk Oriented processing with Spring Batch"
Happy Learning !!!!