This is continuation to my previous post. In my previous application, we created a step, job within the Main class itself. Let’s create a configuration class and move everything to the configuration class.
Create a package ‘com.sample.app.configuration’ and define JobConfiguration class like below.
JobConfiguration.java
package com.sample.app.configuration;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class JobConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
private static int count = 0;
@Bean
public Step step() {
return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
System.out.println(count + ". Hello, World!");
count++;
if (count < 5) {
return RepeatStatus.CONTINUABLE;
} else {
return RepeatStatus.FINISHED;
}
}
}).build();
}
@Bean
public Job job() {
return this.jobBuilderFactory.get("job").start(step()).build();
}
}
Define App.java like below.
App.java
package com.sample.app;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableBatchProcessing
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Run App.java, you will see below messages in console.
2020-03-28 16:09:20.153 INFO 70179 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] launched with the following parameters: [{}] 2020-03-28 16:09:20.176 INFO 70179 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1] 0. Hello, World! 1. Hello, World! 2. Hello, World! 3. Hello, World! 4. Hello, World! 2020-03-28 16:09:20.201 INFO 70179 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] completed with the following parameters: [{}] and the following status: [COMPLETED]
You can download complete working application from this link.
https://github.com/harikrishna553/springboot/tree/master/batch/spring-batch-hello-world-enhancement
Previous Next Home
No comments:
Post a Comment