I am trying to run multi step spring batch job with tasklet.Inside concreat implementation of tasklet trying to use other services with @Autowired . But not working and facing null pointer exception :
java.lang.NullPointerException: null at com.rml.api.poc.MyTaskTwo.getStackStatus(MyTaskTwo.java:52) at com.rml.api.poc.MyTaskTwo.execute(MyTaskTwo.java:41) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
Example code is given bellow :
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public Step stepOne() {
return steps.get("stepOne")
.tasklet(new MyTaskOne())
.build();
}
@Bean
public Step stepTwo() {
return steps.get("stepTwo")
.tasklet(new MyTaskTwo())
.build();
}
@Bean
public Step stepThree() {
return steps.get("stepThree")
.tasklet(new MyTaskThree())
.build();
}
@Bean
public Job helloJob() {
return jobs.get("createProject")
.start(stepOne())
.next(stepTwo())
.next(stepThree())
.build();
}
}
Tasklet classes :
public class MyTaskOne implements Tasklet {
@Autowired
HelloService helloService;
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
//custom code
return RepeatStatus.FINISHED;
}
}
public class MyTaskTwo implements Tasklet {
@Autowired
HelloService helloService;
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
//custom code
return RepeatStatus.FINISHED;
}
}
public class MyTaskThree implements Tasklet {
@Autowired
HelloService helloService;
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
//custom code
return RepeatStatus.FINISHED;
}
}
What i am missing here ?