การกำหนดเวลาผ่าน @Scheduled
เรียกใช้งานตามอัตราคงที่หรือตาราง cron
การกำหนดเวลาผ่าน @Scheduled เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Scheduling in Spring Boot
Spring Boot lets you run methods automatically on a schedule without any external cron daemon. You annotate a method with @Scheduled and Spring invokes it repeatedly in the background.
- Perfect for cleanup jobs, polling, report generation
- No extra infrastructure needed
Enabling scheduling
Scheduling is off by default. You turn it on with @EnableScheduling on a configuration class (often the main application class).
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableScheduling
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}fixedRate
fixedRate runs the method at a fixed interval measured from the start of one run to the start of the next. The value is in milliseconds.
@Component
public class RateTask {
@Scheduled(fixedRate = 5000)
public void report() {
System.out.println("Runs every 5 seconds");
}
}fixedDelay
fixedDelay waits a fixed gap after the previous run finishes before starting the next one. Use it when runs must never overlap.
@Scheduled(fixedDelay = 5000)
public void process() {
// next run starts 5s AFTER this one ends
doWork();
}fixedRate vs fixedDelay
The key difference is the reference point:
fixedRate= interval between start times (can overlap if a run is slow)fixedDelay= gap between end and next start (never overlaps)
initialDelay
initialDelay delays the first execution after startup. Combine it with fixedRate or fixedDelay to avoid running immediately on boot.
@Scheduled(initialDelay = 10000, fixedRate = 5000)
public void warmThenRun() {
// waits 10s after startup, then every 5s
}cron expressions
For calendar-based scheduling use cron. Spring cron has 6 fields: second, minute, hour, day-of-month, month, day-of-week.
@Scheduled(cron = "0 0 9 * * MON-FRI")
public void weekdayMorning() {
// 09:00:00 every weekday
}Reading cron fields
An asterisk * means "every value". A ? can be used in day-of-month or day-of-week when one is unspecified. Ranges (MON-FRI), lists (1,15) and steps (*/10) are supported.
@Scheduled(cron = "*/30 * * * * *") // every 30 seconds
public void everyHalfMinute() { }
@Scheduled(cron = "0 0 0 1 * *") // midnight on the 1st of every month
public void monthly() { }Time zones
By default cron uses the server time zone. Set zone to make schedules explicit and predictable across environments.
@Scheduled(cron = "0 0 8 * * *", zone = "Europe/Istanbul")
public void eightAmIstanbul() { }Externalizing the schedule
Hardcoding intervals is inflexible. Use a property placeholder so the schedule can change per environment without recompiling.
@Scheduled(fixedRateString = "${app.report.rate:5000}")
public void report() { }
// application.yml
// app:
// report:
// rate: 10000Scheduling pitfalls
By default a single thread runs all scheduled tasks, so a slow task can delay others. For parallel execution configure a ThreadPoolTaskScheduler. Also keep scheduled methods idempotent in case of restarts.
Quick Check
Test your understanding of scheduling.
Recap
You learned how to schedule work in Spring Boot:
@EnableSchedulingturns the feature onfixedRate= start-to-start,fixedDelay= end-to-startcronhandles calendar schedules; setzonefor clarity- Externalize intervals with property placeholders
คำถามที่พบบ่อย
บทเรียน “การกำหนดเวลาผ่าน @Scheduled” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกำหนดเวลาผ่าน @Scheduled” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดเวลาผ่าน @Scheduled”
เรียกใช้งานตามอัตราคงที่หรือตาราง cron คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนดเวลาผ่าน @Scheduled” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดเวลาผ่าน @Scheduled
- งานและขั้นตอน Spring Batch
- ตัวอ่าน ตัวประมวลผล และตัวเขียน
- การเริ่มใหม่และการจัดการข้อผิดพลาด