How to dynamically turn on/off a scheduled method in a springboot application
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot scheduled jobs are static by default, but real systems often need runtime control for maintenance windows, incident mitigation, or progressive feature rollout. A safe design has to handle thread safety, authorization, and cluster behavior, not just a boolean flag. The real question is whether you want to skip execution or stop scheduling entirely.
Pattern 1: Guard Execution with a Thread Safe Flag
The fastest implementation keeps @Scheduled timing static but skips job work when disabled.
This is simple and reliable when skipped triggers are acceptable and you do not need to stop the scheduler thread itself.
Expose an Admin Endpoint for Runtime Toggle
Wrap toggle access in a dedicated admin endpoint and secure it. Runtime job control without authorization is risky.
Use role based access in Spring Security so only approved operators can toggle jobs.
Pattern 2: True Start and Stop Scheduling with TaskScheduler
If you need to stop scheduling itself, manage ScheduledFuture manually.
This avoids wakeups when disabled and gives explicit lifecycle control.
Persist State Across Restarts
Runtime flags in memory are lost on deployment or restart. If toggle state must persist:
- Store state in database, feature flag service, or configuration service.
- Load state on startup and apply it before tasks run.
- Log state changes with actor and timestamp.
Without persistence, teams may think a job is disabled while it restarts enabled after release.
Handle Multi Instance Deployments
In clustered environments, dynamic control must consider all instances. Disabling on one node is not enough if every node schedules the same task.
Common approaches:
- Shared state plus periodic refresh.
- Distributed lock for singleton job execution.
- External scheduler orchestration where app receives work events.
Pick one clearly, document it, and test failover scenarios.
Add Observability and Operational Controls
Dynamic scheduling is operational logic, so expose telemetry:
- Job enabled state metric.
- Last run start and completion time.
- Success and failure counters.
- Last operator toggle event.
These signals reduce confusion during incidents and make runbook actions auditable.
Integration Testing Strategy
Test both functional and timing behavior. A useful integration flow:
- Enable job and verify execution counter increases.
- Disable job and verify counter stays constant.
- Re-enable and verify execution resumes.
Using deterministic test doubles for side effects makes these tests fast and reliable.
Common Pitfalls
- Assuming
@Scheduledannotations can be turned off dynamically without custom control logic. - Using non-thread-safe mutable fields for enable flags.
- Exposing toggle APIs without authentication and authorization.
- Ignoring in-flight run behavior when turning jobs off.
- Missing cluster coordination, causing duplicate execution across instances.
Summary
- Use an execution guard for simple runtime on or off behavior.
- Use
TaskSchedulerandScheduledFuturefor true schedule lifecycle control. - Secure toggle paths and record audit events for operations.
- Persist state when toggle decisions must survive restarts.
- Design and test for multi-instance deployments to avoid duplicate runs.
Related reading
- How to edit configmap configuration in spring boot kubernetes application during runtime
- How to efficiently remove all null elements from a ArrayList or String Array?
- How to enable all endpoints in actuator Spring Boot 2.0.0 RC1
- How to enable batch inserts with Hibernate and Spring Boot
- How to enable Bearer authentication on Spring Boot application?
- How to enable CORS in Spring Boot - Not working
- How to enable HTTP response caching in Spring Boot
- How to enable POST, PUT AND DELETE methods in spring security

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.