在我的Spring批处理项目(Spring Boot版本2.3.4.RELEASE,Java 1.8(中,我有一个处理器组件需要访问作业Id(用于跟踪目的(。我在bean声明中添加了@JobScope,这使我可以访问作业Id。
@Component("dealerItemProcessor")
@JobScope
public class DealerItemProcessor implements ItemProcessor<Dealer, Dealer> {
@Value("#{jobExecution}")
private JobExecution jobExecution;
@Override
public Dealer process(final Dealer dealer) throws Exception {
//Get jobExecution.getJobId(), process data bean
}
我用XML声明了这份工作,如下所示:
<job id="syncJob" >
<step id="step1">
<tasklet>
<chunk reader="itemReader"
processor="dealerItemProcessor"
writer="itemWriter" commit-interval="1"/>
</tasklet>
</step>
<listeners>
<listener ref="syncJobCompletionNotificationListener"/>
</listeners>
</job>
XML配置加载为:
@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:batch-job.xml")
public class XMLConfigurationLoader {
}
作业安排如下:
public SyncJobScheduler(@Qualifier("syncJob") Job dealerSyncJob,
JobLauncher jobLauncher) {
this.syncJob = syncJob;
this.jobLauncher = jobLauncher;
}
@Scheduled(cron = "0 0 */1 * * *")
public void schedule() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
jobLauncher.run(syncJob, new JobParametersBuilder()
.addDate("date", new Date())
.toJobParameters());
}
当我在Linux操作系统服务器和OpenJDK 1.8中构建和运行该项目时,我会收到以下错误。
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'scopedTarget.dealerItemProcessor', defined in BeanDefinition defined in URL [jar:file:/var/www/jobs/upload-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/sync/connect/processor/DealerItemProcessor.class], could not be registered. A bean with that name has already been defined in URL [jar:file:/var/www/jobs/upload-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/sync/connect/processor/DealerItemProcessor.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
我已启用调试模式,并看到相同的错误。如何解决此问题?你能给我一些建议吗?
更新1:我尝试将JobScope更改为StepScope,并看到类似的异常。2020-10-15 10:53:02.651[调试][主要]
[org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:37] Application failed to start due to an exception
org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'scopedTarget.dealerItemProcessor' defined in BeanDefinition defined in URL [jar:file:/var/www/jobs/upload-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/sync/connect/processor/DealerItemProcessor.class]: Cannot register bean definition [Root bean: class [org.springframework.aop.scope.ScopedProxyFactoryBean]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in BeanDefinition defined in URL [jar:file:/var/www/jobs/upload-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/sync/connect/processor/DealerItemProcessor.class]] for bean 'scopedTarget.dealerItemProcessor': There is already [Generic bean: class [com.sync.connect.processor.DealerItemProcessor]; scope=step; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=false; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/var/www/jobs/upload-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/sync/connect/processor/DealerItemProcessor.class]] bound.
您可以使用@StepScope
而不是@JobScope
。您可以使用stepExecution.getJobExecution()
来获取jobExecution。然后,正如你所提到的,你可以获得工作ID。
我通过实现StepExecutionListener实现了这一点。这提供了对StepExecution对象的访问,我们从中获得作业Id。
@Component("dealerItemProcessor")
public class DealerItemProcessor implements ItemProcessor<Dealer, Dealer> , StepExecutionListener {
private StepExecution stepExecution;
@Override
public Dealer process(final Dealer dealer) throws Exception {
//get unique JobId like this - stepExecution.getJobExecutionId()
// process logic
}
@Override
public void beforeStep(StepExecution stepExecution) {
//get the stepExecution Object.
this.stepExecution = stepExecution;
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
return null;
}
}
该解决方案通过提供一个替代方案来解决获取JobId的需要。它并没有解决问题中发布的Bean创建异常。