2 回答

TA贡献1848条经验 获得超2个赞
@Scheduled您可以在 bean 的一种方法中使用注释来实现这一点。为了启用调度,您需要将@EnableScheduling注释放在您的配置类之一中,可以是主类:
@SpringBootApplication
@EnableScheduling
public class TestingApplication {
public static void main(String[] args) {
SpringApplication.run(TestingApplication.class, args);
}
}
然后您创建一个类,对其进行注释@Component并创建一个带有@Scheduled注释的方法,其中包含一个 cron 语句:
@Component
public class MyWorkerComponent {
@Autowired
private MyListChecker myListChecker;
@Scheduled(cron = "0 0 19 * * ?")
public void doTheListThingy() {
if (myListChecker.isTheListAvailable()) {
// your task logic
}
}
}

TA贡献1802条经验 获得超5个赞
首先,当您的应用程序有多个实例,该任务需要执行一次或可以执行多次时,您应该做出决定。
如果任务可以执行多次,@Pijotrek 和@mkjh 提供的方法很好。如果任务必须只执行一次,您必须使用Quartz Scheduler
或其他框架支持分发调度任务系统。您可以从这里获得更多信息
添加回答
举报