| Title | org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [....] was already registered org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [....] was already registered | ||||||
| Writer | Ji-Seob Lee | Write Date | May 11 2025 | Modify Date | May 13 2025 | View Count | 1745 |
When you raised the version from 3.3.x to 3.4.x in a spring boot,
you may encounter a message like the one below
org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [testJob] was already registered
Spring Boot 3.4.x → uses Spring Batch 5.1.x under the hood.
In this version, the following changes may have occurred
- Possible changes in the way Job registration and JobRegistry behave.
In Spring Boot 3.4, the automatic registration of JobRegistry or JobBuilderFactory when setting up Spring Batch might have been tightened up or duplicate detection might have been stricter.
In other words, after the same Job name is automatically registered,
and then trying to register it again manually might have caused an exception to be thrown.
1. Try changing to avoid calling JobRegistry.register() directly
Because in Spring Boot 3.4.x, you might already have the JobRegistry auto-registration feature enabled,
try removing the direct registration.
@Configuration
@RequiredArgsConstructor
//@Component
@Slf4j
public class BatchConfig {
private final JobRegistry jobRegistry;
@Bean
public Job testJob2(JobRepository jobRepository,PlatformTransactionManager transactionManager) throws DuplicateJobException {
Job job = new JobBuilder("testJob2",jobRepository)
.start(testStep(jobRepository,transactionManager))
.build();
// Comment out this part
//ReferenceJobFactory factory = new ReferenceJobFactory(job);
//jobRegistry.register(factory);
return job;
}
@Bean
public Step testStep(JobRepository jobRepository,PlatformTransactionManager transactionManager){
Step step = new StepBuilder("testStep",jobRepository)
.tasklet(testTasklet(null),transactionManager)
.build();
return step;
}
@Bean
@StepScope
public Tasklet testTasklet(@Value("#{jobParameters[time]}") String time) {
return ((contribution, chunkContext) -> {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = now.format(formatter);
log.info(formattedDate + " : ***** hello batch! ***** : " + time);
return RepeatStatus.FINISHED;
});
}
}
If you instead let Spring Boot register the job to the JobRegistry on its own, the duplication problem may go away.
The web page referenced
| |||||||