简体   繁体   中英

Duplication Job in Spring Batch

Getting error Cannot Register Job Configuration because DuplicationJobException After Upgrade to Spring 2.x

Already checked, there is no duplicate name in my code.

The Error

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:558)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobCalcSampleDetail' defined in class path resource [id/co/a/microservice/batch/job/SampleJobConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.FatalBeanException: Cannot register job configuration; nested exception is org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [jobCalcSampleDetail] was already registered
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at id.co.a.microservice.batch.NcsBatchServiceApplication.main(NcsBatchServiceApplication.java:15)
    ... 6 more
Caused by: org.springframework.beans.FatalBeanException: Cannot register job configuration; nested exception is org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [jobCalcSampleDetail] was already registered
    at org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor.postProcessAfterInitialization(JobRegistryBeanPostProcessor.java:150)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:429)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    ... 21 more
Caused by: org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [jobCalcSampleDetail] was already registered
    at org.springframework.batch.core.configuration.support.MapJobRegistry.register(MapJobRegistry.java:52)
    at org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor.postProcessAfterInitialization(JobRegistryBeanPostProcessor.java:146)
    ... 24 more

Job Configuration Code

@Configuration 
@EnableBatchProcessing 
public class SampleJobConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Autowired
public PostgresDbConfig postgres;

@Value("${xxx.chunk.default}")
private int chunkSize;

@Value("${xxx.limit.retry}")
private int retryLimit;

@Bean
@StepScope
public Tasklet taskletRmvSample(@Value("#{jobParameters['period']}") String period,
        @Value("#{jobParameters['clearData']}") Boolean clearData) {
    return (StepContribution stepContribution, ChunkContext chunkContext) -> {
        if (clearData) {
            new JdbcTemplate(postgres.dataSource()).execute("");
        }

        return RepeatStatus.FINISHED;
    };
}

@Bean
public Step step1RmvSample() {
    return stepBuilderFactory.get("step1RmvSampleDetail").tasklet(
            taskletRmvSample(null, null)).build();
}

@Bean
public Job jobCalcSampleDetail() throws Exception {
    return jobBuilderFactory.get("jobCalcSampleDetail").incrementer(new RunIdIncrementer()).start(
            step1RmvSample()).build();
}
}

Main Application Code

@SpringBootApplication
@EnableDiscoveryClient 
@EnableScheduling
public class NcsBatchServiceApplication {
   public static void main(String[] args) {
    SpringApplication.run(NcsBatchServiceApplication.class, args);
   }
}

Used Spring Boot Parent 2.1.4 - Spring Batch Core 4.1.1

previously it s working fine when using spring 1.5.17 but after upgrade to spring 2.* is error.

Also tried to add make modular but still error

@EnableBatchProcessing(modular = true)

Package Structure image

Maybe somebody can help. Thanks

In my case I had several jobs defined in different packages but if I created more than one job at a time (by toggling the @Configuration) the application threw a duplicateJobConfiguration Exception. After working through pretty much every solution presented by Google and StackOverflow I stumbled across my solution which was to remove from the class that included the Job bean a boilerplate post-processor that had been copied in from a code sample somewhere:

@Bean
public JobRegistryBeanPostProcessor edwJobRegistryBeanPostProcessor(JobRegistry 
 jobRegistry) {
    JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new 
     JobRegistryBeanPostProcessor();
 jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
 return jobRegistryBeanPostProcessor;
}

Somehow the multiple instances of JobRegistryBeanPostProcessor were triggering the error even though each was given a unique name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM