0

I am using spring boot + spring scheduler. I want to shut down the scheduler after some condition is met. Please let me know how to make it possible.

@Component
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
    static int a=0;
    @Autowired ThreadPoolTaskScheduler scheduler;

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 2000)
    public void reportCurrentTime() {
        if(a==5)
        {
            //I need code to stop this here
        }
        log.info("The time is now {}", dateFormat.format(new Date()));
        Thread.currentThread().interrupt();
        System.out.println(a);
        a++;
    }
}
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Pavan Jois
  • 31
  • 5
  • Possible duplicate of [How to stop a scheduled task that was started using @Scheduled annotation?](https://stackoverflow.com/questions/44644141/how-to-stop-a-scheduled-task-that-was-started-using-scheduled-annotation) – C-Otto Jan 16 '18 at 14:31
  • Thanks Michael... The solution worked out for me. – Pavan Jois Jan 19 '18 at 07:10

0 Answers0