CommandLineJobRunnerでSpringバッチジョブを実行する
CommandLineJobRunner
を使用してSpringバッチジョブを実行する方法を示すクイックガイド。
1. Spring Batchジョブの例
簡単な仕事。
resources/spring/batch/jobs/job-read-files.xml
2. パッケージプロジェクト
Mavenを使用して、プロジェクトを単一のjarファイル(target/your-project.jar)にパッケージ化し、すべての依存関係をtarget/dependency-jars/にコピーします。
pom.xml
org.apache.maven.plugins maven-dependency-plugin 2.5.1 copy-dependencies package copy-dependencies ${project.build.directory}/dependency-jars/
$ mvn package
3. CommandLineJobRunnerの例
使用法 :
CommandLineJobRunner jobPathjobIdentifier (jobParameters)
春のバッチジョブを実行するには、次のコマンドを入力します。
$ java -cp "target/dependency-jars/*:target/your-project.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readJob
jobParameters
の場合、コマンドの最後に追加します。
$ java -cp "target/dependency-jars/*:target/your-project.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readJob file.name=testing.cvs
スケジュールに従って実行するには、通常、上記のコマンドを.sh
ファイルにコピーし、* nixのcron
などの任意のスケジューラコマンドを使用して実行できます。 この例–Add Jobs To cron Under Linuxを参照してください。
P.S When batch job is running under system scheduler, make sure it can locate your project’s classpath.
ソースコードをダウンロード
ダウンロード–SpringBatch-Run-Example.zip(12 KB)