Spring Batch Job Repository

时间:2017-09-15 22:07:44

标签: spring-batch spring-batch-admin

我已经开始探索Spring Batch并遇到一些基本问题。

如何为作业存储库单独配置数据源。我的业务数据位于不同的存储库中。

当我尝试我的批处理应用程序时,spring batch repeateldy尝试一遍又一遍地创建相同的作业模式表。

感谢您的帮助。

1 个答案:

答案 0 :(得分: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