[Java] [Spring] [Spring Batch] Do not create / use Spring Batch metadata table

Premise

Spring Batch uses a database to store the state of jobs and so on.

It's usually convenient, but it's a little annoying when it's not such a heavy batch.

It is a method that does not use or make such a table.

This article was very helpful. : smile: Do this when Spring Batch does not want to use metatable but it is used

environment

How to prevent a table from being made

The table is created while Spring Boot automatically configures it.

You can do it well.

But this time it is omitted.

Add the following settings to your properties file or yaml file.

application.properties


spring.batch.initializer.enabled=false

How to avoid using a table

Please read this article. Do this when Spring Batch does not want to use metatable but it is used

Alternatively, you may create your own BatchConfigurer class (I created it because I made a detour).

MyBatchConfigurer.java


import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.configuration.BatchConfigurationException;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

/**
 *Use Job Repository without database
 *
 */
@Component
public class MyBatchConfigurer implements BatchConfigurer {

    private static final Log LOG = LogFactory.getLog(MyBatchConfigurer.class);

    private PlatformTransactionManager transactionManager;
    private JobRepository jobRepository;
    private JobLauncher jobLauncher;
    private JobExplorer jobExplorer;

    @PostConstruct
    public void initialize() {
        if (this.transactionManager == null) {
            LOG.info("Create ResourceLessTransactionManager.");
            this.transactionManager = new ResourcelessTransactionManager();
        }

        try {
            MapJobRepositoryFactoryBean repoFactory
                    = new MapJobRepositoryFactoryBean(this.transactionManager);
            repoFactory.afterPropertiesSet();
            this.jobRepository = repoFactory.getObject();

            MapJobExplorerFactoryBean explFactory
                    = new MapJobExplorerFactoryBean(repoFactory);
            explFactory.afterPropertiesSet();

            this.jobExplorer = explFactory.getObject();
            this.jobLauncher = createJobLauncher();
        } catch (Exception ex) {
            LOG.fatal(ex.getMessage(), ex);
            throw new BatchConfigurationException(ex);
        }

    }

    protected JobLauncher createJobLauncher() throws Exception {
        SimpleJobLauncher launcher = new SimpleJobLauncher();
        launcher.setJobRepository(jobRepository);
        launcher.afterPropertiesSet();
        return launcher;
    }

    @Override
    public JobRepository getJobRepository() throws Exception {
        return jobRepository;
    }

    @Override
    public PlatformTransactionManager getTransactionManager() throws Exception {
        return transactionManager;
    }

    @Override
    public JobLauncher getJobLauncher() throws Exception {
        return jobLauncher;
    }

    @Override
    public JobExplorer getJobExplorer() throws Exception {
        return jobExplorer;
    }
}

Picking up fallen ears related to Spring Boot

Disable Batch Auto Configuration?

When I disabled it, there were various classes that I had to create, so I enabled it.

Is it OK to enable Data Source Auto Configuration?

I prepared Batch Configurer by myself, so it seems OK.

Recommended Posts

[Java] [Spring] [Spring Batch] Do not create / use Spring Batch metadata table
[Java] Do not use "+" in append!
File upload with Spring Boot (do not use Multipart File)
Do you use Stream in Java?
Do not use instance variables in partial
Create a simple on-demand batch with Spring Batch
Create Java Spring Boot project in IntelliJ
Do not declare variables in List in Java
[Java] Why do you bother to use the interface (Spring is also available)
Spring Java
Support for CheckStyle "Do not use inline conditions"
[JAVA] [Spring] [MyBatis] Use IN () with SQL Builder
[Spring Batch] Output table data to CSV file