0
votes

I have started to explore Spring Batch and running into some fundamental issues.

How can I configure a datasource separately for the Job Repository. My Business data resides in a different repository.

Second when I try my batch app, spring batch repeateldy tries to create the same job schema tables over and over again.

Appreciate your help.

1
Are you using Spring Boot?Michael Minella

1 Answers

0
votes

If you are using Spring Boot then do like this. note below we are configuring getDatasource() method which provides the data source what we need. This will force boot to not use the default data source.

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