Hackforge Academy

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 * * * ?

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community