【Java】Spring Batchを試す
IntellijでSpring Batchの公式チュートリアルを試す
1. Spring Initializrからアプリの雛型(zipファイル)をダウンロードする
2. zipファイルを解凍し、Intellijにインポートする
3. 必要なファイルを作成する
以下のファイルをチュートリアルのコードをコピーして作成する。
- src/main/resources/sample-data.csv
- src/main/resources/schema-all.sql
- src/main/java/com/example/batchprocessing/Person.java
- src/main/java/com/example/batchprocessing/PersonItemProcessor.java
- src/main/java/com/exampe/batchprocessing/BatchConfiguration.java
- src/main/java/com/example/batchprocessing/JobCompletionNotificationListener.java
- src/main/java/com/example/batchprocessing/BatchProcessingApplication.java
4. JARを作成する。
Mavenウィンドウ > batch-processing > Lifecycle > package を右クリックし、「Run Maven Build」をクリックする
5. JARを実行する
target/batch-processing-0.0.1-SNAPSHOT.jar を右クリックし、「Run 'batch-processing-0.0...'」をクリックする
C:\Users\stmtk\.jdks\openjdk-17\bin\java.exe -Dfile.encoding=windows-31j -jar C:\work\IdeaProjects\sample\batch-processing\target\batch-processing-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.6) 2021-11-08 09:34:06.581 INFO 17392 --- [ main] c.e.b.BatchProcessingApplication : Starting BatchProcessingApplication v0.0.1-SNAPSHOT using Java 17 on DESKTOP-TGI2QJ0 with PID 17392 (C:\work\IdeaProjects\sample\batch-processing\target\batch-processing-0.0.1-SNAPSHOT.jar started by stmtk in C:\work\IdeaProjects\sample) 2021-11-08 09:34:06.581 INFO 17392 --- [ main] c.e.b.BatchProcessingApplication : No active profile set, falling back to default profiles: default 2021-11-08 09:34:07.248 INFO 17392 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2021-11-08 09:34:07.500 INFO 17392 --- [ main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported) 2021-11-08 09:34:07.500 INFO 17392 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2021-11-08 09:34:07.691 INFO 17392 --- [ main] o.s.b.c.r.s.JobRepositoryFactoryBean : No database type set, using meta data indicating: HSQL 2021-11-08 09:34:07.852 INFO 17392 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor. 2021-11-08 09:34:07.933 INFO 17392 --- [ main] c.e.b.BatchProcessingApplication : Started BatchProcessingApplication in 1.744 seconds (JVM running for 2.153) 2021-11-08 09:34:07.933 INFO 17392 --- [ main] o.s.b.a.b.JobLauncherApplicationRunner : Running default command line with: [] 2021-11-08 09:34:07.980 INFO 17392 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{run.id=1}] 2021-11-08 09:34:08.011 INFO 17392 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1] 2021-11-08 09:34:08.043 INFO 17392 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Jill, lastName: Doe) into (firstName: JILL, lastName: DOE) 2021-11-08 09:34:08.058 INFO 17392 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Joe, lastName: Doe) into (firstName: JOE, lastName: DOE) 2021-11-08 09:34:08.058 INFO 17392 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Justin, lastName: Doe) into (firstName: JUSTIN, lastName: DOE) 2021-11-08 09:34:08.058 INFO 17392 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Jane, lastName: Doe) into (firstName: JANE, lastName: DOE) 2021-11-08 09:34:08.058 INFO 17392 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: John, lastName: Doe) into (firstName: JOHN, lastName: DOE) 2021-11-08 09:34:08.058 INFO 17392 --- [ main] o.s.batch.core.step.AbstractStep : Step: [step1] executed in 47ms 2021-11-08 09:34:08.074 INFO 17392 --- [ main] c.e.b.JobCompletionNotificationListener : !!! JOB FINISHED! Time to verify the results 2021-11-08 09:34:08.074 INFO 17392 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JILL, lastName: DOE> in the database. 2021-11-08 09:34:08.074 INFO 17392 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JOE, lastName: DOE> in the database. 2021-11-08 09:34:08.074 INFO 17392 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JUSTIN, lastName: DOE> in the database. 2021-11-08 09:34:08.074 INFO 17392 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JANE, lastName: DOE> in the database. 2021-11-08 09:34:08.074 INFO 17392 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JOHN, lastName: DOE> in the database. 2021-11-08 09:34:08.074 INFO 17392 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED] in 79ms 2021-11-08 09:34:08.074 INFO 17392 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2021-11-08 09:34:08.089 INFO 17392 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. Process finished with exit code 0
Linux環境でJARを実行する
Last login: Sat Nov 6 23:32:45 2021 from 10.0.2.2 vagrant@vagrant:~$ cd java vagrant@vagrant:~/java$ ls -l batch-processing-0.0.1-SNAPSHOT.jar -rw-rw-r-- 1 vagrant vagrant 15735333 Nov 6 23:38 batch-processing-0.0.1-SNAPSHOT.jar vagrant@vagrant:~/java$ java -jar batch-processing-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.6) 2021-11-08 00:46:23.761 INFO 1291 --- [ main] c.e.b.BatchProcessingApplication : Starting BatchProcessingApplication v0.0.1-SNAPSHOT using Java 11.0.11 on vagrant with PID 1291 (/home/vagrant/java/batch-processing-0.0.1-SNAPSHOT.jar started by vagrant in /home/vagrant/java) 2021-11-08 00:46:23.770 INFO 1291 --- [ main] c.e.b.BatchProcessingApplication : No active profile set, falling back to default profiles: default 2021-11-08 00:46:27.184 INFO 1291 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2021-11-08 00:46:28.500 INFO 1291 --- [ main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported) 2021-11-08 00:46:28.507 INFO 1291 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2021-11-08 00:46:29.293 INFO 1291 --- [ main] o.s.b.c.r.s.JobRepositoryFactoryBean : No database type set, using meta data indicating: HSQL 2021-11-08 00:46:29.989 INFO 1291 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor. 2021-11-08 00:46:30.360 INFO 1291 --- [ main] c.e.b.BatchProcessingApplication : Started BatchProcessingApplication in 8.442 seconds (JVM running for 10.434) 2021-11-08 00:46:30.388 INFO 1291 --- [ main] o.s.b.a.b.JobLauncherApplicationRunner : Running default command line with: [] 2021-11-08 00:46:30.681 INFO 1291 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{run.id=1}] 2021-11-08 00:46:30.840 INFO 1291 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1] 2021-11-08 00:46:31.000 INFO 1291 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Jill, lastName: Doe) into (firstName: JILL, lastName: DOE) 2021-11-08 00:46:31.001 INFO 1291 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Joe, lastName: Doe) into (firstName: JOE, lastName: DOE) 2021-11-08 00:46:31.005 INFO 1291 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Justin, lastName: Doe) into (firstName: JUSTIN, lastName: DOE) 2021-11-08 00:46:31.006 INFO 1291 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: Jane, lastName: Doe) into (firstName: JANE, lastName: DOE) 2021-11-08 00:46:31.007 INFO 1291 --- [ main] c.e.batchprocessing.PersonItemProcessor : Converting (firstName: John, lastName: Doe) into (firstName: JOHN, lastName: DOE) 2021-11-08 00:46:31.059 INFO 1291 --- [ main] o.s.batch.core.step.AbstractStep : Step: [step1] executed in 207ms 2021-11-08 00:46:31.081 INFO 1291 --- [ main] c.e.b.JobCompletionNotificationListener : !!! JOB FINISHED! Time to verify the results 2021-11-08 00:46:31.109 INFO 1291 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JILL, lastName: DOE> in the database. 2021-11-08 00:46:31.111 INFO 1291 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JOE, lastName: DOE> in the database. 2021-11-08 00:46:31.112 INFO 1291 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JUSTIN, lastName: DOE> in the database. 2021-11-08 00:46:31.112 INFO 1291 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JANE, lastName: DOE> in the database. 2021-11-08 00:46:31.113 INFO 1291 --- [ main] c.e.b.JobCompletionNotificationListener : Found <firstName: JOHN, lastName: DOE> in the database. 2021-11-08 00:46:31.126 INFO 1291 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED] in 300ms 2021-11-08 00:46:31.154 INFO 1291 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2021-11-08 00:46:31.172 INFO 1291 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Linux環境は以下。
Linux環境(仮想環境)にJAR(ファイル)を転送する方法は以下。