2017-09-15 186 views
0

我已经开始探索Spring Batch并遇到一些基本问题。Spring批处理作业信息库

我怎么能单独配置数据源为工作存储库。我的商家数据驻留在不同的存储库中。

其次,当我尝试我的批处理应用程序,Spring Batch的repeateldy尝试一遍又一遍的创造同样的工作模式表。

感谢您的帮助。

+0

你使用Spring启动? –

回答

0

如果您使用的是Spring Boot,那就这样做吧。 注意下面我们正在配置getDatasource()方法,它提供了我们需要的数据源。这将强制引导不使用默认数据源。

package com.demo.configuration; 

import org.springframework.batch.core.Job; 
import org.springframework.batch.core.Step; 
import org.springframework.batch.core.StepContribution; 
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 
import org.springframework.batch.core.scope.context.ChunkContext; 
import org.springframework.batch.core.step.tasklet.Tasklet; 
import org.springframework.batch.repeat.RepeatStatus; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.jdbc.datasource.SimpleDriverDataSource; 

import javax.sql.DataSource; 

/** 
* Created by Sushil Behera on 12/26/2017. 
*/ 
@Configuration 
@EnableBatchProcessing 
public class BatchConfiguration { 

    @Autowired 
    public JobBuilderFactory jobBuilderFactory; 

    @Autowired 
    public StepBuilderFactory stepBuilderFactory; 

    @Bean 
    public Job job1(){ 
     return jobBuilderFactory.get("job1") 
      .start(step1()) 
      .build(); 
    } 

    @Bean 
    public Step step1(){ 
     return stepBuilderFactory.get("job1step1") 
      .tasklet(
        new Tasklet(){ 
         @Override 
         public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { 
          System.out.println("Tasklet executed"); 
          return RepeatStatus.FINISHED; 
         } 
        } 
      ).build(); 
    } 

    @Bean 
    @ConfigurationProperties(prefix = "demo.datasource") 
    public DataSource getDatasource(){ 
     return new SimpleDriverDataSource(); 
    } 

} 

application.properties

spring.application.name=Demo 
spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver 
spring.datasource.url=URL1 
spring.datasource.username=ABC 
spring.datasource.password=ABC1 
demo.datasource.url=URL2 
demo.datasource.driverClassName=oracle.jdbc.driver.OracleDriver 
demo.datasource.username=XYZ 
demo.datasource.password=XYZ1 
相关问题