Loading

Quipoin Menu

Learn • Practice • Grow

spring / Scheduling in Spring
tutorial

Scheduling in Spring

Imagine you need to run a task every day at midnight – like cleaning up old records, sending daily reports, or backing up data. You could stay awake every night and do it manually, but that's not practical. Scheduling lets your application run tasks automatically at fixed times.

Spring provides powerful scheduling capabilities with the @Scheduled annotation. It's like setting an alarm clock for your code.

Enable Scheduling:
First, enable scheduling in your configuration:


@Configuration
@EnableScheduling
public class SchedulingConfig {
}

Basic Scheduled Methods:


@Component
public class ScheduledTasks {

// Run every 5 seconds (fixed delay between executions)
@Scheduled(fixedDelay = 5000)
public void taskWithFixedDelay() {
System.out.println("Fixed delay task executed at: " + new Date());
}

// Run every 5 seconds (fixed rate, regardless of execution time)
@Scheduled(fixedRate = 5000)
public void taskWithFixedRate() {
System.out.println("Fixed rate task executed at: " + new Date());
}

// Run with initial delay of 1 second, then every 5 seconds
@Scheduled(fixedDelay = 5000, initialDelay = 1000)
public void taskWithInitialDelay() {
System.out.println("Task with initial delay executed");
}
}

Cron Expressions (Most Powerful):
Cron expressions let you specify complex schedules like "every day at 2:30 PM" or "every Monday at 9 AM".


// Run every day at midnight
@Scheduled(cron = "0 0 0 * * ?")
public void runAtMidnight() { ... }

// Run every Monday at 9:30 AM
@Scheduled(cron = "0 30 9 ? * MON")
public void runOnMondayMorning() { ... }

// Run every hour at minute 0
@Scheduled(cron = "0 0 * * * ?")
public void runEveryHour() { ... }

// Run every 15 minutes
@Scheduled(cron = "0 */15 * * * ?")
public void runEvery15Minutes() { ... }

Cron Expression Format:
second minute hour day-of-month month day-of-week year(optional)

Example: 0 30 9 ? * MON means: at second 0, minute 30, hour 9, any day-of-month, any month, only on Monday.

Dynamic Scheduling with Cron Triggers:
For schedules that change at runtime, use SchedulingConfigurer:


@Component
public class DynamicScheduler implements SchedulingConfigurer {

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(
() -> System.out.println("Dynamic task executed"),
triggerContext -> {
String cron = "0 */5 * * * ?"; // Get from database or config
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}
}
Two Minute Drill
  • @EnableScheduling enables scheduled tasks.
  • @Scheduled with fixedDelay runs after previous execution completes.
  • @Scheduled with fixedRate runs at fixed intervals regardless of execution time.
  • Cron expressions give precise control over schedule.
  • Use SchedulingConfigurer for dynamic schedules from database.
  • Scheduled methods must be void and take no parameters.

Need more clarification?

Drop us an email at career@quipoinfotech.com