Scheduling Batch Jobs
Midnight payroll, 3 AM reports. Cron makes it run on time.
The Hyderabad RTC Bus Schedule Analogy:
The RTC buses in Hyderabad don’t wait for passengers to tell them when to leave. They follow a strict timetable: “Bus 127K leaves every 30 minutes from 8 AM to 10 PM.”
In software, we use Cron Expressions to define these timetables. A cron expression is a string of 6 fields (Seconds, Minutes, Hours, Day of Month, Month, Day of Week) that tells Spring exactly when to execute your code.
- @EnableScheduling: Must be placed on your main
Applicationclass to turn on the scheduling engine. - fixedRate: Runs the method at a fixed interval (e.g., every 5000ms), regardless of how long the task takes.
- fixedDelay: Runs the method X milliseconds after the previous task finishes.
- cron: Uses a cron string for complex schedules (e.g.,
0 0 0 * * ?= midnight every day).
First, enable scheduling in your main class.
package com.devinhyderabad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling // 1. Turn on the scheduling engine
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}package com.devinhyderabad;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class BatchScheduler {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job csvToDbJob; // Inject the Job bean we created earlier
// 2. Schedule using a Cron Expression
// This means: Run at 1:30 AM every day
@Scheduled(cron = "0 30 1 * * ?")
public void runBatchJob() throws Exception {
System.out.println("Scheduler triggered! Launching Job...");
// 3. Always add a unique parameter so Spring Batch
// doesn’t skip it as a duplicate run.
JobParameters params = new JobParametersBuilder()
.addLong("runTime", System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(csvToDbJob, params);
}
}Interview Question: “What is the difference between fixedRate and fixedDelay in Spring Scheduling?”
Answer: fixedRate executes the method at every specified interval, regardless of whether the previous execution has finished. fixedDelay waits for the previous execution to finish, and then waits exactly the specified delay time before starting the next one.
Enterprise Note: @Scheduled runs in a single thread by default. If you have multiple scheduled jobs and one takes 5 minutes, the others will be blocked. In enterprise apps, you must implement a TaskScheduler bean with a thread pool (e.g., ThreadPoolTaskScheduler) to allow jobs to run in parallel.
Key Takeaways
- ✅ @EnableScheduling activates the scheduling engine in Spring Boot
- ✅ fixedRate runs at a fixed interval regardless of execution time
- ✅ fixedDelay waits for the previous run to finish before the next
- ✅ Cron expressions allow complex schedules like “1st day of month at midnight”
- ✅ Use a unique JobParameters value to prevent Spring Batch from skipping runs
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login