Jobについて説明します。
Jobを実行するために必要なものを以下に示します。
JobID
Jobパラメータ
Jobの実行方法は、Spring Batchアプリケーションの起動方法によって異なります。Jobの起動方法については、 “4.7.3.3 Jobの起動方法”を参照してください。
Job定義の記述形式とタグ一覧を以下に示します。
記述形式
Job定義ファイル(job.xml):
<!-- Job定義 --> <batch:job id="testJob" job-repository="testJobRepository"> … </batch:job> |
タグ一覧
タグ一覧を以下に示します。
タグ名 | 概要 | |||
job | Jobの開始と終了 | |||
step | “4.7.4.1.1 チャンクモデル”を参照してください。 | |||
tasklet | ||||
chunk | ||||
next | “4.8.1 条件分岐”を参照してください。 | |||
end | ||||
fail |
タグの説明
job
Jobの開始と終了を定義します。
指定可能な属性名を以下に示します。
属性名 | 必須 | 意味 |
id | ○ | 定義するJobIDを指定します。JobIDはNCName型が使用されます。 |
job-repository | - | 使用するJobRepositoryIDを指定します。指定しない場合は、“jobRepository”が指定されます。 |
WebアプリケーションからJobを起動する場合は、以下メソッドを使用します。
JobLauncher.run(Job job,JobParameters jobParameters)
JobLauncher.run(Job job,JobParameters jobParameters)を使用した起動例は以下のとおりです。
例
コントローラ(JobController.java):
@Scope(value = WebApplicationContext.SCOPE_REQUEST) @Controller // Job実行用コントローラ public class JobController { // JobLauncherの呼び出し @Autowired JobLauncher jobLauncher; // Job(testJob)の呼び出し @Autowired Job testJob; // Jobの起動クラス @RequestMapping(value="/testJob", method=RequestMethod.GET) public String testJob() throws Exception { // Jobを起動 jobLauncher.run(testJob, JobParameters()); return "end"; } // Jobパラメータ生成処理 private static JobParameters JobParameters() { Map<String, JobParameter> map = new HashMap<>(); Date date = new Date(); // 現在時刻をパラメータにセット map.put("date", new JobParameter(date.toString())); JobParameters jobParameters = new JobParameters(map); return jobParameters; } } |
コマンドラインからJobを起動する場合は、以下クラスを使用します。
org.springframework.batch.core.launch.support.CommandLineJobRunner
CommandLineJobRunnerクラスについての詳細事項を以下に示します。
ポイント
CommandLineJobRunnerクラスはjavaコマンドで実行します。実行時に“-classpath”オプションに以下のJarファイルを追加する必要があります。
以下のクラスパス設定サポート用jarファイル
/opt/FJSVibs/lib/classpathhelper/spring-batch30-cph.FUJITSU.jar /opt/FJSVibs/lib/classpathhelper/spring43-web-cph.FUJITSU.jar /opt/FJSVibs/lib/classpathhelper/logback-cph.FUJITSU.jar |
[Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\spring-batch30-cph.FUJITSU.jar [Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\spring43-web-cph.FUJITSU.jar [Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\logback-cph.FUJITSU.jar |
Java EE 7が提供している以下のJarファイル
/opt/FJSVisje7/glassfish/modules/javax.batch-api.jar |
[Interstageのインストールディレクトリ]\F3FMisje7\glassfish\modules\javax.batch-api.jar |
実行するBatchアプリケーション(Jarファイル)
その他処理に必要なJarファイル
■形式
org.springframework.batch.core.launch.support.CommandLineJobRunner Job定義ファイル JobID Jobパラメータ[Jobパラメータ ...] [-stop]
■オプション
-stop
実行中のJobを停止します。
ポイント
StopオプションでJobを停止させる場合は、JobExplorer定義が必要です。JobExplorer定義については、“4.7.4.1.3 JobExplorer定義(コマンドライン)”を参照してください。
■使用例
例
コマンドラインからJobを起動する場合の例を以下に示します。
java -classpath /opt/FJSVibs/lib/classpathhelper/spring-batch30-cph.FUJITSU.jar:/opt/FJSVibs/lib/classpathhelper/spring43-web-cph.FUJITSU.jar:/opt/FJSVibs/lib/classpathhelper/logback-cph.FUJITSU.jar:/opt/FJSVisje7/glassfish/modules/javax.batch-api.jar:/AppDir/TestApp.jar org.springframework.batch.core.launch.support.CommandLineJobRunner testJob.xml testJob date=2017/11/20 |
java -classpath "[Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\spring-batch30-cph.FUJITSU.jar;[Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\spring43-web-cph.FUJITSU.jar;[Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\logback-cph.FUJITSU.jar;[Interstageのインストールディレクトリ]\F3FMisje7\glassfish\modules\javax.batch-api.jar;C:\AppDir\TestApp.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner testJob.xml testJob date=2017/11/20 |
実行中のJobを停止する例を以下に示します。
java -classpath /opt/FJSVibs/lib/classpathhelper/spring-batch30-cph.FUJITSU.jar:/opt/FJSVibs/lib/classpathhelper/spring43-web-cph.FUJITSU.jar:/opt/FJSVibs/lib/classpathhelper/logback-cph.FUJITSU.jar:/opt/FJSVisje7/glassfish/modules/javax.batch-api.jar:/AppDir/TestApp.jar org.springframework.batch.core.launch.support.CommandLineJobRunner testJob.xml testJob date=2017/11/20 -stop |
java -classpath "[Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\spring-batch30-cph.FUJITSU.jar;[Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\spring43-web-cph.FUJITSU.jar;[Interstageのインストールディレクトリ]\BAS\lib\classpathhelper\logback-cph.FUJITSU.jar;[Interstageのインストールディレクトリ]\F3FMisje7\glassfish\modules\javax.batch-api.jar;C:\AppDir\TestApp.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner testJob.xml testJob date=2017/11/20 -stop |
ポイント
終了ステータスの変更をする場合は、すべてのExitStatusに終了ステータスを設定してください。
コマンドラインからJobを起動する場合は、ExitCodeMapperにて設定されている終了ステータス(0,1,2)が返却されます。終了ステータスを変更するためにはJob定義ファイルにExitCodeMapperを設定し、終了ステータス返却クラスを呼び出すことによって、条件に応じた終了ステータスを返却することができます。実行結果がCOMPLETEDだった場合、終了ステータス100を返却する例を以下に示します。
例
Job定義ファイル(job.xml):
<!-- exitCodeMapperの設定 --> <bean id="exitCodeMapper" class="example.SampleExitCodeMapper" /> |
終了ステータス返却クラス(SampleExitCodeMapper.java):
… public class SampleExitCodeMapper extends SimpleJvmExitCodeMapper { Map<String, Integer> mapping = new HashMap<String, Integer>(); public int intValue(String exitCode) { mapping.put(ExitStatus.COMPLETED.getExitCode(), new Integer(100)); mapping.put(ExitStatus.EXECUTING.getExitCode(), new Integer(101)); mapping.put(ExitStatus.FAILED.getExitCode(), new Integer(102)); mapping.put(ExitStatus.NOOP.getExitCode(), new Integer(103)); mapping.put(ExitStatus.STOPPED.getExitCode(), new Integer(104)); mapping.put(ExitStatus.UNKNOWN.getExitCode(), new Integer(105)); setMapping(mapping); return mapping.get(exitCode); } } |
終了ステータスの設定可能範囲はOSによって異なります。
各OSにおける終了ステータスの設定可能範囲を以下に示します。
0 ~ 255
-2147483648 ~ 2147483647