@Scheduledによるタスクのスケジューリング
Spring組み込みのタスクスケジューリング機能を使い、一定間隔またはcronスケジュールでバックグラウンドジョブを実行します。
「@Scheduledによるタスクのスケジューリング」はCoddyKit上の無料Spring Boot 4 Complete Guideレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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時間対応のAIチューター)、Spring Boot 4 Complete Guideコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Complete Guideコースには全4レッスンが含まれています。
「@Scheduledによるタスクのスケジューリング」で何を学びますか?
Spring組み込みのタスクスケジューリング機能を使い、一定間隔またはcronスケジュールでバックグラウンドジョブを実行します。 ブラウザで直接実行するハンズオンコードでSpring Boot 4 Complete Guideを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Boot 4 Complete Guideを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Boot 4 Complete Guideは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「@Scheduledによるタスクのスケジューリング」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Boot 4 Complete Guideレッスンでコードを書いて実行できますか?
はい。すべてのSpring Boot 4 Complete Guideレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- @Asyncによる非同期メソッド
- メッセージキュー入門
- RabbitMQとKafkaの統合
- @Scheduledによるタスクのスケジューリング