簡體   English   中英

春季批處理中每個作業有不同的Jobrepository數據源?

[英]Different Jobrepository datasource per job in spring batch?

如果我有2個作業,每個作業都寫入一個不同的數據源,則將彈簧批處理元數據(jobExecution,結果等)寫入與其一起使用的數據源中是有意義的。 但是,春季批處理似乎指導您將一個“主要”數據源用於該元數據。

我定義了兩個數據源,兩個都不標記為主要,並且應用程序無法啟動:

Field dataSource in org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration required a single bean, but 2 were found:
    - firstDataSource: defined by method 'firstDataSource' in class path resource [secret/DataSourceConfig.class]
    - secondDataSource: defined by method 'secondDataSource' in class path resource [secret/DataSourceConfig.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

我嘗試創建兩個配置,每個配置都擴展DefaultBatchConfigurer:

@Configuration
@EnableBatchProcessing
public class MyJobConfiguration extends DefaultBatchConfigurer {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Bean
    public Job my_job(@Qualifier("somestep") Step step) {
        return jobBuilderFactory.get("my_job")
                .start(step)
                .build();
    }

    @Bean
    @Profile("my_job")
    public JobExecution launchMyJob(@Qualifier("my_job") Job job, JobLauncher jobLauncher) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        Map<String, JobParameter> params = new HashMap<>();
        params.put("Time", new JobParameter(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())));
        return jobLauncher.run(job, new JobParameters(params));
    }

    @Override
    @Autowired
    public void setDataSource(@Qualifier("firstDataSource") DataSource dataSource) {
        super.setDataSource(dataSource);
    }
}

另一個完全相同,只是工作和數據源不同。

那么,當我顯然想通過擴展DefaultBatchConfigurer來自己做時,為什么spring仍然嘗試創建AbstractBatchConfiguration?

更多堆棧跟蹤:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found 2: firstDataSource,secondDataSource
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.5.RELEASE.jar:5.0.5.RELEASE]

您是說要在項目中使用多個數據源?

這是供您參考的示例

https://github.com/michaelliao/springcloud/tree/master/data-multidatasource

Spring Batch的SimpleBatchConfiguration通過@EnableBatchProcessing 它使用BatchConfigurer提供需要添加到ApplicationContext的組件。 您真正想做的是創建一個BatchConfigurer ,它在正確的時間提供正確的DataSource 然后, SimpleBatchConfiguration將使用配置ApplicationContext創建的組件將它們添加到ApplicationContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM