Loading

Quipoin Menu

Learn • Practice • Grow

spring / Scheduling in Spring
interview

Q1. How do you schedule tasks in Spring?
Use @Scheduled annotation on methods, combined with @EnableScheduling. Spring provides cron expressions, fixed delay, or fixed rate. Example:
@Scheduled(cron = "0 0 * * * ?") public void hourlyTask() { ... }

Q2. What is the difference between fixedRate and fixedDelay?
fixedRate runs the task at a specified interval regardless of previous execution duration. fixedDelay runs after the previous execution completes, with a delay between the end and next start. fixedRate may lead to overlapping executions if task runs longer.

Q3. How do you configure a thread pool for scheduled tasks?
By default, Spring uses a single-threaded scheduler. To configure a thread pool, implement SchedulingConfigurer and set TaskScheduler. Example:
@Configuration @EnableScheduling public class SchedulerConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar registrar) { registrar.setScheduler(Executors.newScheduledThreadPool(10)); } }

Q4. How do you conditionally disable a scheduled task?
You can use @ConditionalOnProperty or SpEL with @Scheduled condition. However, @Scheduled doesn't directly support conditions; you can wrap the task in a service and conditionally call it, or use profiles to enable/disable entire configuration.

Q5. What is cron expression?
A cron expression consists of six or seven fields: second, minute, hour, day of month, month, day of week, (optional year). Example: "0 0 9-17 * * MON-FRI" runs every hour from 9am to 5pm on weekdays. Spring uses the same syntax as Quartz.