我有一些预定的任务:
@Component
class Task {
@Scheduled(...)
void exec() {
doSmth();
if (allDone) {
// cancel task, it wont ever run again
}
}
}
这就是我想要的样子。网上有解决方案,但它们似乎都相当复杂,对我来说不太合适,而它似乎是相当常见的问题。
答案 0 :(得分: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...
}
}
...
}