Spring Batch Disable Meta Data Log

Meta Data Log?

By default, Spring Batch saves status into DataSource.

This is for recover batch. If the batch stops due to errors, the status is saved and developers can check its status and consider recovery way.

But, sometimes this function is annoying. We cannot run same batch with same parameters, or this is small batch, we don’t need to consider intermediate recovery(It’s fine always recover from beginning, just re-run)

What happens without datasource setting?

Let’s run

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

Oh, error occurred .

Prevent auto configuration of datasource

To prevent this situation, disable auto setting for datasource. Not save data into datasource.

Create configuration exclude datasource setting

BatchConfiguration

@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class BatchConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet((StepContribution contribution, ChunkContext chunkContext)-> {
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Job job(Step step1) throws Exception {
        return jobBuilderFactory.get("job1")
                .incrementer(new RunIdIncrementer())
                .start(step1)
                .build();
    }
}

Point is @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})

Java
スポンサーリンク
Professional Programmer2

コメント