Map-based Implementations Deprecated: Overriding getDataSource in Spring Boot's DefaultBatchConfiguration

Map-based Implementations Deprecated: Overriding getDataSource in Spring Boot's DefaultBatchConfiguration
Photo by nilufar nattaq / Unsplash

With Spring Batch 5.0, significant changes have been made regarding job repository and explorer configurations. The Map-based job repository/explorer implementations were deprecated in version 4 and completely removed in version 5. This necessitates customizing the default batch configuration to use a Jdbc-based implementation. Additionally, Spring Batch 5.0 mandates using the Jdbc-based JobRepository, and customizing the data source configuration can help keep batch job-related data separate from your production database. Here’s how you can override the getDataSource method in DefaultBatchConfiguration to achieve this.

Step-by-Step Guide

  1. Create a Custom Batch Configuration Class: Extend DefaultBatchConfiguration and override the getDataSource method in your custom configuration class.
  2. Define the DataSource Bean: Define a DataSource bean within your custom configuration class or another central configuration class.

Here's an example:

import org.springframework.batch.core.configuration.annotation.DefaultBatchConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.batch.core.initializer.BatchDataSourceScriptDatabaseInitializer;
import org.springframework.batch.core.configuration.BatchProperties;
import org.springframework.jdbc.datasource.init.DatabaseInitializationMode;

@Configuration
public class CustomBatchConfiguration extends DefaultBatchConfiguration {

    @Override
    public DataSource getDataSource() {
        return customBatchDataSource();
    }

    private DataSource customBatchDataSource() {
        DataSource dataSource = DataSourceBuilder.create()
                .url("jdbc:h2:mem:batchdb;DB_CLOSE_DELAY=-1")
                .username("sa")
                .password("")
                .driverClassName("org.h2.Driver")
                .build();

        // Initialize batch datasource script
        BatchProperties.Jdbc jdbc = new BatchProperties.Jdbc();
        jdbc.setInitializeSchema(DatabaseInitializationMode.ALWAYS);
        new BatchDataSourceScriptDatabaseInitializer(dataSource, jdbc)
                .initializeDatabase();

        return dataSource;
    }
}

In this example:

  • CustomBatchConfiguration extends DefaultBatchConfiguration.
  • The getDataSource method is overridden to return a custom DataSource for batch jobs.
  • customBatchDataSource method configures the data source properties, using H2 in-memory database.
  • The batch datasource script is initialized to ensure that the necessary batch job tables are created.
  1. Ensure Configuration is Picked Up by Spring Boot: Make sure your custom configuration class is in a package scanned by Spring Boot. If not, use @ComponentScan on your main application class or another configuration class.

Job Repository/Explorer Configuration Updates

  • Map-based Implementations Deprecated: The Map-based job repository/explorer implementations were deprecated in version 4 and completely removed in version 5.
  • Jdbc-based Implementation: In Spring Batch 5.0, the Jdbc-based JobRepository is the only supported implementation. The @EnableBatchProcessing annotation will configure a Jdbc-based JobRepository, which requires a DataSource bean in the application context.
  • Using In-Memory Database for Batch Jobs: If you do not want batch job-related tables created in your production database, you can configure the batch job repository to use an embedded database like H2. This way, batch job metadata is stored in the in-memory database while your production data remains in your primary database (e.g., Oracle, Microsoft SQL Server, MySQL).

For more details, refer to the Spring Batch Migration Guide.

By following these steps, you can effectively customize the data source configuration for your Spring Batch jobs in a Spring Boot application while adhering to the latest updates in Spring Batch 5.0 and maintaining separation between batch job metadata and production data.

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe