title: Spring: Cancel a @Scheduled annotated task
body: <p>I am running a Spring Boot app, and trying to get two jobs running with a specific delay using the <code>@Scheduled</code> annotation.</p>
  
  <p>I would like to cancel these jobs programmatically on a particular condition. What's the recommended way of doing this? Following is the configuration of my app:</p>
  
  <p><strong>Main.java</strong></p>
  
  <pre><code>@SpringBootApplication
  @EnableScheduling
  public class Main implements CommandLineRunner {
  
    static LocalDateTime startTime = LocalDateTime.now();
  
    public static void main(String[] args) {
      SpringApplication app = new SpringApplication(Main.class);
      app.setBannerMode(Banner.Mode.OFF);
      app.run(args);
    }
  
    @Override
    public void run(String... args) throws Exception {
    }
  
  }
  </code></pre>
  
  <p><strong>Job1.java</strong></p>
  
  <pre><code>@Component
  public class Job1 {
  
    @Scheduled(fixedDelay = 10000)
    public void run() {
      System.out.println("Job 1 running");
    }
  
  }
  </code></pre>
  
  <p><strong>Job2.java</strong></p>
  
  <pre><code>@Component
  public class Job1 {
  
    @Scheduled(fixedDelay = 10000)
    public void run() {
      System.out.println("Job 2 running");
    }
  
  }
  </code></pre>