0Pricing
Spring Boot 4 Microservices & REST APIs · レッスン

Spring Batchのジョブとステップ

バッチ処理をステップに分けて構成します

「Spring Batchのジョブとステップ」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What is Spring Batch?

Spring Batch is a framework for robust, large-volume batch processing: reading, transforming and writing data in bulk with built-in transaction management, chunking, and restartability.

  • ETL jobs, migrations, nightly reports
  • Handles millions of records reliably

Adding the dependency

Add spring-boot-starter-batch. Spring Batch needs a datasource to store its metadata (job and step execution history).

<!-- Maven -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

Core concepts: Job and Step

A Job is the whole batch process. It is made of one or more Steps executed in sequence. Each Step does one phase of work.

  • Job = the overall unit
  • Step = a phase (read-process-write or a tasklet)

JobRepository and metadata

The JobRepository persists execution state to database tables (BATCH_JOB_EXECUTION, BATCH_STEP_EXECUTION...). This is what enables restartability and prevents re-running a completed job instance.

Defining a Job with JobBuilder

Use JobBuilder to assemble a job. You give it a name, a JobRepository, and a starting step.

@Bean
public Job importJob(JobRepository repo, Step step1) {
    return new JobBuilder("importJob", repo)
            .start(step1)
            .build();
}

Defining a Step with StepBuilder

StepBuilder builds a step. A chunk-oriented step declares the input/output types, a chunk size, and the reader, processor and writer.

@Bean
public Step step1(JobRepository repo,
                  PlatformTransactionManager tx,
                  ItemReader<String> reader,
                  ItemWriter<String> writer) {
    return new StepBuilder("step1", repo)
            .<String, String>chunk(10, tx)
            .reader(reader)
            .writer(writer)
            .build();
}

Chunk-oriented processing

Chunk processing reads items one by one, accumulates them up to the chunk size, then writes the whole chunk in a single transaction. If the chunk fails it rolls back together.

  • read 10 items -> process each -> write 10 -> commit

Tasklet steps

For a single unit of work (delete a file, run a stored procedure) use a Tasklet step instead of reader/writer.

@Bean
public Step cleanupStep(JobRepository repo, PlatformTransactionManager tx) {
    return new StepBuilder("cleanup", repo)
            .tasklet((contribution, context) -> {
                deleteTempFiles();
                return RepeatStatus.FINISHED;
            }, tx)
            .build();
}

Chaining multiple steps

Run steps in order with .next(). The job moves to the next step only if the previous one succeeds.

@Bean
public Job pipeline(JobRepository repo, Step extract, Step transform, Step load) {
    return new JobBuilder("etl", repo)
            .start(extract)
            .next(transform)
            .next(load)
            .build();
}

JobParameters

A JobInstance is identified by its name plus JobParameters. Different parameters (e.g. a date) create distinct instances, which is how you re-run a job for a new day.

JobParameters params = new JobParametersBuilder()
        .addString("runDate", "2026-05-30")
        .toJobParameters();
jobLauncher.run(importJob, params);

Launching jobs in Spring Boot

By default Spring Boot runs all defined jobs on startup. Disable that with spring.batch.job.enabled=false and launch manually via JobLauncher when you need control (e.g. on a schedule).

Quick Check

Check your grasp of Job and Step structure.

Recap

You now understand the batch model:

  • Job contains ordered Steps
  • JobBuilder / StepBuilder assemble them
  • Chunk steps read-process-write in transactions; tasklet steps do single units
  • JobParameters identify a JobInstance and enable re-runs

よくある質問

「Spring Batchのジョブとステップ」レッスンは無料ですか?

はい。「Spring Batchのジョブとステップ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

「Spring Batchのジョブとステップ」で何を学びますか?

バッチ処理をステップに分けて構成します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Spring Batchのジョブとステップ」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?

はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. @Scheduledによるスケジューリング
  2. Spring Batchのジョブとステップ
  3. Reader、Processor、Writer
  4. 再実行とエラー処理
← Spring Boot 4 Microservices & REST APIsに戻る