DBがデフォルトで不要になったSpring Batch 6 / Spring Boot 4でHello Worldジョブを試す
以前の記事でSpring Batch 5 / Spring Boot 3系でDBを使わないSpring Batchジョブの起動方法について説明しましたが、Spring Batch 6 / Spring Boot 4ではデフォルトでDB不要になっています。
この記事では、以前の記事で実装したHello Worldジョブを再実装してみます。
Note
この記事はSpring Batch 6.0.2 / Spring Boot 4.0.2で検証しました。
Spring Bootプロジェクトの作成
Spring Batch のプロジェクトを次のコマンドで作成します。
curl https://start.spring.io/starter.tgz \
-d type=maven-project \
-d artifactId=hello-spring-batch \
-d baseDir=hello-spring-batch \
-d packageName=com.example.hello \
-d dependencies=batch,native \
-d applicationName=HelloSpringBatchApplication | tar -xzvf -
cd hello-spring-batch
Jobの定義
次のようにJobを定義します。Tasklet 指定時に PlatformTransactionManager を設定しない場合、ResourcelessTransactionManager が使われるようになりました。
cat <<EOF > src/main/java/com/example/hello/JobConfig.java
package com.example.hello;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.Job;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.Step;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.infrastructure.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
public class JobConfig {
private final Logger log = LoggerFactory.getLogger(JobConfig.class);
private final JobRepository jobRepository;
public JobConfig(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}
@Bean
@StepScope
public Tasklet helloTasklet() {
return (contribution, chunkContext) -> {
log.info("Hello World!");
return RepeatStatus.FINISHED;
};
}
@Bean
public Step step1(Tasklet helloTasklet) {
return new StepBuilder("step1", jobRepository).tasklet(helloTasklet)
.build();
}
@Bean
public Job job1(Step step1) {
return new JobBuilder("job1", jobRepository).start(step1).build();
}
}
EOF
実行
次のコマンドでビルドと実行を行います。
./mvnw clean package -DskipTests
java -jar target/hello-spring-batch-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v4.0.2)
2026-01-24T10:32:48.592+09:00 INFO 98071 --- [demo] [ main] c.e.hello.HelloSpringBatchApplication : Starting HelloSpringBatchApplication v0.0.1-SNAPSHOT using Java 25.0.1 with PID 98071 (/private/tmp/hello-spring-batch/target/hello-spring-batch-0.0.1-SNAPSHOT.jar started by toshiaki in /private/tmp/hello-spring-batch)
2026-01-24T10:32:48.594+09:00 INFO 98071 --- [demo] [ main] c.e.hello.HelloSpringBatchApplication : No active profile set, falling back to 1 default profile: "default"
2026-01-24T10:32:48.811+09:00 INFO 98071 --- [demo] [ main] c.e.hello.HelloSpringBatchApplication : Started HelloSpringBatchApplication in 0.359 seconds (process running for 0.532)
2026-01-24T10:32:48.813+09:00 INFO 98071 --- [demo] [ main] o.s.b.b.a.JobLauncherApplicationRunner : Running default command line with: []
2026-01-24T10:32:48.835+09:00 INFO 98071 --- [demo] [ main] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=job1]] launched with the following parameters: [{}]
2026-01-24T10:32:48.837+09:00 INFO 98071 --- [demo] [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
2026-01-24T10:32:48.840+09:00 DEBUG 98071 --- [demo] [ main] s.b.i.s.t.ResourcelessTransactionManager : Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2026-01-24T10:32:48.841+09:00 INFO 98071 --- [demo] [ main] com.example.hello.JobConfig : Hello World!
2026-01-24T10:32:48.841+09:00 DEBUG 98071 --- [demo] [ main] s.b.i.s.t.ResourcelessTransactionManager : Initiating transaction commit
2026-01-24T10:32:48.842+09:00 DEBUG 98071 --- [demo] [ main] s.b.i.s.t.ResourcelessTransactionManager : Committing resourceless transaction on [org.springframework.batch.infrastructure.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@58b71ceb]
2026-01-24T10:32:48.842+09:00 INFO 98071 --- [demo] [ main] o.s.batch.core.step.AbstractStep : Step: [step1] executed in 4ms
2026-01-24T10:32:48.843+09:00 INFO 98071 --- [demo] [ main] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=job1]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 5ms
無事に起動できました。
GraalVM Native Imageでの実行
Native Image化も簡単です。GraalVM Native Imageで実行する場合は、次のコマンドでビルドと実行を行います。私の手元の環境(Mac M4 Max, 128GB RAM)では20秒くらいでビルドが終わりました。
./mvnw native:compile -Pnative -DskipTests
./target/hello-spring-batch
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v4.0.2)
2026-01-24T10:35:01.048+09:00 INFO 98324 --- [demo] [ main] c.e.hello.HelloSpringBatchApplication : Starting AOT-processed HelloSpringBatchApplication using Java 25.0.1 with PID 98324 (/private/tmp/hello-spring-batch/target/hello-spring-batch started by toshiaki in /private/tmp/hello-spring-batch)
2026-01-24T10:35:01.048+09:00 INFO 98324 --- [demo] [ main] c.e.hello.HelloSpringBatchApplication : No active profile set, falling back to 1 default profile: "default"
2026-01-24T10:35:01.052+09:00 INFO 98324 --- [demo] [ main] c.e.hello.HelloSpringBatchApplication : Started HelloSpringBatchApplication in 0.01 seconds (process running for 0.019)
2026-01-24T10:35:01.052+09:00 INFO 98324 --- [demo] [ main] o.s.b.b.a.JobLauncherApplicationRunner : Running default command line with: []
2026-01-24T10:35:01.052+09:00 INFO 98324 --- [demo] [ main] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=job1]] launched with the following parameters: [{}]
2026-01-24T10:35:01.052+09:00 INFO 98324 --- [demo] [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
2026-01-24T10:35:01.052+09:00 DEBUG 98324 --- [demo] [ main] s.b.i.s.t.ResourcelessTransactionManager : Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2026-01-24T10:35:01.052+09:00 INFO 98324 --- [demo] [ main] com.example.hello.JobConfig : Hello World!
2026-01-24T10:35:01.052+09:00 DEBUG 98324 --- [demo] [ main] s.b.i.s.t.ResourcelessTransactionManager : Initiating transaction commit
2026-01-24T10:35:01.052+09:00 DEBUG 98324 --- [demo] [ main] s.b.i.s.t.ResourcelessTransactionManager : Committing resourceless transaction on [org.springframework.batch.infrastructure.support.transaction.ResourcelessTransactionManager$ResourcelessTransaction@7eb43831]
2026-01-24T10:35:01.052+09:00 INFO 98324 --- [demo] [ main] o.s.batch.core.step.AbstractStep : Step: [step1] executed in
2026-01-24T10:35:01.052+09:00 INFO 98324 --- [demo] [ main] o.s.b.c.l.s.TaskExecutorJobLauncher : Job: [SimpleJob: [name=job1]] completed with the following parameters: [{}] and the following status: [COMPLETED] in
こちらも問題なく実行できました。
Spring Batch 6 / Spring Boot 4でデータベースを使わずにJobを作成・実行する方法を紹介しました。
今までSpring Batchのフレームワークは使いたかったけれど、メタデータDBの扱いがいやで、CommandLineRunnerなどでバッチ処理を実装していた方は多いのではないでしょうか?そういう方には朗報です。Spring Batch 6でインフラストラクチャの大きな改善がありました(Breaking Changesも多数ありますが…)。