title: Modify scheduler timing dynamically based on the condition used with spring-boot @Scheduled annotation
body: <p>I have a scheduler, which triggers at a fixed delay of 5secs.<br/>
  I am planning to have more than one schedulers, but for now, let's stick to just one scheduler.<br/><br/>
  <strong>Requirement:</strong> Based on business condition scheduler's <em>fixedDelay</em> should be changed.<br/> **e.g, ** default <em>fixedDelay</em> is <em>5secs</em>, but it can be <em>6, 8, 10secs</em>, based on condition.
  <br/>
  <br/>
  So, in order to acheive this, I am trying to modify the <em>fixedDelay</em>.
  But it's not working for me.<br/></p>
  
  <p><strong>Code:</strong><br/>
  Interface, with delay methods.</p>
  
  <pre><code>public abstract class DynamicSchedule{
          /**
           * Delays scheduler
           * @param milliseconds - the time to delay scheduler.
           */
          abstract void delay(Long milliseconds);
  
          /**
           * Decreases delay period
           * @param milliseconds - the time to decrease delay period.
           */
          abstract void decreaseDelayInterval(Long milliseconds);
  
          /**
           * Increases delay period
           * @param milliseconds - the time to increase dela period
          */
          abstract void increaseDelayInterval(Long milliseconds);
  }
  </code></pre>
  
  <p><br/>
  Implementating Trigger interface that is located at org.springframework.scheduling in the spring-context project.
  <br/></p>
  
  <pre><code>import org.springframework.scheduling.TaskScheduler;
  import org.springframework.scheduling.Trigger;
  import org.springframework.scheduling.TriggerContext;
  
  import java.util.Date;
  import java.util.concurrent.ScheduledFuture;
  
  public class CustomDynamicSchedule extends DynamicSchedule implements Trigger {
  
      private TaskScheduler taskScheduler;
      private ScheduledFuture&lt;?&gt; schedulerFuture;
  
      /**
       * milliseconds
       */
      private long delayInterval;
  
      public CustomDynamicSchedule(TaskScheduler taskScheduler) {
          this.taskScheduler = taskScheduler;
      }
  
  
      @Override
      public void increaseDelayInterval(Long delay) {
          if (schedulerFuture != null) {
              schedulerFuture.cancel(true);
          }
          this.delayInterval += delay;
          schedulerFuture = taskScheduler.schedule(() -&gt; { }, this);
      }
  
      @Override
      public void decreaseDelayInterval(Long delay) {
          if (schedulerFuture != null) {
              schedulerFuture.cancel(true);
          }
          this.delayInterval += delay;
          schedulerFuture = taskScheduler.schedule(() -&gt; { }, this);
      }
  
      @Override
      public void delay(Long delay) {
          if (schedulerFuture != null) {
              schedulerFuture.cancel(true);
          }
          this.delayInterval = delay;
          schedulerFuture = taskScheduler.schedule(() -&gt; { }, this);
      }
  
      @Override
      public Date nextExecutionTime(TriggerContext triggerContext) {
          Date lastTime = triggerContext.lastActualExecutionTime();
          return (lastTime == null) ? new Date() : new Date(lastTime.getTime() + delayInterval);
      }
  }
  </code></pre>
  
  <p><br/>
   configuration:
  <br/></p>
  
  <pre><code>@Configuration
  public class DynamicSchedulerConfig {
      @Bean
      public CustomDynamicSchedule getDinamicScheduler() {
          ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
          threadPoolTaskScheduler.initialize();
          return  new CustomDynamicSchedule(threadPoolTaskScheduler);
      }
  }
  </code></pre>
  
  <p><br/>
  Test class, to test the usage.</p>
  
  <pre><code>@EnableScheduling
  @Component
  public class TestSchedulerComponent {
  
      @Autowired
      private CustomDynamicSchedule dynamicSchedule;
  
      @Scheduled(fixedDelay = 5000)
      public void testMethod() {
          dynamicSchedule.delay(1000l);
          dynamicSchedule.increaseDelayInterval(9000l);
          dynamicSchedule.decreaseDelayInterval(5000l);
      }
  
  }
  </code></pre>
  
  <p><br/>
  <br/>
  I have took help of <a href="https://stackoverflow.com/a/51333059/4770397">https://stackoverflow.com/a/51333059/4770397</a>,  <br/>
  <br/>
  But unfortunately, this code is not working for me.<br/>
  Scheduler is running at <em>fixedDelay</em>, there is not change in that.<br/><br/>
  Please help..</p>