0Pricing
Spring Boot 4 Microservices & REST APIs · درس

مهام Spring Batch وخطواتها

نظّم المعالجة الدفعية في خطوات

مهام Spring Batch وخطواتها درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «مهام Spring Batch وخطواتها»؟

نظّم المعالجة الدفعية في خطوات تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «مهام Spring Batch وخطواتها»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الجدولة باستخدام @Scheduled
  2. مهام Spring Batch وخطواتها
  3. القارئات والمعالجات والكتّاب
  4. إعادة التشغيل ومعالجة الأخطاء
← العودة إلى Spring Boot 4 Microservices & REST APIs