spring-batch

How to set up a Resourceless Job Repository?


I'm trying to set up a job using Spring Batch 5.2 and I'd like to not use any data source for metadata.

I couldn't find any examples of any implementation of the Resourceless Job Repository, I made a lot of attempts but I'm not a Java expert and couldn't get so far.

Attempt #1

@Configuration
public class BatchConfig {
  @Bean
  public ResourcelessJobRepository jobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    return (ResourcelessJobRepository) factory.getObject();
  }

  @Bean
  public Job taskletJob(JobRepository jobRepository, Step step) {
    return new JobBuilder("taskletJob", jobRepository)
      .start(step)
      .build();
  }
}

Error

The bean 'jobRepository', defined in class path resource [org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration$SpringBootBatchConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/batch/job/config/BatchConfig.class] and overriding is disabled.

Attempt #2

@Configuration
public class BatchConfig {
  @Bean
  public Job taskletJob(JobRepository jobRepository, Step step) {
    return new JobBuilder("taskletJob", jobRepository)
      .start(step)
      .build();
  }
}

Error

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Solution

  • After I posted I found the Mahmoud answer and I was able to implement it. Thank you!

    BatchConfig.java

    @Configuration
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
    public class BatchConfig {
    
      @Bean
      public PlatformTransactionManager transactionManager() {
        return new ResourcelessTransactionManager();
      }
    
      @Bean
      public JobRepository jobRepository() {
        return new ResourcelessJobRepository();
      }
    
      @Bean
      public JobLauncher jobLauncher(JobRepository jobRepository) {
        TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        return jobLauncher;
      }
    }
    

    JobConfig.java

    @Configuration
    public class JobConfig {
    
      @Bean
      public Job taskletJob(JobRepository jobRepository, Step step) {
        return new JobBuilder("taskletJob", jobRepository)
          .start(step)
          .build();
      }
    
      @Bean
      public Step sampleStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("sampleStep", jobRepository)
          .tasklet(new SampleTasklet(), transactionManager)
          .build();
      }
    }