再実行とエラー処理
バッチジョブの失敗から復旧します
「再実行とエラー処理」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Failures are inevitable
Long batch jobs hit bad rows, transient network errors, and crashes. Spring Batch provides skip, retry and restartability so a job can survive and resume.
Restartability basics
Because the JobRepository records each step execution, a failed job can be restarted and it resumes from where it stopped, not from the beginning. The same JobParameters must be used.
Re-launching a failed job
Relaunch with the same parameters and Spring Batch detects the prior failed execution and continues it. Completed steps are skipped.
JobParameters params = new JobParametersBuilder()
.addString("runDate", "2026-05-30")
.toJobParameters();
// running again with SAME params resumes the failed instance
jobLauncher.run(importJob, params);Skipping bad items
Use a fault-tolerant step to skip records that throw a given exception, up to a limit, instead of failing the whole job.
return new StepBuilder("step1", repo)
.<Person, Person>chunk(10, tx)
.reader(reader()).processor(proc()).writer(writer())
.faultTolerant()
.skip(ParseException.class)
.skipLimit(20)
.build();Skip limit
The skipLimit caps how many skips are tolerated. Once exceeded, the step fails. This prevents a job from silently ignoring a flood of bad data.
Retrying transient errors
For temporary failures (a deadlock, a brief network blip) use retry. The item operation is retried up to a limit before being skipped or failing.
return new StepBuilder("step1", repo)
.<Person, Person>chunk(10, tx)
.reader(reader()).processor(proc()).writer(writer())
.faultTolerant()
.retry(DeadlockLoserDataAccessException.class)
.retryLimit(3)
.build();Combining skip and retry
You can chain both: retry transient exceptions a few times, and skip persistent ones. Order matters - retry first, then skip if retries are exhausted.
.faultTolerant()
.retry(TransientException.class).retryLimit(3)
.skip(InvalidDataException.class).skipLimit(50)SkipListener and RetryListener
Listeners let you observe and log what was skipped or retried, which is essential for auditing batch runs.
public class LogSkipListener implements SkipListener<Person, Person> {
@Override
public void onSkipInProcess(Person item, Throwable t) {
log.warn("Skipped {} due to {}", item, t.getMessage());
}
}Idempotency matters
On restart, already-committed chunks are not re-read, but your writes should still be idempotent where possible (use upserts or natural keys) so a partial run does not create duplicates.
No-rollback exceptions
Sometimes an exception should not roll back the chunk. Declare it with noRollback so processing continues without discarding the transaction.
.faultTolerant()
.noRollback(NonCriticalException.class)Preventing accidental reruns
A completed JobInstance cannot be re-run with the same parameters - Spring throws JobInstanceAlreadyCompleteException. Add a unique parameter (like a timestamp) when you truly want a fresh run.
Quick Check
Test your error-handling knowledge.
Recap
You made batch jobs resilient:
- Restartability resumes a failed job from the last good point
skip+skipLimittolerates bad dataretry+retryLimithandles transient errors- Listeners audit skips/retries; keep writes idempotent
よくある質問
「再実行とエラー処理」レッスンは無料ですか?
はい。「再実行とエラー処理」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。
「再実行とエラー処理」で何を学びますか?
バッチジョブの失敗から復旧します ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「再実行とエラー処理」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。