2
3

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のRunIdIncrementerはジョブパラメータを今回未指定なら前回を引き継ぐ

Posted at

spring-batchのRunIdIncrementerは、もし前回実行時のパラメータがあり、今回はそれが未指定であれば、前回実行時のパラメータを使用する。

動作確認

動作確認は以下のとおり。

まずパラメータを指定して実行する。

java -jar springbatchsample.jar sample.param=value001

以下のようにログにrun.id=2, sample.param=value001と表示される。

Job: [SimpleJob: [name=myJobparam001]] launched with the following parameters: [{run.id=2, sample.param=value001}]

次に、パラメータを削除して実行する。

java -jar springbatchsample.jar

以下のようにログはrun.id=3, sample.param=value001のようになる。run.idはインクリメントされ、sample.paramは未指定なのに前回実行時のものが残っている。

Job: [SimpleJob: [name=myJobparam001]] launched with the following parameters: [{run.id=3, sample.param=value001}]

ちなみに、以下のようにパラメータを空文字にすれば、空文字になる。

java -jar springbatchsample.jar sample.param=
Job: [SimpleJob: [name=myJobparam001]] launched with the following parameters: [{run.id=4, sample.param=}]

RunIdIncrementer

なぜこのような挙動になるか。該当箇所のソースは以下の箇所。

	@Override
	public JobParameters getNext(@Nullable JobParameters parameters) {

		JobParameters params = (parameters == null) ? new JobParameters() : parameters;

		long id = params.getLong(key, new Long(0)) + 1;
		return new JobParametersBuilder(params).addLong(key, id).toJobParameters();
	}

引数のparametersは前回実行時のパラメータが入ってくる。ここに前回のrun.idが含まれるのでインクリメントされる。そして、今回のジョブパラメータは前回のパラメータをベース(new JobParametersBuilder(params))にする。なので、前回指定したパラメータで、かつ、今回未指定であれば、前回指定時のパラメータがそのまま使われることになる。

この挙動が合わないなら自前のJobParametersIncrementer作ってね、という事なのかな、と。それと、spring-batchはジョブパラメータが同一なら同一ジョブ実行と見なす仕様なので、あるジョブ実行だけパラメータ未指定にする、という使い方は仕様に合わないのかも、とも思う。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?