0

I am using both Spring Boot and Batch in a maven multi-module project for parsing CSV files and storing data in a MySQL database.

When running the batch module using my BatchLauncher class (shared below) I get a BeanCurrentlyInCreationException caused by getDataBase() which I use for configuring my MySQL database. (click this link to see logs)

And when I remove this method Spring Boot choose automatically an embedded database of type H2 (link for logs)

BatchLauncher class :

@Slf4j
public class BatchLauncher {

    public static void main(String[] args) {
        try {
            Launcher.launchWithConfig("My Batch", BatchConfig.class, false);
        }catch (Exception ex) {
        log.error(ex.getMessage());
        }
    }
}

Launcher class :

@Slf4j
public class Launcher {

    private Launcher() {}

    public static void launchWithConfig(String batchName, Class<?> configClass, boolean oncePerDayMax) throws JobExecutionException, BatchException {
        try {
            // Check the spring profiles used
            log.info("Start batch \"" + batchName + "\" with profiles : " + System.getProperty("spring.profiles.active"));

            // Load configuration
            @SuppressWarnings("resource")
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configClass);

            JobLauncher jobLauncher = context.getBean(JobLauncher.class);
            Job job = context.getBean(Job.class);

            //Authorize only one execution of each job per day
            JobParameters jobParameters = new JobParameters();
            JobExecution execution = jobLauncher.run(job, jobParameters);

            if(!BatchStatus.COMPLETED.equals(execution.getStatus())) {
                throw new BatchException("Unknown error while executing batch : " + batchName);
            }
        }catch (Exception ex){
            log.error("Exception",ex);
            throw new BatchException(ex.getMessage());
        }
    }
}

BatchConfig class :

@Slf4j
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableBatchProcessing
@ComponentScan(basePackages = {
        "fr.payet.flad.batch.tasklet",
        "fr.payet.flad.batch.mapper"
})
@Import({CoreConfig.class})
public class BatchConfig {


    private StepBuilderFactory steps;
    private JobBuilderFactory jobBuilderFactory;
    private ReadInputTasklet readInputTasklet;

    public BatchConfig(StepBuilderFactory steps, JobBuilderFactory jobBuilderFactory, ReadInputTasklet readInputTasklet) {
        this.steps = steps;
        this.jobBuilderFactory = jobBuilderFactory;
        this.readInputTasklet = readInputTasklet;
    }

    @Bean
    public DataSource getDataBase(){
        return DataSourceBuilder
                .create()
                .driverClassName("com.mysql.jdbc.Driver")
                .url("jdbc:mysql://localhost:3306/myDb?useSSL=false")
                .username("myuser")
                .password("mypwd")
                .build();
    }

    @Bean
    public Step readInputStep() {
        return steps.get("readInputStep")
                .tasklet(readInputTasklet)
                .build();
    }

    @Bean
    public Job readCsvJob() {
        return jobBuilderFactory.get("readCsvJob")
                .incrementer(new RunIdIncrementer())
                .flow(readInputStep())
                .end()
                .build();
    }
}
Ghassen
  • 591
  • 1
  • 15
  • 33
  • This might help: https://stackoverflow.com/questions/28158094/spring-boot-enableautoconfiguration-with-exclude-not-working – moilejter Jul 25 '18 at 05:58

1 Answers1

0

The solution was to create a custom DataSourceConfiguration class annotated with @Configuration in which I set my own database like this :

@Bean
public DataSource getDataBase(){
    return DataSourceBuilder
            .create()
            .driverClassName("com.mysql.jdbc.Driver")
            .url("jdbc:mysql://localhost:3306/myDB?useSSL=false")
            .username("myUser")
            .password("myPwd")
            .build();
}
Ghassen
  • 591
  • 1
  • 15
  • 33