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.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

  <bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="transactionManager" />
  </bean>

  <bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
  </bean>
</beans>

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.

jobConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:batch="http://www.springframework.org/schema/batch"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-2.0.xsd">

  <import resource="applicationContext.xml" />

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

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

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

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

</beans>

The Tasklet Class

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

SimpleTasklet.java

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 {

  @Override
  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.

MyLauncher.java

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",
        System.currentTimeMillis()).toJobParameters();
    try {
      JobExecution execution = jobLauncher.run(job, jobParameters);
      System.out.println("Exit Status : " + execution.getStatus());
    } catch (Exception e) {
      System.out.println("Unable to execute job");
      e.printStackTrace();
    }
  }

  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;
  }
}
Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s