2017-09-15 3 views
0

J'ai commencé à explorer Spring Batch et à rencontrer quelques problèmes fondamentaux.Regroupement de tâches par lots de printemps

Comment puis-je configurer une source de données séparément pour le référentiel de travaux? Les données de Mon entreprise résident dans un référentiel différent.

Lorsque j'essaie mon application par lots, spring batch repeateldy essaie de créer plusieurs fois les mêmes tables de schémas de travail.

Appréciez votre aide.

+0

Utilisez-vous Spring Boot? –

Répondre

0

Si vous utilisez Spring Boot, faites comme ceci. note ci-dessous nous configurons la méthode getDatasource() qui fournit à la source de données ce dont nous avons besoin. Cela forcera le démarrage à ne pas utiliser la source de données par défaut.

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