Category: java
Introduction to Scheduled Tasks
Published on 24 Jul 2026
Explanation
Spring Boot allows applications to execute tasks automatically at fixed intervals or specific times without manual intervention. Scheduling is commonly used for sending emails, generating reports, cleaning logs, synchronizing data, and running background jobs.
Code:
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Explanation
The @Scheduled annotation executes a method automatically based on a fixed rate, fixed delay, or initial delay. Spring manages the scheduling process, making it easy to automate repetitive tasks.
Code:
@Component
public class ReportScheduler {
@Scheduled(fixedRate = 5000)
public void generateReport() {
System.out.println("Generating Report...");
}
}
Explanation
Cron expressions allow tasks to run at specific dates and times. They provide greater flexibility than fixed intervals and are commonly used for daily backups, monthly reports, and scheduled maintenance jobs.
Code:
@Scheduled(cron = "0 0 10 * * ?")
public void sendDailyReport() {
System.out.println("Report sent at 10 AM");
}
Explanation
Spring Boot supports multiple scheduling strategies. fixedRate executes at regular intervals, fixedDelay waits until the previous execution finishes, and cron schedules tasks using calendar-based expressions.
Code:
@Scheduled(fixedRate = 10000) @Scheduled(fixedDelay = 10000) @Scheduled(cron = "0 */30 * * * ?")
Explanation
Scheduled tasks are widely used in enterprise applications to automate recurring operations such as sending reminder emails, cleaning temporary files, refreshing cache, processing queued messages, generating invoices, and synchronizing data with external systems.
Code:
Daily Backup β 0 0 2 * * ? Hourly Sync β 0 0 * * * ? Every 5 Minutes β 0 */5 * * * ?