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

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

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

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

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

  1. @Configuration
  2. @EnableBatchProcessing
  3. public class BatchConfigurationLIS {
  4. @Autowired
  5. public JobBuilderFactory jobBuilderFactory;
  6. @Autowired
  7. public StepBuilderFactory stepBuilderFactory;
  8. @Autowired
  9. public RestWebClient webClient;
  10. @Bean
  11. @StepScope
  12. public CodeAndLabelRestItemReader reader() {
  13. return new CodeAndLabelRestItemReader(webClient);
  14. }
  15. @Bean
  16. @StepScope
  17. public CodeAndLabelItemProcessor processor() {
  18. return new CodeAndLabelItemProcessor();
  19. }
  20. @Bean
  21. @StepScope
  22. //push data to data base
  23. public CodeAndLabelLISItemWriter calWriter() {
  24. return new CodeAndLabelLISItemWriter();
  25. }
  26. @Bean(name = "importCodesAndLabelsJobLIS")
  27. public Job importCodesAndLabelsJobLIS(JobCompletionNotificationListener listener, Step stepJms) {
  28. return jobBuilderFactory.get("importCodesAndLabelsJobLIS")
  29. .incrementer(new RunIdIncrementer())
  30. .listener(listener)
  31. .flow(stepJms)
  32. .end()
  33. .build();
  34. }
  35. @Bean
  36. public Step stepJms() {
  37. return stepBuilderFactory.get("stepJms")
  38. .<Code, CodeAndLabel>chunk(10)
  39. .reader(reader())
  40. .processor(processor())
  41. .writer(calWriter())
  42. .build();
  43. }
  44. }

第二个:

  1. @Configuration
  2. @EnableBatchProcessing
  3. public class BatchConfiguration {
  4. @Autowired
  5. public JobBuilderFactory jobBuilderFactory;
  6. @Autowired
  7. public StepBuilderFactory stepBuilderFactory;
  8. @Autowired
  9. public RestWebClient webClient;
  10. @Bean
  11. @StepScope
  12. public CodeAndLabelRestItemReader reader() {
  13. return new CodeAndLabelRestItemReader(webClient);
  14. }
  15. @Bean
  16. @StepScope
  17. public CodeAndLabelItemProcessor processor() {
  18. return new CodeAndLabelItemProcessor();
  19. }
  20. @Bean
  21. @StepScope
  22. //push data to queue
  23. public CodeAndLabelItemWriter calWriter(AmqpTemplate amqpTemplate) {
  24. return new CodeAndLabelItemWriter(amqpTemplate);
  25. }
  26. @Bean(name = "importCodesAndLabelsJob")
  27. public Job importCodesAndLabelsJob(JobCompletionNotificationListener listener, Step stepJms) {
  28. return jobBuilderFactory.get("importCodesAndLabelsJob")
  29. .incrementer(new RunIdIncrementer())
  30. .listener(listener)
  31. .flow(stepJms)
  32. .end()
  33. .build();
  34. }
  35. @Bean
  36. public Step stepJms(ItemWriter<CodeAndLabel> writer) {
  37. return stepBuilderFactory.get("stepJms")
  38. .<Code, CodeAndLabel>chunk(10)
  39. .reader(reader())
  40. .processor(processor())
  41. .writer(writer)
  42. .build();
  43. }
  44. }

运行第一个作业的java类:

  1. @Component
  2. public class Extract {
  3. @Autowired
  4. private ApplicationContext appContext;
  5. public void lauchExtract(Process process, String parentMonitoringId , String language)
  6. throws Exception {
  7. JobLauncher jobLauncher = appContext.getBean(JobLauncher.class);
  8. Job importCodesAndLabelsJob = appContext.getBean("importCodesAndLabelsJob", Job.class);
  9. //some code here
  10. for(String lis : lisUid) {
  11. JobParameters params = new JobParametersBuilder()
  12. .addString("JobID",String.valueOf(System.currentTimeMillis()))
  13. .addString("endPointSuffix",
  14. "/codesAndLabels".concat(lis).concat(language.toUpperCase()))
  15. .toJobParameters();
  16. jobLauncher.run(importCodesAndLabelsJob, params);
  17. }
  18. }
  19. }

运行第二个作业的java类:

  1. @Component
  2. public class ExtractLIS {
  3. @Autowired
  4. private ApplicationContext appContext;
  5. public void lauchExtractLIS(Process process, String parentMonitoringId , String language)
  6. throws Exception {
  7. JobLauncher jobLauncher = appContext.getBean(JobLauncher.class);
  8. Job importCodesAndLabelsJobLIS = appContext.getBean("importCodesAndLabelsJobLIS", Job.class);
  9. //some code here
  10. for(String lis : lisUid) {
  11. JobParameters params = new JobParametersBuilder()
  12. .addString("JobID",String.valueOf(System.currentTimeMillis()))
  13. .addString("endPointSuffix",
  14. "/codesAndLabels".concat(lis).concat(language.toUpperCase()))
  15. .toJobParameters();
  16. jobLauncher.run(importCodesAndLabelsJobLIS, params);
  17. }
  18. }
  19. }

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

  1. @Component
  2. public class ExtractLIS {
  3. @Autowired
  4. JobLauncher jobLauncher;
  5. @Autowired
  6. @Qualifier("importCodesAndLabelsJobLIS")
  7. Job importCodesAndLabelsJobLIS;

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

  1. @Bean(name = "importCodesAndLabelsJob")
  2. @Primary
  3. public Job importCodesAndLabelsJob(JobCompletionNotificationListener listener, Step stepJms) {
  4. return jobBuilderFactory.get("importCodesAndLabelsJob")
  5. .incrementer(new RunIdIncrementer())
  6. .listener(listener)
  7. .flow(stepJms)
  8. .end()
  9. .build();
  10. }

有人能帮忙吗

暂无答案!

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

相关问题