How does @Scheduled work in Spring Boot, and what is the difference between fixedRate, fixedDelay, and cron? स्प्रिंग बूट में @Scheduled कैसे काम करता है, और fixedRate, fixedDelay और cron में क्या अंतर है?
@Scheduled, enabled with @EnableScheduling, runs a void, no-argument method automatically on a recurring basis without any external trigger like a cron daemon or a message. fixedRate starts a new execution at a fixed interval measured from the start of the previous execution, regardless of how long that execution took — so overlapping runs are possible if the task runs longer than the rate.
fixedDelay waits for the fixed interval after the previous execution completes before starting the next one, guaranteeing no overlap. cron uses a cron expression for precise, calendar-based scheduling (e.g. every weekday at 2 AM) rather than a simple recurring interval, offering the most flexibility for real-world scheduling needs.
@Scheduled(fixedRate = 60000) // every 60s from start of previous run
public void syncInventory() { ... }
@Scheduled(fixedDelay = 60000) // 60s after previous run finishes
public void cleanupTempFiles() { ... }
@Scheduled(cron = "0 0 2 * * MON-FRI") // 2 AM every weekday
public void generateDailyReport() { ... }@Scheduled, जिसे @EnableScheduling से सक्षम किया जाता है, किसी बाहरी ट्रिगर के बिना एक void, बिना-आर्ग्युमेंट मेथड को स्वचालित रूप से बार-बार चलाता है। fixedRate पिछले एक्ज़ीक्यूशन की शुरुआत से मापे गए एक निश्चित अंतराल पर नया एक्ज़ीक्यूशन शुरू करता है, चाहे वह एक्ज़ीक्यूशन कितने भी समय तक चले।
fixedDelay अगला शुरू करने से पहले पिछले एक्ज़ीक्यूशन के पूरा होने के बाद निश्चित अंतराल की प्रतीक्षा करता है, जिससे ओवरलैप न होने की गारंटी मिलती है। cron सटीक, कैलेंडर-आधारित शेड्यूलिंग के लिए एक cron एक्सप्रेशन का उपयोग करता है।
@Scheduled(fixedRate = 60000)
public void syncInventory() { ... }
@Scheduled(cron = "0 0 2 * * MON-FRI")
public void generateDailyReport() { ... }Was this answer clear?