Executionとは? (SpringBatch用語) | Java Springの逆引きメモ

Java Springの逆引きメモ

JavaのSpring frameworkのメモを書いていきます!
初心者の勉強ノートなので間違いがあるかもしれませんが、何かヒントになることがあれば幸いです。

SpringBatchではExecutionという概念が出てきます。

これが何を表すかを簡単に見ていきたいと思います。


まず、ひとことで。


Executionは、処理の結果や、状態を保持するクラスです。



【用意されているクラス】

SpringBatchで用意されているクラスには以下のものがあります。


 ・JobExectution

    Job全体の結果とStepExecutionを持ちます。

 

 ・StepExecution

    1つのStepにつき1つのStepExecutionを作成します。

    開始の状態と終了時の状態だけを保持します。

    Stepは途中で失敗した場合、自動でリトライすることもできますが、

    このときもStepExecutionは、リトライ毎の状態をすべて持つのではなく、

    1つの終了状態だけを持ちます。

    


  <イメージ>
Java Springの逆引きメモ

 ※idは、JobInstanceの識別子を表しています。

  詳しくは、・SpringBatch機能について を参照。



【誰が生成するのか?】

上記のExecutionはJobなどのSpringBatchの機能が勝手に生成してくれるので

プログラマーが生成に関して特に気にする場面は少ないかと思います。


ちなみに、Executionはリポジトリから取得します。

リポジトリはたいていDBからExecutionの情報を取得します。

(保存先、取得先は使用するリポジトリによって変わります。)

取得先にまだ生成されていなければ生成します。



【Executionの利用のされ方】

では、何に利用するのでしょうか?

SpringBatchにはリトライ機能や、処理結果による条件分岐などの機能があります。

これは処理結果が分からないと処理できない機能です。

実はこのような機能で使用することになります。


例えば、StepExecutionは開始の状態と終了の状態しかもっていません。

Step処理の途中途中の細かな情報は持っていないのです。

しかし、あるStep処理が途中でエラーが発生してしまったとします。

この場合、どこまで終了しているかさえ分かればリトライ処理が実行できます。

つまり、終了の状態さえ知っていれば十分と言うことになります。


条件分岐の機能も終了の状態さえ知っていれば処理できます。


ですので、StepExecutionは、開始と終了の状態しか持っていないようです。




【その他】
上記でIDや状態を保持するのがExecutionだと説明しましたが、もうひとつ記憶しておいた方がよいことがあります。

ExecutionはExcecutionContextを持っています。

ExcecutionContextは、データをリポジトリに保存できるMapです。

リポジトリに保存できると言うことは、実行中はもちろんのこと、バッチ終了後も保持され、参照・更新ができることになります。

SpringBatchが途中でFAILして終了しrestartしたいときにどこまで処理をしているかを保存しておきたい場合や、他のStepで作成した値を使いたい場合などに便利です。


ExcecutionContextはJobExecution、StepExecution毎に1つ持っています。

Step内だけで使用する場合はStepExecution内のExecutionContextを使用し、

Step間をまたぐような値の場合はJobExecution内のExecutionContextを使用しましょう。

上でも書きましたが、JobExecutionは起動のたびに新規作成されます(違うExecutionのIDで)。

つまり、1つのJobInstance(パラメタが同じ起動)に対しても、複数のJobExecutionが存在します。

(同じパラメタでも-restart, -nextで起動できるので何回でも起動できるので)
その点は頭においておき、気をつけましょう。


【参考】

StepExecution クラスのプロパティ

status A BatchStatus object that indicates the status of the execution. While it's running, the status is BatchStatus.STARTED, if it fails, the status is BatchStatus.FAILED, and if it finishes successfully, the status is BatchStatus.COMPLETED
startTime A java.util.Date representing the current system time when the execution was started.
endTime A java.util.Date representing the current system time when the execution finished, regardless of whether or not it was successful.
exitStatus The ExitStatus indicating the result of the execution. It is most important because it contains an exit code that will be returned to the caller. See chapter 5 for more details.
executionContext The 'property bag' containing any user data that needs to be persisted between executions.
readCount The number of items that have been successfully read
writeCount The number of items that have been successfully written
commitCount The number transactions that have been committed for this execution
rollbackCount The number of times the business transaction controlled by the Step has been rolled back.
readSkipCount The number of times read has failed, resulting in a skipped item.
processSkipCount The number of times process has failed, resulting in a skipped item.
filterCount The number of items that have been 'filtered' by the ItemProcessor.
writeSkipCount The number of times write has failed, resulting in a skipped item.



参考:

・SpringBatch機能について

・実際に作成するものとは(Stepの概念について)

・CommandLineJobRunnerとは?

・自作クラスをrestartできるようにするには?