0Pricing
Spring Boot 4 Complete Guide · درس

جدولة المهام باستخدام @Scheduled

شغّلوا المهام الخلفية وفق فواصل زمنية ثابتة أو جداول cron باستخدام دعم Spring المضمّن لجدولة المهام.

جدولة المهام باستخدام @Scheduled درس مجاني في Spring Boot 4 Complete Guide على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Complete Guide، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Complete Guide 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Scheduled Tasks?

Many applications need work to run periodically: cleaning up data, sending digests, refreshing caches. Spring provides scheduling without an external job framework.

Enabling Scheduling

Add @EnableScheduling to a configuration class to activate Spring's scheduler.

@EnableScheduling
@Configuration
public class SchedulingConfig {
}

Fixed Rate Execution

The fixedRate option runs a method every N milliseconds, measured from the start of each run.

@Scheduled(fixedRate = 5000)
public void poll() {
    System.out.println("Polling");
}

Fixed Delay Execution

fixedDelay waits N milliseconds after the previous run finishes, preventing overlap when a task takes a while.

@Scheduled(fixedDelay = 5000)
public void process() {
}

Rate vs Delay

Use fixedRate for steady cadence and fixedDelay when each run must fully complete before the next begins. Choosing wrong can cause overlapping executions.

Cron Expressions

For calendar-based timing use cron. The expression has six fields: second, minute, hour, day, month, weekday.

@Scheduled(cron = "0 0 2 * * *")
public void nightlyJob() {
}

Reading the Cron Above

0 0 2 * * * means at second 0, minute 0, hour 2, every day, every month, any weekday, that is, 2 AM daily.

Initial Delay

The initialDelay attribute postpones the first execution, useful for letting the app warm up before background work begins.

@Scheduled(fixedRate = 5000, initialDelay = 10000)
public void start() {
}

Avoiding Blocking the Scheduler

By default a single thread runs all scheduled tasks. A long task can delay others, so configure a thread pool or pair scheduling with @Async for heavy work.

Scheduling in Clusters

If you run multiple instances, each will fire the same schedule. Use a distributed lock (for example ShedLock) to ensure a job runs once across the cluster.

Externalizing the Schedule

Put the cron expression in properties so you can change timing per environment without recompiling.

@Scheduled(cron = "${cleanup.cron}")
public void cleanup() {
}

Quick Check

Test your understanding of scheduled tasks.

Recap

You enabled scheduling and used fixedRate, fixedDelay, cron expressions, and initial delays. You also learned to externalize schedules and to coordinate jobs safely across a cluster.

الأسئلة الشائعة

هل درس «جدولة المهام باستخدام @Scheduled» مجاني؟

نعم — نص درس «جدولة المهام باستخدام @Scheduled» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Complete Guide، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Complete Guide 4 دروس في المجموع.

ماذا ستتعلم في «جدولة المهام باستخدام @Scheduled»؟

شغّلوا المهام الخلفية وفق فواصل زمنية ثابتة أو جداول cron باستخدام دعم Spring المضمّن لجدولة المهام. تتمرن على Spring Boot 4 Complete Guide مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Complete Guide؟

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

كم من الوقت يستغرق درس «جدولة المهام باستخدام @Scheduled»؟

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

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Complete Guide هذا؟

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

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

  1. الأساليب غير المتزامنة باستخدام @Async
  2. مقدمة إلى قوائم انتظار الرسائل
  3. دمج RabbitMQ وKafka
  4. جدولة المهام باستخدام @Scheduled
← العودة إلى Spring Boot 4 Complete Guide