I am trying to restart a job which was exited with the status code "FAILED" through a rest end point.
The job is scheduled at a particular time and it has failed for some reason.
If I try to restart the job via above rest API getting the error "No job configuration with the name [updateJob] was registered"
The execution Id passed to rest end point is extracted from the db(job repository)
I can see the job with the the name "updateJob" in the table "BATCH_JOB_EXECUTION". Still I am getting the error "No job configuration with the name [updateJob] was registered".
@Log4j2
@RestController
@RequestMapping("/jobs")
public class JobController {
private final JobOperator jobOperator;
private final JobExplorer jobExplorer;
public JobController(JobOperator jobOperator, JobExplorer jobExplorer) {
this.jobOperator = jobOperator;
this.jobExplorer = jobExplorer;
}
@GetMapping("/restart")
public ResponseEntity<String> restartJob(@RequestParam("id") Long id) {
try {
final Long restartId = jobOperator.restart(id);
final JobExecution restartExecution = jobExplorer.getJobExecution(restartId);
assert restartExecution != null;
final ExitStatus status = restartExecution.getExitStatus();
log.info("Job exited with status: {}", status.getExitDescription());
return ResponseEntity.ok("started");
} catch (Exception e) {
log.error("Unable to restart job: {}", e.getLocalizedMessage());
return ResponseEntity.ok("ended with an error: " + e.getLocalizedMessage());
}
}
}
Configuration class
@Configuration
public class BatchConfig {
@Bean
Job updateLinkStatusJob(JobRepository jobRepository, Step step) {
return new JobBuilder("updateJob", jobRepository)
.incrementer(new RunIdIncrementer())
.start(step)
.build();
}
@Bean
public Step step(JobRepository jobRepository,
PlatformTransactionManager transactionManager,
MyItemReader reader,
MyItemWriter writer) {
return new StepBuilder("updateStep", jobRepository)
.<StatusUpdateDTO, StatusUpdateDTO>chunk(chunkSize, transactionManager)
.reader(reader)
.writer(writer)
.build();
}
}
Please suggest me how to resolve this error?
That error means the job you are trying to restart is not registered in the JobRegistry
used by the JobOperator
. You need to add a bean of type JobRegistryBeanPostProcessor
in the application context, see: https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#jobregistrybeanpostprocessor