9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring BatchでIn-MemoryのJobリポジトリを利用する方法

Last updated at Posted at 2017-11-22

Spring Batchは、デフォルトでDBベースのJobリポジトリを利用するため、勝手にJob管理用のテーブルが作られます。テーブルのCreate権限がないDBユーザの場合やパフォーマンスが気になる場合、In-Memory Jobリポジトリを利用することが可能です。
但し、In-MemoryのJobリポジトリの場合、再起動などの機能が利用できなくなりますので、使うかどうかは非機能要件次第になります。

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.

##解決方法1
一番簡単な解決方法は、DataSourceの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 Annotation Type EnableBatchProcessingの説明

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

##解決方法2
DataSourceがAutoConfigの場合でも、JobRepository、JobExplorer、JobLauncherをOverrideすることで、MapベースのJobRepositoryを利用することが可能です。


@Configuration
@EnableBatchProcessing
public class BatchConfig {

	......
    
	/**
	 * in-memoryジョブリポジトリを使用する
	 * 
	 * @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;
			}
		};
	}
}

メタデータテーブルを生成させないために、application.propertiesを以下のように改修する。

application.properties
spring.batch.initializer.enabled=false

参考資料:
https://blog.ik.am/entries/409
https://qiita.com/blackawa/items/e9eaa254cbe27e257e10

9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?