gpt4 book ai didi

java - Spring启动时在数据库中执行异步任务

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:54 25 4
gpt4 key购买 nike

我试图让我的 Spring Boot 应用程序在不连接到数据库的情况下启动。 this question中的方法似乎很有希望,但是,我的应用程序执行 CommandLineRunner ,它在启动时执行一些数据库维护(验证所有索引是否就位等)。为了避免出现超时,我尝试异步运行 CommandLineRunner 方法(使用 Spring 的 @Async),尝试获取数据库连接直到成功,但仍然遇到超时异常。我假设 CommandLineRunner 方法的异步执行不起作用。

对此有任何提示吗?

最佳答案

您可以重构,使 CommandLineRunner 中的任何功能都可以使用 TaskScheduler 进行调度,并在将来安排足够的时间以避免超时。发生故障时,如果维护未按预期运行,您可以正常关闭 Spring Boot 应用程序。

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-scheduler

The simplest method is the one named 'schedule' that takes a Runnable and Date only. That will cause the task to run once after the specified time. All of the other methods are capable of scheduling tasks to run repeatedly. The fixed-rate and fixed-delay methods are for simple, periodic execution, but the method that accepts a Trigger is much more flexible.

关于java - Spring启动时在数据库中执行异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com