[JAVA] How to use In-Memory Job repository in Spring Batch

Since Spring Batch uses DB-based Job repository by default, a table for Job management is created without permission. If you are a DB user who does not have the authority to create a table, or if you are concerned about performance, you can use the In-Memory Job repository. However, in the case of In-Memory Job repository, functions such as restart will not be available, so whether to use it depends on non-functional requirements.

Note that the in-memory repository is volatile and so does not allow restart between JVM instances. It also cannot guarantee that two job instances with the same parameters are launched simultaneously, and is not suitable for use in a multi-threaded Job, or a locally partitioned Step. So use the database version of the repository wherever you need those features.

Solution 1

The easiest solution is not to use the DataSource's AutoConfig.

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args).close();
	}
}

Spring Batch 3.0.8.RLEASE API Description of Annotation Type Enable Batch Processing

If a user does not provide a DataSource within the context, a Map based JobRepository will be used.

Solution 2

Even if the DataSource is AutoConfig, you can use the Map-based JobRepository by overriding JobRepository, JobExplorer, and JobLauncher.


@Configuration
@EnableBatchProcessing
public class BatchConfig {

	......
    
	/**
	 * in-use memory job repository
	 * 
	 * @return
	 */
	@Bean
	DefaultBatchConfigurer batchConfigurer() {
		return new DefaultBatchConfigurer() {
			
			private JobRepository jobRepository;
			private JobExplorer jobExplorer;
			private JobLauncher jobLauncher;

			{
				MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean();
				try {
					this.jobRepository = jobRepositoryFactory.getObject();
					MapJobExplorerFactoryBean jobExplorerFactory = new MapJobExplorerFactoryBean(jobRepositoryFactory);
					this.jobExplorer = jobExplorerFactory.getObject();
					SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
					jobLauncher.setJobRepository(jobRepository);
					jobLauncher.afterPropertiesSet();
					this.jobLauncher = jobLauncher;
					
				} catch (Exception e) {
				}
			}

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

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

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

Modify ʻapplication.properties` as follows to prevent the metadata table from being generated.

application.properties


spring.batch.initializer.enabled=false

Reference: https://blog.ik.am/entries/409 https://qiita.com/blackawa/items/e9eaa254cbe27e257e10

Recommended Posts

How to use In-Memory Job repository in Spring Batch
How to use Lombok in Spring
How to use CommandLineRunner in Spring Batch of Spring Boot
How to use InjectorHolder in OpenAM
[Artifactory] How to use Docker repository
How to use ModelMapper (Spring boot)
How to use classes in Java?
How to call and use API in Java (Spring Boot)
[Tutorial] Spring Batch
I tried Spring Batch
How to use In-Memory Job repository in Spring Batch
Loop step in Spring Batch
Things to consider when running a specified job using Spring Batch
Multilingual Locale in Java How to use Locale
How to use custom helpers in rails
How to use named volume in docker-compose.yml
How to include Spring Tool in Eclipse 4.6.3?
How to use MySQL in Rails tutorial
How to set and use profile in annotation-based Configuration in Spring framework
How to run a job with docker login in AWS batch
How to use environment variables in RubyOnRails
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to use ExpandableListView in Android Studio
[Rails] How to use select boxes in Ransack
How to use built-in h2db with spring boot
How to use "sign_in" in integration test (RSpec)
How to use Spring Boot session attributes (@SessionAttributes)
How to add a classpath in Spring Boot
How to use JQuery in js.erb of Rails6
How to bind to property file in Spring Boot
How to define multiple orm.xml in Spring4, JPA2.1
[Rails] How to use PostgreSQL in Vagrant environment
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
Use Interceptor in Spring
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Ruby] How to use standard output in conditional branching
How to use Struts2 * Spring Framework (Spring plugin) June 2017 Version
How to use Z3 library in Scala with Eclipse
How to create a Spring Boot project in IntelliJ
Understand how to use Swift's JSON Decoder in 3 minutes
How to use JDD library in Scala with Eclipse
How to test file upload screen in Spring + Selenium
Notes on how to use regular expressions in Java
[Java] How to use Map
How to use Chain API