การตั้งเวลางานด้วย @Scheduled
เรียกใช้งานเบื้องหลังตามช่วงเวลาคงที่หรือตาราง cron ด้วยความสามารถในตัวของ Spring สำหรับการตั้งเวลางาน
การตั้งเวลางานด้วย @Scheduled เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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.
เรียนรู้ Java ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 21
- บทเรียน
- 84
คำถามที่พบบ่อย
บทเรียน “การตั้งเวลางานด้วย @Scheduled” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตั้งเวลางานด้วย @Scheduled” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตั้งเวลางานด้วย @Scheduled”
เรียกใช้งานเบื้องหลังตามช่วงเวลาคงที่หรือตาราง cron ด้วยความสามารถในตัวของ Spring สำหรับการตั้งเวลางาน คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เมธอดแบบอะซิงโครนัสด้วย @Async
- บทนำสู่คิวข้อความ
- การผสานรวม RabbitMQ/Kafka
- การตั้งเวลางานด้วย @Scheduled