title: Spring SchedulingConfigurer dynamic delay
body: <p>I have an app that should do something once everyday between between 7am and 7pm. I want to randomly pick a valid time to run the task, then use a DIFFERENT start time for the <em>next</em> task.  </p>
  
  <p>Every solution I've found generates a valid random start cron, but then its the same cron for each run afterwards. I need a different, random start each time.</p>
  
  <p>I tried using a CronTrigger, but that still ends up being constant. Here's some example code:</p>
  
  <pre><code>@Component
  public class ScheduledTasks implements SchedulingConfigurer {
  
    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
      taskRegistrar.setScheduler(this.taskExecutor());
      taskRegistrar.addTriggerTask(
        () -&gt; logger.info("The time is {}", dateFormat.format(new Date())),
        new CronTrigger(this.randomCron(0, 60)));
    }
  
    public Executor taskExecutor() {
      return Executors.newScheduledThreadPool(5);
    }
  
    private String randomCron(int min, int max) {
      return ThreadLocalRandom.current().nextInt(min, max) + " * * * * *";
    }
  }
  </code></pre>