0

I have some scheduled task:

@Component
class Task {
  @Scheduled(...)
  void exec() {
    doSmth();
    if (allDone) {
      // cancel task, it wont ever run again
    }
  } 
}

This is how I want this to look like. There are solutions on web, but they all seems to be rather complex and not quite work for me, while it seems to be rather common problem.

Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38

1 Answers1

0
     @Component
     class Task {
        private Logger logger = Logger.getLogger(this.getClass());

      @Value("${task.enable}")
      private boolean enableTask;

      @Override
      @Transactional(readOnly=true)
      @Scheduled(cron = "${task.schedule}")
      public void execute() {

           //Do something
          //can use DAO or other autowired beans here
          if(enableTask){
              Do your conditional job here...
          }
     }
     ...
     }