Kohei Nozaki's blog 

ExitStatusでフロー制御してみる


Posted on Sunday Feb 16, 2014 at 08:17AM in Technology


ExitStatusでフロー制御的なことをして遊んでみる

環境・前提条件

仕様を見てみる

[1]から引いてみる

8.6 Transition Elements

Transition elements may be specified in the containment scope of a step, flow, split, or decision to direct job execution sequence or to terminate job execution. There are four transition elements:

  1. next - directs execution flow to the next execution element.
  2. fail - causes a job to end with FAILED batch status.
  3. end - causes a job to end with COMPLETED batch status.
  4. stop - causes a job to end with STOPPED batch status.

Fail end, and stop are considered “terminating elements” because they cause a job execution to terminate.

この4つを使って遊んでみる

サンプルの仕様

  • 第一レベルの要素は以下
    1. step1
    2. step2
  • どちらも参照するartifactは同じExitStatusBatchlet
    • パラメータで与えたExitStatusで終わるだけ
    • step1のExitStatusはジョブパラメータで変えられるようにしてある
    • step2のExitStatusはnull(COMPLETED)固定
  • step1には先で引いたTransition Elementsを4つ指定してある
  • 4つのテストメソッドでstep1のExitStatusを以下のパターンで変えてテストする
    • next(): step2に遷移する
    • fail(): step1で異常終了
    • end(): step1で正常終了
    • stop(): step1で停止

資源

資源はこのへんにまとめて全部ある

動かしてみる

next

ログ

10:29:46,990 FINE  [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 6) step1: exitStatus=NEXT
10:29:47,009 FINE  [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 6) step2: exitStatus=null

Repository

job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1;
 jobexecutionid | jobinstanceid | version |       createtime        |        starttime        |         endtime         |     lastupdatedtime     | batchstatus | exitstatus |   jobparameters   | restartposition 
----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+------------+-------------------+-----------------
            148 |           141 |         | 2014-02-16 10:29:46.976 | 2014-02-16 10:29:46.976 | 2014-02-16 10:29:47.011 | 2014-02-16 10:29:47.011 | COMPLETED   | COMPLETED  | exitStatus = NEXT+| 
                |               |         |                         |                         |                         |                         |             |            |                   | 
(1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (148) order by jobexecutionid, stepexecutionid;
 stepexecutionid | jobexecutionid | version | stepname |        starttime        |         endtime         | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo 
-----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+----------------------
             171 |            148 |         | step1    | 2014-02-16 10:29:46.982 | 2014-02-16 10:29:46.991 | COMPLETED   | NEXT       |                    |                    |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
             172 |            148 |         | step2    | 2014-02-16 10:29:47.008 | 2014-02-16 10:29:47.009 | COMPLETED   | COMPLETED  |                    |                    |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
(2 rows)
  • 普通にstep2に遷移している

fail

ログ

10:41:31,341 FINE  [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 9) step1: exitStatus=FAIL

Repository

job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1;
 jobexecutionid | jobinstanceid | version |       createtime        |        starttime        |         endtime         |     lastupdatedtime     | batchstatus |        exitstatus         |   jobparameters   | restartposition 
----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+---------------------------+-------------------+-----------------
            149 |           142 |         | 2014-02-16 10:41:31.338 | 2014-02-16 10:41:31.338 | 2014-02-16 10:41:31.344 | 2014-02-16 10:41:31.344 | FAILED      | EARLY COMPLETION (FAILED) | exitStatus = FAIL+| 
                |               |         |                         |                         |                         |                         |             |                           |                   | 
(1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (149) order by jobexecutionid, stepexecutionid;
 stepexecutionid | jobexecutionid | version | stepname |        starttime        |         endtime         | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo 
-----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+----------------------
             173 |            149 |         | step1    | 2014-02-16 10:41:31.339 | 2014-02-16 10:41:31.341 | COMPLETED   | FAIL       |                    |                    |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
(1 row)
  • step1終了後に異常終了している
  • step_executionのbatchstatusがCOMPLETEDなのが気になるけどjob_executionのbatchstatusはちゃんとFAILEDになっている

end

ログ

10:43:54,134 FINE  [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 3) step1: exitStatus=END

Repository

job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1;
 jobexecutionid | jobinstanceid | version |       createtime        |        starttime        |         endtime         |     lastupdatedtime     | batchstatus |       exitstatus       |  jobparameters   | restartposition 
----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+------------------------+------------------+-----------------
            150 |           143 |         | 2014-02-16 10:43:54.129 | 2014-02-16 10:43:54.129 | 2014-02-16 10:43:54.137 | 2014-02-16 10:43:54.137 | COMPLETED   | EARLY COMPLETION (END) | exitStatus = END+| 
                |               |         |                         |                         |                         |                         |             |                        |                  | 
(1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (150) order by jobexecutionid, stepexecutionid;
 stepexecutionid | jobexecutionid | version | stepname |       starttime        |         endtime         | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo 
-----------------+----------------+---------+----------+------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+----------------------
             174 |            150 |         | step1    | 2014-02-16 10:43:54.13 | 2014-02-16 10:43:54.134 | COMPLETED   | END        |                    |                    |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
(1 row)
  • step2には遷移せず正常終了している

stop

ログ

10:46:52,718 FINE  [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 2) step1: exitStatus=STOP

Repository

job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1;
 jobexecutionid | jobinstanceid | version |       createtime        |        starttime        |        endtime         |    lastupdatedtime     | batchstatus |       exitstatus        |   jobparameters   | restartposition 
----------------+---------------+---------+-------------------------+-------------------------+------------------------+------------------------+-------------+-------------------------+-------------------+-----------------
            151 |           144 |         | 2014-02-16 10:46:52.705 | 2014-02-16 10:46:52.705 | 2014-02-16 10:46:52.72 | 2014-02-16 10:46:52.72 | STOPPED     | EARLY COMPLETION (STOP) | exitStatus = STOP+| step2
                |               |         |                         |                         |                        |                        |             |                         |                   | 
(1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (151) order by jobexecutionid, stepexecutionid;
 stepexecutionid | jobexecutionid | version | stepname |        starttime        |         endtime         | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo 
-----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+----------------------
             175 |            151 |         | step1    | 2014-02-16 10:46:52.712 | 2014-02-16 10:46:52.718 | COMPLETED   | STOP       |                    |                    |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
(1 row)
  • step1終了後に停止している
  • step_executionのbatchstatusがCOMPLETEDなのが気になるけどjob_executionのbatchstatusはちゃんとSTOPPEDになっている

stop要素のrestart属性で再実行時に遷移するstepを指定してみる

仕様を引いてみる

[1]の「8.6.4 Stop Element」に、restart属性についてこう書いてある

Specifies the job-level step, flow, or split at which to restart when the job is restarted. It must be a valid XML string value. This is a required attribute.

再実行してみる

テストメソッドstopRestart()に再実行のテストを書いてあるので実行してみる

ログ

10:58:59,039 FINE  [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 4) step1: exitStatus=STOP
10:59:00,053 WARN  [org.jberet] (batch-batch - 6) JBERET000018: Could not find the original step execution to restart.  Current step execution id: 0, step name: step2
10:59:00,054 FINE  [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 6) step2: exitStatus=null

Repository

job_execution
jbatch=# select * from job_execution where jobexecutionid in (161, 162) order by jobexecutionid;
 jobexecutionid | jobinstanceid | version |       createtime        |        starttime        |         endtime         |     lastupdatedtime     | batchstatus |       exitstatus        |   jobparameters   | restartposition 
----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+-------------------------+-------------------+-----------------
            161 |           151 |         | 2014-02-16 10:58:59.035 | 2014-02-16 10:58:59.035 | 2014-02-16 10:58:59.042 | 2014-02-16 10:58:59.042 | STOPPED     | EARLY COMPLETION (STOP) | exitStatus = STOP+| step2
                |               |         |                         |                         |                         |                         |             |                         |                   | 
            162 |           151 |         | 2014-02-16 10:59:00.05  | 2014-02-16 10:59:00.05  | 2014-02-16 10:59:00.056 | 2014-02-16 10:59:00.056 | COMPLETED   | COMPLETED               | exitStatus = STOP+| 
                |               |         |                         |                         |                         |                         |             |                         |                   | 
(2 rows)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (161, 162) order by jobexecutionid, stepexecutionid;
 stepexecutionid | jobexecutionid | version | stepname |        starttime        |         endtime         | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo 
-----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+----------------------
             186 |            161 |         | step1    | 2014-02-16 10:58:59.037 | 2014-02-16 10:58:59.04  | COMPLETED   | STOP       |                    |                    |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
             187 |            162 |         | step2    | 2014-02-16 10:59:00.051 | 2014-02-16 10:59:00.054 | COMPLETED   | COMPLETED  |                    |                    |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
(2 rows)
  • 一応予想通り動いたけど、ログにWARNで出ている文言が気になる
  • 再実行時に初回実行時に実行してないStepから再開するっていうのは仕様上微妙なのかも

備考

  • on属性にはワイルドカード(*と?)が使える
  • next on=“*” とかよく使いそう
  • endは正常系で後続処理やらなくて良い時に使う感じ。非営業日だから何もせずに終わりとか(営業日関連はジョブ内でやらずに、ジョブ呼び出すところを作り込んでジョブが動く前に止めた方がいい気がするけど)
  • failは異常系の時(例外投げてもいい気がするけど)
  • stopは再実行前提で止めたい時とか、再実行するstepを制御したい時に使う?どういう状況で使うのかいまいち想像できないけど
  • 先行処理の状況から分岐させたいときは、StepExecutionの配列を受け取ってExitStatusを決められるDeciderを使うのが良い

参考文献

  1. JSR-000352 Batch Applications for the Java Platform - Final Release



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed