1
2

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 1 year has passed since last update.

Spring Batch 複数job作成時の実行メモ

Last updated at Posted at 2023-06-19

Spling Batch で複数Job定義したときの実行job指定方法

ソースは公式のサンプルを使用

Job定義

BatchConfiguration.java
	@Bean
	Job firstJob(JobRepository jobRepository,
			JobCompletionNotificationListener listener, Step step1) {
		return new JobBuilder("firstJob", jobRepository)
				.incrementer(new RunIdIncrementer())
				.listener(listener)
				.flow(step1)
				.end()
				.build();
	}

	@Bean
	Job secondJob(JobRepository jobRepository,
			JobCompletionNotificationListener listener, Step step1) {
		return new JobBuilder("secondJob", jobRepository)
				.incrementer(new RunIdIncrementer())
				.listener(listener)
				.flow(step1)
				.end()
				.build();
	}

このまま実行するとエラーになる

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobLauncherApplicationRunner': Invocation of init method failed
・・・
Caused by: java.lang.IllegalArgumentException: Job name must be specified in case of multiple jobs

Job指定

下記の"18.12.2. Running Spring Batch Jobs on Startup"に記載のとおり、実行するJobは、"spring.batch.job.name"に指定する。

application.properties
spring.batch.job.name=firstJob

※application.propertiesはcomplete/src/main/resourcesに配置

引数にJob指定

起動時の引数にも設定可

--spring.batch.job.name="secondJob"

複数Job実行指定

今は1回の実行でひとつのJobになってるみたい...

application.properties
spring.batch.job.name=firstJob, secondJob

※実行時の順番はプロパティファイルに記載した通りになるわけではないらしいので注意、Spring Batch では制御できないらしい

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?