# Infrastructure Configuration (Using H2)

In 
Published 2022-12-03

The tutorial explains what you have to do in order to start working with Spring Batch. For this example I use the H2 database.

With Spring Batch, you can define and run jobs. Typically, Batch Jobs are long-running, non-interactive and process large volumes of data, more than fits in memory or a single transaction. The jobs may have a step or many steps. During a step, the job do something, a particular task, named tasklet. When you define a step you can define the next step you have to execute. A job could execute steps in a defined order. We can have sequential steps and parallel steps. We can execute some tasks (steps) when a condition is true or false. Because of these things, you can create workflows using Spring Batch.

For more information related to Spring Batch model you can take a look to the article named SPRING BATCH: The Main Concepts

All the information about the job executions are stored in a Database Repository. This is named JobRepository. You can take a look at the following picture:

In my example I will store the JobRepository into an H2 database.

For doing this, you have first to install an H2 database and to configure the context of a Spring application to use it as a JobRepository. This configuration will be done by the SpringBatchConfiguration class:

package com.examples.config;
 
import javax.sql.DataSource;
 
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
 
@Configuration
@EnableBatchProcessing
@ComponentScan("com.examples.*")
@PropertySource("classpath:/com/examples/properties/batch.properties")
public class SpringBatchConfiguration {
    
    @Autowired
    private Environment env;
     
    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl(env.getRequiredProperty("dataSource.url"));
        dataSource.setDriverClassName(env.getRequiredProperty("dataSource.driverClassName"));
        dataSource.setUsername(env.getRequiredProperty("dataSource.username"));
        dataSource.setPassword(env.getRequiredProperty("dataSource.password"));
         
        return dataSource;
    }
     
    @Bean
    public DataSourceInitializer h2DatabasePopulator() {
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(
                new ClassPathResource("org/springframework/batch/core/schema-h2.sql"));
        populator.addScript(
                new ClassPathResource("main/resource/sql/reset_user_registration.sql"));
        populator.setContinueOnError(true);
        populator.setIgnoreFailedDrops(true);
 
        DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDatabasePopulator(populator);
        initializer.setDataSource(dataSource());
         
        return initializer;
    }
}

Pay attention: @EnableBachProcessing annotation will configure a default JobRepository, JobRegistry and JobLauncher beans.

In the batch.properties file I have:

Now you have to add the main class to test our work:

package com.examples;
 
import java.util.Date;
 
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import com.examples.config.SpringBatchConfiguration;
import com.examples.config.UserJob;
 
public class Main {
    public static void main(String[] args) throws Throwable {
         
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringBatchConfiguration.class);
         
        JobRegistry jobRegistry = context.getBean("jobRegistry", JobRegistry.class);
        JobLauncher jobLauncher = context.getBean("jobLauncher", JobLauncher.class);
        JobRepository jobRepository = context.getBean("jobRepository", JobRepository.class);
         
        System.out.println(" jobRegistry: "+jobRegistry);
        System.out.println(" jobLauncher: "+jobLauncher);
        System.out.println(" jobRepository: "+jobRepository);
    }
}

When you run the application you will see an output like this:

When you take a look into the H2 database you will see something like this:

New tables are created into the BATCH schema for storing the Job Repository.