Spring batch - Neethahiremath/Wiki GitHub Wiki
refer:
https://spring.io/guides/gs/batch-processing/
https://www.journaldev.com/17157/spring-batch-example
Steps to create a spring batch project:
create a reader implements ItemReader
@Service
@Slf4j
@StepScope
public class Reader implements ItemReader<List<Entity>>{
@Override
public List<Entity> read() throws Exception {
log.debug("in reader");
return new ArrayList<>();
}
}
create a writer
@Service
@Slf4j
@StepScope
public class Writer implements ItemWriter<List<Response>> {
@Override
public void write(List<Entity> entity) throws Exception {
// code where to write
}
}
write a processor service and create a instance of it (Bean)
@Service
@Slf4j
public class Processor implements ItemProcessor<List<Entity>, List<Response>> {
@Override
public List<Response> process(List<Entity> entity) {
}
}
set up listeners for reader, writer, step and job execution: ex:
@Service
@Slf4j
public class BatchJobListener implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
log.info("Batch job start time {} " ,jobExecution.getStartTime());
}
@Override
public void afterJob(JobExecution jobExecution) {
log.info("Batch job end time {} " ,jobExecution.getEndTime());
}
}
configure the steps and job:
@Bean(name = "BatchJob")
public Job BatchJob() {
return jobBuilderFactory
.get("BatchJob")
.start(step1())
.listener(BatchJobListener)
.build();
}
@Bean
public Processor processor() {
return new Processor();
}
@Bean
protected Step step1() {
return stepBuilderFactory.get("step1").<List<Entity>, List<Response>>chunk(1)
.reader(reader).listener(Listener)
.processor(processor())
.writer(writer).listener(writerListener)
.taskExecutor(ayncTaskExecutor()).throttleLimit(concurrencyLimit)
.listener(BatchStepListener)
.build();
}
@Bean
public TaskExecutor ayncTaskExecutor() {
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
taskExecutor.setConcurrencyLimit(concurrencyLimit);
return taskExecutor;
}
@EnableBatchProcessing in application and launch the job
public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException,
JobParametersInvalidException {
applicationContext = SpringApplication.run(Application.class, args);
log.info("configurations are loaded ");
try {
job(JOB_NAME);
}catch(JobInstanceAlreadyCompleteException ex) {
log.info("Job completed,{}, {}",ex.getMessage(),ex);
}
log.info("Shutting down the batch Job");
applicationContext.close();
}
private static JobExecution job(String jobName) throws JobExecutionAlreadyRunningException,
JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
JobLauncher jobLauncher = (JobLauncher) applicationContext.getBean("jobLauncher");
Job job = (Job) applicationContext.getBean(jobName);
JobExecution jobExecution = jobLauncher.run(job, new JobParametersBuilder().addLong("JobTime",System.currentTimeMillis()).toJobParameters());
log.info("Batch job with jobName {} . Execution status {}", jobName,
jobExecution.getExitStatus().getExitDescription());
return jobExecution;
}
@Slf4j
@Component
public class Scheduler {
private final Job batch;
private final JobLauncher jobLauncher;
@Autowired
public Scheduler(Job batch, JobLauncher jobLauncher) {
this.batch = batch;
this.jobLauncher = jobLauncher;
}
@Scheduled(cron = "0 */15 * * * *", zone = "est")
public JobExecution launchJob()
throws JobExecutionAlreadyRunningException, JobRestartException,
JobInstanceAlreadyCompleteException, JobParametersInvalidException {
JobParameters jobTime =
new JobParametersBuilder()
.addString("jobName", JOB_NAME)
.addLong("JobTime", System.currentTimeMillis())
.toJobParameters();
JobExecution jobExecution = jobLauncher.run(batch, jobTime);
log.info(
" job ran with jobName {} . Execution status {}",
JOB_NAME,
jobExecution.getExitStatus().getExitDescription());
return jobExecution;
}
}
tables:
- delete from batch_job_execution where job_execution_id =20;
- delete from batch_step_execution where job_execution_id =20;
- delete from batch_step_execution_context where step_execution_id =19;
- delete from batch_job_execution_context where job_execution_id =20;