title: How to dynamically schedule a Spring Batch job with ThreadPoolTaskScheduler
body: <p>I have a Spring Batch application in which I want to schedule jobs calls.</p>
  
  <p>The scheduling interval is not known at build so I can't just annotate my Job with @Scheduled.This led me to use a ThreadPoolTaskScheduler.</p>
  
  <p>The thing is the method schedule takes a Runnable as a parameter. Is it possible to schedule jobs this way ?
  I can call the job directly from the following service but I can't schedule it.</p>
  
  <p>Here is my the background of my problem, I tried to make it simple :</p>
  
  <pre><code>@Service
  public class ScheduledProcessor{
      private final ThreadPoolTaskScheduler  threadPoolTaskScheduler;
      private Application application;   
      @Autowired
      public ScheduledProcessor(ThreadPoolTaskScheduler threadPoolTaskScheduler, Application application){
          this.threadPoolTaskScheduler = threadPoolTaskScheduler;
          this.application = application;
          scheduledTasks = new ArrayList();
  
  
          Trigger trigger = new CronTrigger("0/6 * * * * *");
  //Here I am trying to schedule my job.
  //The following line is wrong because a Job can't be cast to a Runnable but I wanted to show the intended behaviour.            
          threadPoolTaskScheduler.schedule((Runnable) application.importUserjob, trigger);
          System.out.println("Job launch !");
      }
  </code></pre>
  
  <p>And here is the JobBuilderFactory :</p>
  
  <pre><code>@Bean
  public Job importUserJob(JobBuilderFactory jobs, Step s1, Step s2) {
      return jobs.get("importUserJob")
              .incrementer(new RunIdIncrementer())
              .flow(s1)
              .end()
              .build();
  }
  </code></pre>
  
  <p>I understand (well, I'm even not sure about that) that I can't directly cast a Job to a Runnable but is it possible to convert it in any way ? Or can you give me some advice about what to use for being able to dynamically schedule spring batch jobs ?</p>
  
  <p>In case that changes something, I also need to be able to restart / skip my steps, as I currently can with the threadPoolTaskScheduler.</p>
  
  <p>Thank you in advance for any help or hint you could provide.</p>