我最近从 @Scheduled(cron="")
切换到 @Scheduled(fixedDelay=)
因为应用程序工作几个小时后计划任务就停止了执行直到应用程序重新启动。
现在我有这样的东西:
@SpringBootApplication(scanBasePackages = {"pl.corpnet.bst"})
@ComponentScan
@EnableScheduling
@EnableJpaAuditing
public class AiostApplication {
public static void main(String[] args) throws JSONException {
SpringApplication.run(AiostApplication.class, args);
}
}
以及我想要每 5 分钟执行一次的计划方法(逻辑只需将 GET 请求发送到 OpenStack API
并将结果加载到 DB
)
@Scheduled(fixedDelay = 300000)
public void reportCurrentTime() throws JSONException, ParseException, UnknownHostException {
Set<Thread> ts = Thread.getAllStackTraces().keySet();
log.info("Running threads {}",ts.size());
List<IaasApi> apis = iaasApiRepository.findAll();
... logic here
}
我猜它是由线程锁以及通过调查更多内容和放置而造成的
Set<Thread> ts = Thread.getAllStackTraces().keySet();
for (Thread t : ts) {
log.info(t.getName()+ " "+ t.getState());
}
我可以看到多个(启动应用程序后总共有 27 个线程)
https-jsse-nio-8443-exec-8
处于 WAITING
状态
任何人都可以给我提示,这是正确的工作方式还是我配置错误?
最佳答案
使用fixedDelay
,Sprint starts counting the delay after the method stops executing 。如果您希望它每 5 分钟运行一次,您应该使用 fixedRate
,它在方法开始运行后立即开始计数,因此它总是每 5 分钟运行一次。如果最后一次执行还没有完成,Spring将创建另一个Thread来再次运行该任务。
关于java - Spring boot Scheduler是随机执行的,而不是根据fixedDelay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54360617/