Job e step di Spring Batch
Strutturi l'elaborazione batch in step.
Job e step di Spring Batch è una lezione Spring Boot 4 Microservices & REST APIs gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Boot 4 Microservices & REST APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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 unitStep= 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:
Jobcontains orderedStepsJobBuilder/StepBuilderassemble them- Chunk steps read-process-write in transactions; tasklet steps do single units
JobParametersidentify aJobInstanceand enable re-runs
Domande Frequenti
La lezione «Job e step di Spring Batch» è gratuita?
Sì — il testo completo di «Job e step di Spring Batch» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Boot 4 Microservices & REST APIs, passa a CoddyKit PRO. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Cosa imparerò in «Job e step di Spring Batch»?
Strutturi l'elaborazione batch in step. Eserciti Spring Boot 4 Microservices & REST APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Spring Boot 4 Microservices & REST APIs?
Non è richiesta alcuna esperienza precedente. Spring Boot 4 Microservices & REST APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Job e step di Spring Batch»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Spring Boot 4 Microservices & REST APIs?
Sì. Ogni lezione Spring Boot 4 Microservices & REST APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Pianificazione con @Scheduled
- Job e step di Spring Batch
- Reader, processor e writer
- Riavvio e gestione degli errori