java - Spring boot Scheduler是随机执行的,而不是根据fixedDelay

标签 java spring spring-boot

我最近从 @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 状态

任何人都可以给我提示,这是正确的工作方式还是我配置错误?

最佳答案

使用fixedDelaySprint 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/

相关文章:

java - .NET - .NET 的安全框架

java - 无法读取请求正文

java - GridBagLayout 会影响 JLabel 图像吗?

java - 列表中的 TextArea 输出

java - 没有 instanceof 的类型安全委托(delegate)

java - 从 JavaScript 读取 JSP 变量

java - Spring批处理中的事务管理

java - 在 Spring 中注入(inject) EJB

java - 运行 Spring Boot 应用程序时出错

spring-boot - Spring 启动 1.5.1 : health endpoint does not show registered health indicators