I am trying a make spring batch utility in which i will be reading files from a location and dump them into database. I am calling batch jobs via 2 ways, one from scheduling and another from rest controller.
My batch config is below
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public SomeReader<Some> reader() {
// some reader configuration
return reader;
}
@Bean
public SomeProcessor processor() {
return new SomeProcessor();
}
@Bean
public SomeWriter<Person> writer() {
// some config
return writer;
}
@Bean
public Job someJob() {
return jobBuilderFactory.get("someJob")
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Some, Some> chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.taskExecutor(new SimpleAsyncTaskExecutor())
.build();
}
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(3);
threadPoolTaskExecutor.setThreadNamePrefix("ImportPoolThread-Utility");
return threadPoolTaskExecutor;
}
}
my rest controller from where i call the job task
@RestController
public class JobInvokerController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@RequestMapping("/invokejob")
public String handle() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().toJobParameters();
jobLauncher.run(job, jobParameters);
return "Task Batch job has been invoked.";
}
}
spring.batch.job.enabled=false
my project start well. But job task of read/write only starts when i change my step name everytime i.e. step1 -->step2 or do the job name changes, this happens only when i call job launcher from rest end point. Project starts but wont call the job task no matter how many times you hit the rest end point. But once you change step name or job name and restart the project, it start working and the task of read and write starts. How to correct this, do i have to change job instance every time for calls from HTTP. For that i also have provided taskExecutor with custom thread pool. referred this documentation.
Don't know what i am doing wrong here, please assist.
CodePudding user response:
You are probably starting a job which already completed, hence it won't start. To start the same job again you can add a parameter with unique value -
Example:
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addLong("startTime", System.currentTimeMillis());
Then pass this as parameter to job.