field作业需要一个bean,但找到了2个spring启动spring批处理

kognpnkq  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(277)

我在同一个项目中有两个独立的spring批处理作业。这两个作业共享相同的读卡器和处理器,但编写器不同。我试图在java类中以编程方式运行这两个作业。当我运行应用程序时,它会抱怨:

Field job  required a single bean, but 2 were found:
- importCodesAndLabelsJob: defined by method 'importCodesAndLabelsJob' in class path resource 
    [/BatchConfiguration.class]
- importCodesAndLabelsJobLIS: defined by method 'importCodesAndLabelsJobLIS' in class path resource 
    [/BatchConfigurationLIS.class]

两个作业配置如下:
第一个:

@Configuration
     @EnableBatchProcessing
     public class BatchConfigurationLIS {

    @Autowired
    public JobBuilderFactory jobBuilderFactory; 

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public RestWebClient webClient;

    @Bean
    @StepScope
    public CodeAndLabelRestItemReader reader() {
        return new CodeAndLabelRestItemReader(webClient);
    }

    @Bean
    @StepScope
    public CodeAndLabelItemProcessor processor() {
        return new CodeAndLabelItemProcessor();
    }

    @Bean
    @StepScope
    //push data to data base
    public CodeAndLabelLISItemWriter calWriter() {          
        return new CodeAndLabelLISItemWriter();          
    }

    @Bean(name = "importCodesAndLabelsJobLIS")
    public Job importCodesAndLabelsJobLIS(JobCompletionNotificationListener listener, Step stepJms) {
        return jobBuilderFactory.get("importCodesAndLabelsJobLIS")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(stepJms)
                .end()
                .build();
    }

    @Bean
    public Step stepJms() {
        return stepBuilderFactory.get("stepJms")
                .<Code, CodeAndLabel>chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(calWriter())
                .build();
    }
   }

第二个:

@Configuration
      @EnableBatchProcessing
      public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory; 

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public RestWebClient webClient;

    @Bean
    @StepScope
    public CodeAndLabelRestItemReader reader() {
        return new CodeAndLabelRestItemReader(webClient);
    }

    @Bean
    @StepScope
    public CodeAndLabelItemProcessor processor() {
        return new CodeAndLabelItemProcessor();
    }

    @Bean
    @StepScope
    //push data to queue 
    public CodeAndLabelItemWriter calWriter(AmqpTemplate amqpTemplate) {            
        return new CodeAndLabelItemWriter(amqpTemplate);             
    }

    @Bean(name = "importCodesAndLabelsJob")
    public Job importCodesAndLabelsJob(JobCompletionNotificationListener listener, Step stepJms) {
        return jobBuilderFactory.get("importCodesAndLabelsJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(stepJms)
                .end()
                .build();
    }

    @Bean
    public Step stepJms(ItemWriter<CodeAndLabel> writer) {
        return stepBuilderFactory.get("stepJms")
                .<Code, CodeAndLabel>chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer)
                .build();
    }
   }

运行第一个作业的java类:

@Component
 public class Extract {     

@Autowired
private ApplicationContext appContext;
 public void lauchExtract(Process process, String parentMonitoringId , String language) 
 throws Exception {
   JobLauncher jobLauncher = appContext.getBean(JobLauncher.class);
   Job importCodesAndLabelsJob = appContext.getBean("importCodesAndLabelsJob", Job.class); 
     //some code here
      for(String lis : lisUid) {            
        JobParameters params = new JobParametersBuilder() 
                  .addString("JobID",String.valueOf(System.currentTimeMillis()))
                  .addString("endPointSuffix", 
                  "/codesAndLabels".concat(lis).concat(language.toUpperCase()))
                  .toJobParameters();
          jobLauncher.run(importCodesAndLabelsJob, params);
    }
  }
 }

运行第二个作业的java类:

@Component
 public class ExtractLIS {      

@Autowired
private ApplicationContext appContext;
 public void lauchExtractLIS(Process process, String parentMonitoringId , String language) 
 throws Exception {
   JobLauncher jobLauncher = appContext.getBean(JobLauncher.class);
   Job importCodesAndLabelsJobLIS = appContext.getBean("importCodesAndLabelsJobLIS", Job.class);  
     //some code here
      for(String lis : lisUid) {            
        JobParameters params = new JobParametersBuilder() 
                  .addString("JobID",String.valueOf(System.currentTimeMillis()))
                  .addString("endPointSuffix", 
                  "/codesAndLabels".concat(lis).concat(language.toUpperCase()))
                  .toJobParameters();
         jobLauncher.run(importCodesAndLabelsJobLIS, params);
    }
  }
 }

我发现这里的解决方法类需要一个bean,但是发现了2个:我试图像这样更改两个类的配置,但是我有相同的问题

@Component
 public class ExtractLIS {
@Autowired
JobLauncher jobLauncher;
@Autowired
@Qualifier("importCodesAndLabelsJobLIS")
Job importCodesAndLabelsJobLIS;

我也试着像这样添加@primary注解,但是这两个作业是以相同的配置运行的

@Bean(name = "importCodesAndLabelsJob")
    @Primary
    public Job importCodesAndLabelsJob(JobCompletionNotificationListener listener, Step stepJms) {
        return jobBuilderFactory.get("importCodesAndLabelsJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(stepJms)
                .end()
                .build();
    }

有人能帮忙吗

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题