kagamihogeの日記

kagamihogeの日記です。

jBatch(JSR-352) on Java SEでhello world的なことやる

設定など

Java Batch: Wiki: RI_TCK — Project Kenaiからjsr352-SE-RI-1.0.zipをダウンロードする。解凍後のjarファイルにパスを通す。

  • derby.jar
  • javax.inject.jar
  • jsr352-SE-RI-javadoc.jar
  • javax.batch.api.jar
  • jsr352-RI-spi.jar
  • jsr352-SE-RI-runtime.jar

コード

ジョブXML

META-INF\batch-jobs\sample-job.xmlを作成し、batchletを一つだけ持つjobを一つだけ作成する。

<?xml version="1.0" encoding="UTF-8"?>
<job id="sample-job"
     xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     version="1.0">
     
     <step id="sample-batchlet">
         <batchlet ref="jbatchsample.SampleBatchlet" />
     </step>
</job>
batchlet

開始されると"## start"とだけ表示する。stopは、jobが外から停止されたときに呼ばれるみたいなので今回は出番無い。

package jbatchsample;

import javax.batch.api.Batchlet;

public class SampleBatchlet implements Batchlet {

    @Override
    public String process() throws Exception {
        System.out.println("## start");
        return null;
    }

    @Override
    public void stop() throws Exception {
        System.out.println("## stop");
    }

}
jobの開始

ジョブXMLで定義した、sample-jobというidのjobを開始する。

package jbatchsample;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;

public class Main {

    public static void main(String[] args) {
        JobOperator job = BatchRuntime.getJobOperator();
        
        long id = job.start("sample-job", null);
        System.out.println("id = " + id);
    }

}

実行結果

初回のみバッチ管理用テーブルのCREATE TABLE文が実行される。

3 30, 2014 2:35:01 午後 com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl createIfNotExists
情報: CHECKPOINTDATA table does not exists. Trying to create it.
3 30, 2014 2:35:02 午後 com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl createIfNotExists
情報: JOBINSTANCEDATA table does not exists. Trying to create it.
3 30, 2014 2:35:02 午後 com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl createIfNotExists
情報: EXECUTIONINSTANCEDATA table does not exists. Trying to create it.
3 30, 2014 2:35:02 午後 com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl createIfNotExists
情報: STEPEXECUTIONINSTANCEDATA table does not exists. Trying to create it.
3 30, 2014 2:35:02 午後 com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl createIfNotExists
情報: JOBSTATUS table does not exists. Trying to create it.
3 30, 2014 2:35:03 午後 com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl createIfNotExists
情報: STEPSTATUS table does not exists. Trying to create it.
id = 1
## start

管理用テーブル

jobの開始終了時間などを持っていると思われる管理用テーブルを見てみる。

$ %JAVA_HOME%\db\bin\ij
ij バージョン 10.8
ij> connect 'jdbc:derby:C:\Java\workspaces\eclipse432\defaults\jbatchsample\RUNTIMEDB';
ij> show tables;
TABLE_SCHEM         |TABLE_NAME                    |REMARKS
------------------------------------------------------------------------
(中略)
SYSIBM              |SYSDUMMY1                     |
JBATCH              |CHECKPOINTDATA                |
JBATCH              |EXECUTIONINSTANCEDATA         |
JBATCH              |JOBINSTANCEDATA               |
JBATCH              |JOBSTATUS                     |
JBATCH              |STEPEXECUTIONINSTANCEDATA     |
JBATCH              |STEPSTATUS                    |

28 行が選択されました

ij> select jobexecid, starttime, endtime, batchstatus, exitstatus from jbatch.ex
ecutioninstancedata;
JOBEXECID           |STARTTIME                    |ENDTIME
|BATCHSTATUS
                                                 |EXITSTATUS


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------
1                   |2014-03-30 14:35:03.939      |2014-03-30 14:35:04.098
|COMPLETED
                                                 |COMPLETED


2                   |2014-03-30 14:48:10.753      |2014-03-30 14:48:10.937
|COMPLETED
                                                 |COMPLETED



2 行が選択されました
ij>