0Pricing
Spring Boot 4 Microservices & REST APIs · Pelajaran

Memulai Ulang dan Menangani Kesalahan

Pulihkan pekerjaan batch dari kegagalan.

Memulai Ulang dan Menangani Kesalahan adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Microservices & REST APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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 + skipLimit tolerates bad data
  • retry + retryLimit handles transient errors
  • Listeners audit skips/retries; keep writes idempotent

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Memulai Ulang dan Menangani Kesalahan” gratis?

Ya — teks lengkap “Memulai Ulang dan Menangani Kesalahan” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Microservices & REST APIs, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Memulai Ulang dan Menangani Kesalahan”?

Pulihkan pekerjaan batch dari kegagalan. Kamu berlatih Spring Boot 4 Microservices & REST APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Microservices & REST APIs?

Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Microservices & REST APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Memulai Ulang dan Menangani Kesalahan” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Microservices & REST APIs ini?

Ya. Setiap pelajaran Spring Boot 4 Microservices & REST APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Penjadwalan dengan @Scheduled
  2. Pekerjaan dan Langkah Spring Batch
  3. Pembaca, Pemroses, dan Penulis
  4. Memulai Ulang dan Menangani Kesalahan
← Kembali ke Spring Boot 4 Microservices & REST APIs