Kohei Nozaki's blog 

StepContext#persistentUserDataで遊ぶ


Posted on Sunday Feb 16, 2014 at 07:43AM in Technology


StepContext#persistentUserDataにはStepに紐づくデータを保存しておける。ここに入れたデータはJobRepositoryに保存され、後続のStepやジョブの再実行時に参照できる。何かしら使い道があると思われるので若干遊んでみる

環境・前提条件

関連メソッドの仕様を見てみる

[1]から引いてみる

javax.batch.runtime.context.StepContextインタフェース

Stepに関連する操作をする時に使うインタフェース。artifactで@Injectで注入して使える。

    /**
     * The getPersistentUserData method returns a persistent data object 
     * belonging to the current step. The user data type must implement 
     * java.util.Serializable. This data is saved as part of a step's 
     * checkpoint. For a step that does not do checkpoints, it is saved 
     * after the step ends. It is available upon restart. 
     * @return user-specified type
     */
    public Serializable getPersistentUserData();

    /**
     * The setPersistentUserData method stores a persistent data object 
     * into the current step. The user data type must implement 
     * java.util.Serializable. This data is saved as part of a step's 
     * checkpoint. For a step that does not do checkpoints, it is saved 
     * after the step ends. It is available upon restart. 
     * @param data is the user-specified type
     */ 
    public void setPersistentUserData(Serializable data);

javax.batch.runtime.StepExecutionインタフェース

Stepの実行履歴的な情報を含むインタフェース。JobOperatorを使って引っ張って来れたり、Deciderのメソッドの引数で受け取れたりする。

    /**
     * Get user persistent data
     * @return persistent data 
     */
    public Serializable getPersistentUserData();

先行StepでセットしたpersistentUserDataを後続Stepで参照してみる

サンプルの仕様

  • 第一レベルの要素は動作順に以下
    1. set (setPersistentUserDataBatchlet)
    2. get (getPersistentUserDataBatchlet)
  • setでセットしたpersistentUserDataをgetで参照してみる

資源

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

動かしてみる

ログ

08:52:02,642 FINE  [org.nailedtothex.jbatch.example.persistentuserdata.SetPersistentUserDataBatchlet] (batch-batch - 4) set: process()
08:52:02,649 FINE  [org.nailedtothex.jbatch.example.persistentuserdata.GetPersistentUserDataBatchlet] (batch-batch - 4) get: process()
08:52:02,650 FINE  [org.nailedtothex.jbatch.example.persistentuserdata.GetPersistentUserDataBatchlet] (batch-batch - 4) stepExecution: stepName=set, persistentUserData=* my step name is set *
08:52:02,650 FINE  [org.nailedtothex.jbatch.example.persistentuserdata.GetPersistentUserDataBatchlet] (batch-batch - 4) stepExecution: stepName=get, persistentUserData=null

setステップで設定した「 my step name is set 」がgetステップから見えている(3行目)。うむ

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 
----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+------------+---------------+-----------------
            131 |           126 |         | 2014-02-16 08:52:02.637 | 2014-02-16 08:52:02.637 | 2014-02-16 08:52:02.653 | 2014-02-16 08:52:02.653 | COMPLETED   | COMPLETED  |               | 
(1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid = 131 order by stepexecutionid;
 stepexecutionid | jobexecutionid | version | stepname |        starttime        |         endtime         | batchstatus | exitstatus | executionexception |                       persistentuserdata                       | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo 
-----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+----------------------------------------------------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+----------------------
             153 |            131 |         | set      | 2014-02-16 08:52:02.639 | 2014-02-16 08:52:02.642 | COMPLETED   | COMPLETED  |                    | \xaced00057400172a206d792073746570206e616d6520697320736574202a |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
             154 |            131 |         | get      | 2014-02-16 08:52:02.645 | 2014-02-16 08:52:02.65  | COMPLETED   | COMPLETED  |                    |                                                                |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
(2 rows)

persistentuserdataカラムに何かが入っている。うむ

前回実行時のデータを再実行時に参照してみる

サンプルの仕様

  • Stepは1つだけ
  • 何もpersistentUserDataに入ってなかったら
    1. persistentUserDataにデータを入れる
    2. 例外投げて死ぬ
  • 何かpersistentUserDataに入っていたら
    1. 内容を表示して終了
  • 初回実行はpersistentUserDataにデータ入れてFAILEDになる
  • 再実行時はpersistentUserDataのデータを表示してCOMPLETEDになる

資源

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

動かしてみる

ログ

09:15:07,338 FINE  [org.nailedtothex.jbatch.example.persistentuserdata.GetAndSetPersistentUserDataBatchlet] (batch-batch - 3) getAndSet: process()
09:15:07,339 WARN  [org.jberet] (batch-batch - 3) JBERET000001: Failed to run batchlet org.jberet.job.model.RefArtifact@7e76d151: java.lang.RuntimeException: to confirm whether the data is visible or not when restart
    at org.nailedtothex.jbatch.example.persistentuserdata.GetAndSetPersistentUserDataBatchlet.process(GetAndSetPersistentUserDataBatchlet.java:31) [classes:]
    at org.jberet.runtime.runner.BatchletRunner.run(BatchletRunner.java:61) [jberet-core-1.0.1.Beta-SNAPSHOT.jar:1.0.1.Beta-SNAPSHOT]
    at org.jberet.runtime.runner.StepExecutionRunner.runBatchletOrChunk(StepExecutionRunner.java:207) [jberet-core-1.0.1.Beta-SNAPSHOT.jar:1.0.1.Beta-SNAPSHOT]
    at org.jberet.runtime.runner.StepExecutionRunner.run(StepExecutionRunner.java:131) [jberet-core-1.0.1.Beta-SNAPSHOT.jar:1.0.1.Beta-SNAPSHOT]
    at org.jberet.runtime.runner.CompositeExecutionRunner.runStep(CompositeExecutionRunner.java:162) [jberet-core-1.0.1.Beta-SNAPSHOT.jar:1.0.1.Beta-SNAPSHOT]
    at org.jberet.runtime.runner.CompositeExecutionRunner.runFromHeadOrRestartPoint(CompositeExecutionRunner.java:88) [jberet-core-1.0.1.Beta-SNAPSHOT.jar:1.0.1.Beta-SNAPSHOT]
    at org.jberet.runtime.runner.JobExecutionRunner.run(JobExecutionRunner.java:58) [jberet-core-1.0.1.Beta-SNAPSHOT.jar:1.0.1.Beta-SNAPSHOT]
    at org.wildfly.jberet.services.BatchEnvironmentService$WildFlyBatchEnvironment$1.run(BatchEnvironmentService.java:149) [wildfly-jberet-8.0.0.Final.jar:8.0.0.Final]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [rt.jar:1.7.0_51]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [rt.jar:1.7.0_51]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_51]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_51]
    at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_51]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122)

09:15:08,351 FINE  [org.nailedtothex.jbatch.example.persistentuserdata.GetAndSetPersistentUserDataBatchlet] (batch-batch - 2) getAndSet: process()
09:15:08,351 FINE  [org.nailedtothex.jbatch.example.persistentuserdata.GetAndSetPersistentUserDataBatchlet] (batch-batch - 2) persistentUserData=* my job execution id is 134 *

初回実行時に入れた「 my job execution id is 134 」が再実行時に見えている。うむ

Repository

job_execution
jbatch=# select * from job_execution where jobexecutionid in (134, 135) order by jobexecutionid;
 jobexecutionid | jobinstanceid | version |       createtime        |        starttime        |         endtime         |     lastupdatedtime     | batchstatus | exitstatus | jobparameters | restartposition 
----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+------------+---------------+-----------------
            134 |           128 |         | 2014-02-16 09:15:07.335 | 2014-02-16 09:15:07.335 | 2014-02-16 09:15:07.343 | 2014-02-16 09:15:07.343 | FAILED      | FAILED     |               | 
            135 |           128 |         | 2014-02-16 09:15:08.347 | 2014-02-16 09:15:08.347 | 2014-02-16 09:15:08.354 | 2014-02-16 09:15:08.354 | COMPLETED   | COMPLETED  |               | 
(2 rows)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (134, 135) order by jobexecutionid, stepexecutionid;
 stepexecutionid | jobexecutionid | version | stepname  |        starttime        |         endtime         | batchstatus | exitstatus |                                                                   executionexception                                                                   |                              persistentuserdata                              | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo 
-----------------+----------------+---------+-----------+-------------------------+-------------------------+-------------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+----------------------
             157 |            134 |         | getAndSet | 2014-02-16 09:15:07.336 | 2014-02-16 09:15:07.34  | FAILED      | FAILED     | java.lang.RuntimeException: to confirm whether the data is visible or not when restart                                                                +| \xaced000574001e2a206d79206a6f6220657865637574696f6e20696420697320313334202a |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
                 |                |         |           |                         |                         |             |            |         at org.nailedtothex.jbatch.example.persistentuserdata.GetAndSetPersistentUserDataBatchlet.process(GetAndSetPersistentUserDataBatchlet.java:31)+|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.jberet.runtime.runner.BatchletRunner.run(BatchletRunner.java:61)                                                                       +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.jberet.runtime.runner.StepExecutionRunner.runBatchletOrChunk(StepExecutionRunner.java:207)                                             +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.jberet.runtime.runner.StepExecutionRunner.run(StepExecutionRunner.java:131)                                                            +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.jberet.runtime.runner.CompositeExecutionRunner.runStep(CompositeExecutionRunner.java:162)                                              +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.jberet.runtime.runner.CompositeExecutionRunner.runFromHeadOrRestartPoint(CompositeExecutionRunner.java:88)                             +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.jberet.runtime.runner.JobExecutionRunner.run(JobExecutionRunner.java:58)                                                               +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.wildfly.jberet.services.BatchEnvironmentService$WildFlyBatchEnvironment$1.run(BatchEnvironmentService.java:149)                        +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)                                                                    +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at java.util.concurrent.FutureTask.run(FutureTask.java:262)                                                                                   +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)                                                            +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)                                                            +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at java.lang.Thread.run(Thread.java:744)                                                                                                      +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |         at org.jboss.threads.JBossThread.run(JBossThread.java:122)                                                                                    +|                                                                              |           |            |             |               |               |                  |             |                |                      | 
                 |                |         |           |                         |                         |             |            |                                                                                                                                                        |                                                                              |           |            |             |               |               |                  |             |                |                      | 
             158 |            135 |         | getAndSet | 2014-02-16 09:15:08.349 | 2014-02-16 09:15:08.352 | COMPLETED   | COMPLETED  |                                                                                                                                                        | \xaced000574001e2a206d79206a6f6220657865637574696f6e20696420697320313334202a |         0 |          0 |           0 |             0 |             0 |                0 |           0 |              0 |                      | 
(2 rows)

備考

  • 再実行時にはpersistentUserDataがコピーされるようなので、データ量がでかい場合は気をつけた方がいいのかも

参考文献

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



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed