spring batch - How to set up a Resourceless Job Repository? - Stack Overflow

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

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 [/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).

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 [/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).
Share Improve this question asked Mar 22 at 16:56 Lucas GuimaLucas Guima 318 bronze badges 1
  • 1 I shared an example here: stackoverflow/a/79492398/5019386 – Mahmoud Ben Hassine Commented Mar 23 at 6:34
Add a comment  | 

1 Answer 1

Reset to default 1

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();
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744307446a4567783.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信