Spring batch example with Spring’s built in scheduling support

Following is a simple example of Spring batch scheduled using the Spring’s built in scheduler.

The Application Context

In the application context file we need to define the :-

  • transactionManager
  • jobRepository
  • jobLauncher

Above are basic objects we would need while defining a batch job.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

  <bean id="transactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

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

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

The Job Configuration File

This file contains the configuration of a batch job and its steps. It also contains the scheduling configuration and batch launcher.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

  <import resource="applicationContext.xml" />

  <bean id="mytasklet" class="javadigest.springbatch.SimpleTasklet" />

  <batch:job id="simpleJob">
    <batch:step id="simpleTaskletStep">
      <batch:tasklet ref="mytasklet" />

  <bean id="myLauncher" class="javadigest.springbatch.MyLauncher">
    <property name="jobLauncher" ref="jobLauncher"></property>
    <property name="job" ref="simpleJob"></property>

    <task:scheduled ref="myLauncher" method="launch"
      fixed-delay="60000" />


The Tasklet Class

This is a simple tasklet prints “Hello World !” on the console.


package javadigest.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 SimpleTasklet implements Tasklet {

  public RepeatStatus execute(StepContribution paramStepContribution,
      ChunkContext paramChunkContext) throws Exception {
    System.out.println("Hello World!");
    return RepeatStatus.FINISHED;

The Launcher Class

This class is used launch the batch job using the Job, JobParameter, and the Job classes, and is scheduled using the spring task scheduler.


package javadigest.springbatch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;

class MyLauncher {

  private JobLauncher jobLauncher;

  private Job job;

  public void launch() {
    JobParameters jobParameters = new JobParametersBuilder().addLong("time",
    try {
      JobExecution execution = jobLauncher.run(job, jobParameters);
      System.out.println("Exit Status : " + execution.getStatus());
    } catch (Exception e) {
      System.out.println("Unable to execute job");

  public JobLauncher getJobLauncher() {
    return jobLauncher;

  public void setJobLauncher(JobLauncher jobLauncher) {
    this.jobLauncher = jobLauncher;

  public Job getJob() {
    return job;

  public void setJob(Job job) {
    this.job = job;

5 thoughts on “Spring batch example with Spring’s built in scheduling support

    • @Override annotation will not work if you are using java1.5; as Tasklet is an interface.
      You can remove the @Override annotation and the code should work fine.

      This is what you will see in the console, after every minute as job the is scheduled to run every minute;

      Hello World!
      Exit Status : COMPLETED

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s