Creating your SpringBatch Application - Part 1

Lets look at creating a very simple Spring Batch Application using Spring Boot.

You can download the entire example from: https://github.com/ricsr/spring-batch-demo/tree/exercise_01

Step1: Lets create our project.
Goto https://start.spring.io/, and select Spring Batch and H2 Database as dependencies.
Click on generate.
Note that I am using Gradle. You are free to use Maven for dependency management.

Step2: Import the project into you IDE. I am using IntelliJ in my examples.

Step3: Edit your main class. In my case its the SpringbootdemoApplication.class
package com.ricsr.springbootdemo;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableBatchProcessing
public class SpringbootdemoApplication {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Bean
public Job packageJob(){
return this.jobBuilderFactory.get("packageJob").start(packageStep()).build();
}

@Bean
public Step packageStep() {
return this.stepBuilderFactory.get("packageStep").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Inside the step");
return RepeatStatus.FINISHED;
}
}).build();
}

public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}

}

First, add the @EnableBatchProcessing annotation, which loads all the essential Beans at startup. Then Autowire JobBuilderFactory and StepBuilderFactory.

We use the method packageJob to define the Job Bean. We give it a simple name "packageJob". Method packageStep defines the Step Bean. We create a Tasklet and override the method execute. Everything in the method execute, will get executed until we return RepeatStatus of FINISHED. If we want to continue processing, we should return the RepeatStatus of CONTINUABLE. We use the Tasklet to process smaller chunks of data.

While defining Job and Step we have used the Builder Pattern - we will see this for a lot of beans we define while using SpringBatch.

Last Step: Lets run the application
JobLauncher is required to run the jobs. SpringBoot makes life easy. It will find the Job and execute the steps!

2020-07-15 00:12:01.771  INFO 17884 --- [           main] c.r.s.SpringbootdemoApplication          : Started SpringbootdemoApplication in 2.503 seconds (JVM running for 2.879)
2020-07-15 00:12:01.774  INFO 17884 --- [           main] o.s.b.a.b.JobLauncherApplicationRunner   : Running default command line with: []
2020-07-15 00:12:01.837  INFO 17884 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=packageJob]] launched with the following parameters: [{}]
2020-07-15 00:12:01.939  INFO 17884 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [packageStep]
Inside the step
2020-07-15 00:12:01.967  INFO 17884 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [packageStep] executed in 26ms
2020-07-15 00:12:01.979  INFO 17884 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=packageJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 66ms

As you can see the SpringBatch Job executed successfully.

You can download the entire example from: https://github.com/ricsr/spring-batch-demo/tree/exercise_01

Comments

Popular posts from this blog

Writing your own ejabberd Module

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6