Upload
trinhthuan
View
217
Download
4
Embed Size (px)
Citation preview
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 1
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 2
Batch Application for the Java Platform – JSR352
NTTコムウェア株式会社 技術SE部 OSS・AP技術 担当 上妻 宜人
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 3
自己紹介
上妻 宜人 (あげつま のりと)
NTTコムウェア株式会社 勤務
社内のJava/APサーバの技術サポートを担当しています
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 4
バッチってなんだろう?
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 5
月次締め 処理
銀行 勘定システム
在庫管理
深夜に起動(非対話型) 計算量が多い
ジョブ 順序制御 並列実行
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 6
月次締め 処理
銀行 勘定システム
在庫管理
深夜に起動(非対話型) 計算量が多い
ジョブ 順序制御 並列実行
多くの業種にバッチ処理 (会計 金融 流通 …)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 7
月次締め 処理
銀行 勘定システム
在庫管理
深夜に起動(非対話型) 計算量が多い
ジョブ 順序制御 並列実行
業種は違えど、 処理の背景は同じ
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 8
月次締め 処理
銀行 勘定システム
在庫管理
深夜に起動(非対話型) 計算量が多い
ジョブ 順序制御 並列実行
バッチに求められる機能も 業種をまたがって同じ
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 9
異なる業種が
類似の処理の背景を持って、
同じような機能を作る
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 10
D R Y !!! 同じことを何度も繰り返すべからず
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 11
標準化しよう
IBM (Spec Lead)
Oracle (Expert Group Member)
RedHat (同)
VMWare(同)
Credit Suisse (同)
ユーザ企業によるJava貢献 http://mi9.com/get-together_232.html
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 12
JSR352
Batch Application for
The Java Platform
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 13
Portable
Extensions
JSP 2.2
Servlet 3.1
Concurrency
Utilities for EE
Batch
Applications
(JSR 352)
Java API for JSON
(JSR-353)
Java API for
WebSocket
(JSR-355)
JSP2.2 JSP2.2 CDI 1.1
Managed Beans 1.0 EJB 3.2
Connector
1.0 JPA 2.1 JTA 1.1 JMS 2.0
JSF 2.2 JAX-RS
2.0 EL 3.0
が Java EE 7 で新たに盛り込まれる仕様
jBatch は Java EE 7 へ
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 14
正式な仕様名は (Batch Application for the java Platform)
とても長いので、
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 15
ここからは jBatch で.
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 16 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Program Agenda
jBatch の概要
アーキテクチャ
– ユーザは何を実装するのか
– Chunk方式処理の流れ
Spring Batch との違い
Java SE / EE の違い
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 17
jBatch の概要
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 18
jBatch って何が仕様化されているの?
どんなことができるの?
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 19
JobOperator Job Step
JobRepository
ItemReader
ItemProcessor
ItemWriter
1. バッチ用語と関連性の定義 (DSL)
2. ジョブXMLの定義
<job id="samplejob">
<step id="step1"/>
</job>
3. APIの定義
public interface ItemReader {
Object readItem(Object item);
}
jBatch仕様 3つの構成
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 20
2. ジョブXMLの定義
<job id="samplejob">
<step id="step1"/>
</job>
3. APIの定義
public interface ItemReader {
Object readItem(Object item);
}
どのような構成で作るのか (アーキテクチャ定義)
jBatch仕様 3つの構成
JobOperator Job Step
JobRepository
ItemReader
ItemProcessor
ItemWriter
1. バッチ用語と関連性の定義 (DSL)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 21
2. ジョブXMLの定義
<job id="samplejob">
<step id="step1"/>
</job>
3. APIの定義
public interface ItemReader {
Object readItem(Object item);
}
jBatch仕様 3つの構成
JobOperator Job Step
JobRepository
ItemReader
ItemProcessor
ItemWriter
1. バッチ用語と関連性の定義 (DSL)
アーキテクチャをどうやって 実装するか(XML/API 定義)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 22
jBatchの機能1 – 順序制御
ジョブXMLで処理の順序を定義することができる。
start
STEP1 Flow1
STEP2-1 STEP2-2
Flow(グループ化)
Decision(分岐)
STEP3-1
Split
Flow
Flow
Flow
end
Split(並行実行)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 23
jBatchの機能2 – チェックポイント
デフォルトでは10レコードごとにコミットされる。
commit
▼
commit
▼
10行処理 10行処理
×
障害発生
ロールバックは最終コミットまで
途中からリスタートも可能
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 24
jBatchの機能3 – エラーハンドリング
jBatch
No. Message
001 I like GlassFish4.
002 I like Java EE 7.
999999999 Bad Record.
004 I like modern framework.
壊れたデータをスキップして処理を継続することができる。
success
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 25
jBatchの機能3 – エラーハンドリング
jBatch
No. Message
001 I like GlassFish4.
002 I like Java EE 7.
999999999 Bad Record.
004 I like modern framework.
壊れたデータをスキップして処理を継続することができる。
success
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 26
jBatchの機能3 – エラーハンドリング
jBatch
No. Message
001 I like GlassFish4.
002 I like Java EE 7.
999999999 Bad Record.
004 I like modern framework.
壊れたデータをスキップして処理を継続することができる。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 27
jBatchの機能3 – エラーハンドリング
jBatch
No. Message
001 I like GlassFish4.
002 I like Java EE 7.
999999999 Bad Record.
004 I like modern framework.
壊れたデータをスキップして処理を継続することができる。
error
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 28
jBatchの機能3 – エラーハンドリング
jBatch
No. Message
001 I like GlassFish4.
002 I like Java EE 7.
999999999 Bad Record.
004 I like modern framework.
壊れたデータをスキップして処理を継続することができる。
SKIP
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 29
jBatchの機能3 – エラーハンドリング
jBatch
No. Message
001 I like GlassFish4.
002 I like Java EE 7.
999999999 Bad Record.
004 I like modern framework.
壊れたデータをスキップして処理を継続することができる。
success
success
skip
success
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 30
ジョブスケジューラ と jBatch
ジョブ実行順序管理 JP1: ジョブネット
jBatch : ジョブ定義XML
スケジューリング (時刻・周期起動)
実行権限の管理
ジョブ実装サポート (API/例外制御)
ジョブスケジューラ (cron/JP1/Tivoliなど) jBatch
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 31
ジョブスケジューラ と jBatch
ジョブ実行順序管理 JP1: ジョブネット
スケジューリング (時刻・周期起動)
実行権限の管理
ジョブスケジューラ (cron/JP1/Tivoliなど)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 32
ジョブスケジューラ と jBatch
ジョブ実行順序管理 JP1: ジョブネット
jBatch : ジョブ定義XML
スケジューリング (時刻・周期起動)
実行権限の管理
ジョブ実装サポート (API/例外制御)
ジョブスケジューラ (cron/JP1/Tivoliなど) jBatch
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 33
ジョブスケジューラ と jBatch
ジョブ実行順序管理 JP1: ジョブネット
jBatch : ジョブ定義XML
スケジューリング (時刻・周期起動)
実行権限の管理
ジョブ実装サポート (API/例外制御)
ジョブスケジューラ (cron/JP1/Tivoliなど) jBatch
jBatchはスケジューリング機能を持っていない。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 34
ジョブスケジューラ と jBatch
ジョブ実行順序管理 JP1: ジョブネット
jBatch : ジョブ定義XML
スケジューリング (時刻・周期起動)
実行権限の管理
ジョブ実装サポート (API/例外制御)
ジョブスケジューラ (cron/JP1/Tivoliなど) jBatch
被る部分は使い分け整理する必要がある。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 35
jBatchは バッチ処理の標準APIを提供します。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 36
jBatch の位置づけ
Cron
他の Java EE 仕様と同様に、APIを提供するのが役割
JA
X-R
S 2
.0
ユーザアプリケーション
jBatch
Java EE サーバ
CDI JTA
EJB/JPA …
データベース
ファイル (CSV/XML 等)
APIの利用
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 37
jBatch を起動する
Cron
JA
X-R
S 2
.0
ユーザアプリケーション
jBatch
Java EE サーバ
CDI JTA
EJB/JPA …
データベース
ファイル (CSV/XML 等)
時刻起動
http post http://localhost:8080/batch/job/1
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 38
jBatch を起動する
Cron
JA
X-R
S 2
.0
ユーザアプリケーション
jBatch
Java EE サーバ
CDI JTA
EJB/JPA …
データベース
ファイル (CSV/XML 等)
時刻起動
http post http://localhost:8080/batch/job/1 @Path("/jobs/{jobId}")
public class BatchResource {
@POST
public void start(@PathParam("jobId") String jobId) {
// バッチジョブの起動
JobOperator jobOperator = BatchRuntime.getJobOperator();
jobOperator.start(jobId, null);
}
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 39
Java EE のコードから 簡単にバッチ起動が可能です。 (JAX-RS EJB Servlet など)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 40
アーキテクチャ
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 41
アーキテクチャ
メインフレームのバッチの移行を視野に入れた構造
JCLやCOBOL開発者にも馴染みやすい
JobOperator
Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 42
ジョブ : トップレベル要素
ジョブは1つ以上のステップから構成
ステップ全体に関わる設定はジョブに設定
リスタート可能かどうか? ジョブ設定プロパティ 等
JobOperator
Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 43
ジョブ : トップレベル要素
ジョブは1つ以上のステップから構成
ステップ全体に関わる設定はジョブに設定
リスタート可能かどうか? ジョブ設定プロパティ 等
JobOperator
Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
<job id="samplejob">
<step id="step1" next="step2" />
<step id="step2" />
<properties>
<property name="filePath" value="input.txt">
</properties>
</job>
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 44
ジョブ : トップレベル要素
ジョブは1つ以上のステップから構成
ステップ全体に関わる設定はジョブに設定
リスタート可能かどうか? ジョブ設定プロパティ 等
JobOperator
Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
<job id="samplejob">
<step id="step1" next="step2" />
<step id="step2" />
<properties>
<property name="filePath" value="input.txt">
</properties>
</job>
ジョブID. 必須属性. 一意の識別子を設定。
JobOperator.start(“samplejob”, null);
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 45
ジョブ : トップレベル要素
ジョブは1つ以上のステップから構成
ステップ全体に関わる設定はジョブに設定
リスタート可能かどうか? ジョブ設定プロパティ 等
JobOperator
Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
<job id="samplejob">
<step id="step1" next="step2" />
<step id="step2" />
<properties>
<property name="filePath" value="input.txt">
</properties>
</job>
ステップの流れを定義する。
最低1つ以上のステップが必要。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 46
ジョブ : トップレベル要素
ジョブは1つ以上のステップから構成
ステップ全体に関わる設定はジョブに設定
リスタート可能かどうか? ジョブ設定プロパティ 等
JobOperator
Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
<job id="samplejob">
<step id="step1" next="step2" />
<step id="step2" />
<properties>
<property name="filePath" value="input.txt">
</properties>
</job>
ジョブ全体で使うプロパティ。
オプション要素。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 47
ジョブインスタンスの考え方
スケジュールごとにJobInstanceが生成される
実行ごとにJobExecutionが生成される
JobInstanceは再実行に備えて永続化 (シリアライズ)
Job
JobInstance JobInstance
JobInstance JobExecution
*
*
『ファイル取り込み』 ジョブ
2013/08/22 に実行する 『ファイル取り込み』 ジョブ
2013/08/22 に実行する 『ファイル取り込み』 ジョブの1回目
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 48
ジョブインスタンスが永続化される訳
8/22 のジョブインタンスは、8/23 に再実行しても、 8/22 のデータにアクセスする。
対象データが異なる場合は、前日リスタート分と 当日分のジョブを並行処理させることも可能。
JobInstance
8月22日分のジョブ
JobExecution JobExecution
*
Date Sales amount
8/22 ¥30, 000
8/22 ¥30, 000
8/23 ¥30, 000
8/22 1回目の実行(失敗)
8/23 8/22分リスタート
再開時も8/22分の データを処理
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 49
各ステップの処理内容はユーザがコーディングする
ステップの実装方法
– chunk方式 : reader processor writer の実装
– batchlet方式 : batchletインタフェース のみ実装
JobOperator Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
ステップ : ジョブに含まれるタスク
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 50
chunk方式の登場人物
Step ItemReader ItemProcessor ItemWriter
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 51
ItemReader 実装方法
Step ItemReader ItemProcessor ItemWriter
@Named
public class MyReader implements ItemReader {
@Override
public void open(Serializable checkPoint)
throws Exception {..}
@Override
public Object readItem() throws Exception {..}
@Override
public void close() throws Exception {..}
...
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 52
ItemProcessor 実装方法
Step ItemReader ItemWriter
@Named
public class MyProcessor implements ItemProcessor {
@Override
public Object processItem(Object item)
throws Exception {..}
}
ItemProcessor
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 53
ItemWriter 実装方法
Step ItemReader
@Named
public class MyWriter implements ItemWriter {
@Override
public void open(Serializable checkPoint)
throws Exception {..}
@Override
public Object writeItems(List<Object> items)
throws Exception {..}
@Override
public void close() throws Exception {..}
...
}
ItemWriter ItemProcessor
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 54
まずはオープンする
Step ItemReader ItemProcessor ItemWriter
Open
Open
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 55
アイテムを1件読む
Step ItemReader ItemProcessor ItemWriter
Open
Open
readItem
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 56
アイテムを1件処理する
Step ItemReader ItemProcessor ItemWriter
Open
Open
readItem
processItem(item)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 57
次の1件を読みに行く
Step ItemReader ItemProcessor ItemWriter
readItem
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 58
同じく1件処理する
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 59
また読み込みを処理を行う。
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
readItem
processItem(item)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 60
デフォルトでは10回、読み込みと処理を繰り返す
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
readItem
processItem(item)
1アイテム目の
読み込みと処理
2アイテム目の
読み込みと処理
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 61
10アイテム目の処理が終わると
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
10アイテム目の
読み込みと処理
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 62
まとめて書き込み処理を行う
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
10アイテム目の
読み込みと処理
writeItems(results)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 63
chunk方式のゆえん
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
10個ずつ纏めて塊を処理しているので、 Chunk(塊)方式 と呼ばれる。
writeItems(results)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 64
書いたらcheckpointを実行
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
writeItems(results)
checkpointInfo
checkpointData
checkpointinfo
Chunkの書き出しが終わると、 コミット前にcheckpointinfoが実行される。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 65
checkpointInfoの実装
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
writeItems(results)
checkpointInfo
checkpointData
checkpointinfo
@Named
public class MyReader implements ItemReader {
@Override
public void open(Serializable checkPoint) throws Exception {..}
@Override
public Serializable checkpointInfo() throws Exception {
// 読み込んだ行数をチェックポイントにする return readedCount; // => 1
}
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 66
チェックポイントは
リスタート時に使われる。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 67
スタートとリスタートは違う
// バッチジョブの起動 JobOperator jobOperator = BatchRuntime.getJobOperator();
long jobExecId = jobOperator.start(“myjob”, null);
// ジョブのリスタート long jobExecId = jobOperator.restart(jobExecId, null);
初回起動時はjob.xmlの <job id =“myjob”> のid文字列を指定する。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 68
スタートとリスタートは違う
// バッチジョブの起動 JobOperator jobOperator = BatchRuntime.getJobOperator();
long jobExecId = jobOperator.start(“myjob”, null);
// ジョブのリスタート long jobExecId = jobOperator.restart(jobExecId, null);
リスタートするときは restart で起動する。 初回起動時に取得したジョブ実行ID(long)を 引数に指定する。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 69
スタートとリスタートは違う
// バッチジョブの起動 JobOperator jobOperator = BatchRuntime.getJobOperator();
long jobExecId = jobOperator.start(“myjob”, null);
// ジョブのリスタート long jobExecId = jobOperator.restart(jobExecId, null);
restart で起動すると、 open(Serializable cp) に checkpointInfo()で返された値が渡される。 通常の start では、nullが常に渡される。
@Named
public class MyReader implements ItemReader {
@Override
public void open(Serializable checkPoint) throws Exception {..}
@Override
public Serializable checkpointInfo() throws Exception {
return readedCount; // => 1
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 70
commitの実行
Step ItemReader ItemProcessor ItemWriter
readItem
processItem(item)
writeItems(results)
checkpointInfo
checkpointData
checkpointinfo
checkpoint処理が終わったら、 バッチランタイムで commit する。 (GlassFish であればJTAトランザクション)
▲
commit
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 71
chunk処理終了の流れ
Step ItemReader ItemProcessor ItemWriter
readItem
読み込み対象のデータがなくなった!
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 72
chunk処理終了の流れ
Step ItemReader ItemProcessor ItemWriter
readItem
ItemReaderから null を返す。 null
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 73
chunk処理終了の流れ
Step ItemReader ItemProcessor ItemWriter
readItem
処理済みのデータを書き出す。
null
writeItems(results)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 74
チェックポイントを実行する。
Step ItemReader ItemProcessor ItemWriter
readItem
null
writeItems(results)
checkpointInfo
checkpointData
checkpointinfo
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 75
トランザクションをコミットする
Step ItemReader ItemProcessor ItemWriter
readItem
null
writeItems(results)
checkpointInfo
checkpointData
checkpointinfo
▲
commit
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 76
コミット後は close 処理を行う
Step ItemReader ItemProcessor ItemWriter
▲
commit
close
close
ItemReaderとItemWriterのclose処理をして chunk方式のステップは完了。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 77
chunk方式 : job.xml の書き方
<?xml version="1.0" encoding="UTF-8"?>
<job xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0" id="job1">
<step>
<chunk item-count="100">
<reader ref="myReader"/>
<processor ref="myProcessor"/>
<writer ref="myWriter"/>
</chunk>
</step>
</job>
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 78
chunk方式 : chunkタグとオプション
<?xml version="1.0" encoding="UTF-8"?>
<job xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0" id="job1">
<step>
<chunk item-count="100">
<reader ref="myReader"/>
<processor ref="myProcessor"/>
<writer ref="myWriter"/>
</chunk>
</step>
</job>
Stepの子要素としてchunkを定義。 コミット間隔チューニングは、item-count属性。 デフォルトは10。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 79
chunk方式 : reader/processor/writer
<?xml version="1.0" encoding="UTF-8"?>
<job xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="1.0" id="job1">
<step>
<chunk item-count="100">
<reader ref="myReader"/>
<processor ref="myProcessor"/>
<writer ref="myWriter"/>
</chunk>
</step>
</job> ItemReader ItemProcessor ItemWriterの クラス名の先頭を小文字にして定義する。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 80
F A Q
全てのメソッドの
実装を強制されると困ります..
(open close処理が必要ない場合)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 81
空実装されたAbstractReaderを継承
デフォルト実装は空実装 (何もしない)
openやcloseが呼ばれても何もしない
public class MyReader extends AbstractItemReader {
@Override
public Object readItem() throws Exception {
// read処理の実装 }
}
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 82
F A Q
Abstractクラスを使うと、
他クラスを継承できないので困る
(POJOが一番嬉しい..)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 83
Public Reviewフェーズまでは、
アノテーション活用提案があった。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 84
アノテーションでItemReaderが定義できた
@Named
public class MyItemReader {
@ReadItem
Object read() throws Exception {
// レコード読み取り処理 }
} 今後盛り込まれることを期待。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 85
F A Q
チャンク処理を実装することで、
何かメリットはあるのか?
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 86
共通的な機能を自身で作成しなくてもよくなる
APサーバからコールバックすることで、以下の処理を差し込める
コミット間隔のコントロール
例外が発生したときのハンドリング
– 例外をスキップするのか
– 例外が発生した時点でバッチ処理を中断するのか
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 87
処理したレコード数がモニタリング可能になる
GlassFish4の管理コンソールでは、jBatch状態の確認が可能。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 88
もう1つのStep実装方式 : batchlet
Step
Stepとbatchletが1対1。非常にシンプルな実装。
batchlet
process()
ExitStatus
public class MyBatchlet implements Batchlet {
@Override
public String process() throws Exception { // 処理をここに実装
}
@Override
public void stop() throws Exception { // JobOperator.stop()で強制停止した時の処理をここに実装
}
}
<step id="step1">
<batchlet ref="myBatchlet" />
</step>
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 89
chunkとbatchletの使い分け
Chunk方式
– バルクオペレーション全般
– ファイル、DBレコードを1件ずつ処理
batchlet方式
– コマンド処理全般
– 他サーバへのファイル転送 (ftpなど)
– 圧縮 解凍コマンドの実行
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 90
JobOperator
JobOperator Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
バッチ処理のコントロール役
JobOperator経由でジョブ起動/再起動/停止
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 91
JobRepository : 永続化機構
ジョブインスタンスやチェックポイント情報を保存
仕様上ではあまり詳細については言及されず
GlassFish4 同梱のRIでは組み込みDerbyに永続化
JobOperator Step1 Step2 Step3
Item
Reader
Item
Processor
Item
Writer batchlet
Job
Job Repositoty
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 92
アーキテクチャの振り返り
Jobには1つ以上のStepが含まれる
JobやStepはXMLで表現する
Stepの実装方法は2つある
– chunk : 読む/処理/書くを繰り返す処理向き
– batchlet : コマンド向き
chunk/batchletはjavaで実装する
JobOperatorからジョブを起動する
JobRepositoryにジョブ情報は永続化される
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 93
Spring Batch と jBatch
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 94
Spring Batch と jBatch
jBatch Spring Batch
基本的な作り XMLベース
(ジョブXML)
ユーティリティ クラスの充実
XMLベース (ジョブXML)
現状はなし (シンプルに標準化)
ファイル操作サポート JPA連携サポート
複数サーバ 分散処理
なし あり
JSR準拠 標準仕様
(ベンダフリー) 現状は未準拠
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 95
Spring Batch が jBatch仕様準拠の方向へ
次バージョンの Spring Batch 3.0 で対応予定
– issue としては既に登録済み。
– https://jira.springsource.org/issues/?jql=labels%20%3D%20JSR-352
JSR-330(DI標準仕様)でも Spring とJava EE では連携があった
Spring と Java EE が類似する傾向は今後も続くと考える
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 96
jBatch 動作環境
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 97
対応しているJDK
JDK6 以上 (既にEndOfServiceLifeなので、ぜひJDK7を)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 98
Java SE 環境と EE環境の違い
Java SE Java EE
トランザクション ローカル
トランザクション
スレッド プール
JTA トランザクション
ランタイム実装 に依存
Concurrency Utilities for Java EE (コンテナ管理スレッド)
必要な 設定ファイル
/META-INF/ batch-jobs/job.xml
・/META-INF/ batch-jobs/job.xml ・/META-INF/batch.xml
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 99
もっと jBatch を知る
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 100
java.net から情報収集
Java EE 7 チュートリアルを読む
– http://docs.oracle.com/javaee/7/tutorial/doc/home.htm
仕様書を読む
– http://jcp.org/aboutJava/communityprocess/final/jsr352/index.html
GlassFish4 を動かしてみる
– GlassFish4にはRI(参照実装)が同梱済み
– Java SE で動かす場合は java.net からダウンロード
– https://java.net/projects/jbatch/downloads/download/jsr352-SE-RI-1.0.zip
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 101
Java EE 7書籍も出始めています(洋書)
Java EE Essentials Arun Gupta(著)
Beginning Java EE 7 (日本語訳もあるEE6金魚本の改訂版)
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 102
お時間があれば、デモの簡単なご紹介。
https://github.com/n-agetsu/JBatchDemo
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 103
GlassFish4 を今すぐダウンロード!
ご清聴ありがとうございました。
OracleとJavaは、Oracle Corporation 及びその子会社、関連会社の米国及びその他の国における登録商標です。文中の社名、商品名等は各社の商標または登録商標である場合があります。
Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13 104