重启和错误处理
从批处理作业的失败中恢复
重启和错误处理 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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
用 AI 导师学习 Java — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 24
- 课程
- 93
常见问题解答
「重启和错误处理」课时是免费的吗?
是的 — 「重启和错误处理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
「重启和错误处理」这节课中我会学到什么?
从批处理作业的失败中恢复 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 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 反馈 — 无需本地设置。