我有一些预定任务:
@Component
class Task {
@Scheduled(...)
void exec() {
doSmth();
if (allDone) {
// cancel task, it wont ever run again
}
}
}
这就是我希望它的样子。网络上有解决方案,但它们似乎都相当复杂,对我来说不太有效,而这似乎是相当普遍的问题。
@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...
}
}
...
}
Java Spring 计划任务