2017-09-15 164 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 
相關問題