I am very new to the spring batch. Trying to create a simple example for myself to understand some of the basic batch-processing concepts. Currently, my spring application starts but unable to execute a simple job which is as follows
- Read from the Student.json file
- Dump it into the database
Adding the GitHub repo link below the code snippets. Do feel free to suggest any other improvements if you can. Thank you :)
public ItemReader<Student> jsonItemReader() {
return new JsonItemReaderBuilder<Student>()
.jsonObjectReader(new JacksonJsonObjectReader<>(Student.class))
.resource(new ClassPathResource("Students.json"))
.name("studentJsonItemReader")
.saveState(true)
.build();
}
public Step sampleStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("studentProcessor", jobRepository)
.<Student, Student>chunk(10, transactionManager)
.reader(jsonItemReader())
.writer(itemWriter())
.processor(studentProcessor)
.build();
}
@Bean
public Job sampleJob(JobRepository jobRepository, Step sampleJob) {
return new JobBuilder("sampleJob", jobRepository)
.start(sampleJob)
.build();
}
@Bean
public ItemWriter<Student> itemWriter() {
return new StudentWriter();
}
public class StudentWriter implements ItemWriter<Student> {
@Autowired
private StudentRespository studentRespository;
@Override
public void write(Chunk<? extends Student> chunk) throws Exception {
studentRespository.saveAll(chunk);
}
}
Github link : https://github.com/mohasink24/spring-batch