I have created the fileAllocationTasklet whose purpose is to move file from one path to other. This file take input as fromPath and To Path. So I am trying to use this same tasklet in two step with making new object. I know the tasklet run only one time.
@Bean
public Step step3() {
System.out.println("******step3 executing ");
return stepBuilderFactory.get("step3")
.tasklet(fileAllocationTasklet("process/data.csv","output/data.csv")).build();
}
@Bean
public Step step1(JdbcBatchItemWriter<TxnDetail> writer) {
return stepBuilderFactory.get("step1")
.tasklet(fileAllocationTasklet("initial/data.csv","process/data.csv")).build();
}
@Bean
public Tasklet fileAllocationTasklet(String fromPath,String toPath) {
FileAllocationTasklet fileAllocation = new FileAllocationTasklet();
fileAllocation.setFromPath(fromPath);
fileAllocation.setToPath(toPath);
return fileAllocation;
}
But the tasklet is running only first time in step1 but not running in step 3. I made this so that i don't have redundancy in code. If there is other best approach then it will be appreciable.
Actually the answer of this I have used @StepScope which means the object will be unique for each step but not singleton. Each time step execute then it will form new object of tasklet which was prevously not forming due to @Bean. The explanation is also available in https://stackoverflow.com/questions/38780796/how-does-spring-batch-step-scope-work?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa