0

I use Spring Batch 4.3.2. I need to define a TaskExecutor for the JobLauncher. As i don't want to enable bean overriding, the solution with DefaultBatchConfigurer is not applicable. I ended up with the the beans below, defining all bean on my own. My Question:

  1. Do you see an other solutions than mine?
  2. Are the beans defined the correct way?

Thanks for your help! Cheers T

 @Bean
public JobBuilderFactory jobBuilderFactory(JobRepository jobRepository) {
    return new JobBuilderFactory(jobRepository);
}

@Bean
public StepBuilderFactory stepBuilderFactory(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
    return new StepBuilderFactory(jobRepository, transactionManager);
}

@Bean
public JobRegistry jobRegistry() {
    return new MapJobRegistry();
}

@Bean
public JobRepository createJobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDataSource(dataSource);
    factory.setTransactionManager(transactionManager);
    factory.afterPropertiesSet();
    return factory.getObject();
}

@Bean
public JobExplorer createJobExplorer(DataSource dataSource) throws Exception {
    JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
    jobExplorerFactoryBean.setDataSource(dataSource);
    jobExplorerFactoryBean.afterPropertiesSet();
    return jobExplorerFactoryBean.getObject();
}

@Bean
public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor, JobRepository jobRepository) throws Exception {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    jobLauncher.setTaskExecutor(taskExecutor);
    jobLauncher.afterPropertiesSet();
    return jobLauncher;
}

@Bean
public ThreadPoolTaskExecutor msBatchTaskExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(50);
    taskExecutor.setQueueCapacity(50);
    return taskExecutor;
}
user286974
  • 111
  • 1
  • 1
  • 12
  • `As i don't want to enable bean overriding, the solution with DefaultBatchConfigurer is not applicable`: I'm curious about the reason for that. Is there is something wrong with bean overriding? What you would you suggest if the solution with `BatchConfigurer` is not applicable? I'm open for ideas to improve things if possible. – Mahmoud Ben Hassine Jun 15 '21 at 08:30
  • Hi Ben For Testing and Non-Prod Environment is Bean Overwriting fine. We are afraid that a miss configuration could cause Problems. Eg that we Overwrite the Prod- DataSource with a H2 DataSource. A cool feature would be, when i could configure which Beans are allowed to overwrite. eg spring.beans-allowed-to-overwrite=org.springframework.batch.core.launch.JobLauncher – user286974 Jun 16 '21 at 09:14
  • You should not rely on bean overriding for such requirement, spring profiles are better suited in my opinion. Something like a test profile and prod profile for the datasource bean, here is an example: https://stackoverflow.com/a/58537904/5019386. – Mahmoud Ben Hassine Jun 16 '21 at 09:59

1 Answers1

1

If you don't want to use @EnableBatchProcessing, then indeed the way to go for Java configuration is to manually define infrastructure beans.

Note this is how XML configuration works as well, you need to define infrastructure beans manually as there is no equivalent to @EnableBatchProcessing in XML (there is an open feature request for it).

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50