gpt4 book ai didi

java - Spring Boot 中是否有一个全局标志来禁用所有计划作业?

转载 作者:行者123 更新时间:2023-12-03 23:05:44 25 4
gpt4 key购买 nike

我有两个作业,第一个作业每 2 秒运行一次,第二个作业每 10 秒运行一次。我在 application.properties 中设置了他们的 cron 表达式,并意识到可以通过将其 cron 表达式修改为 来禁用每个作业>application.properties 文件中的“-”

但真的很想知道 Spring Boot 中是否有一个全局标志可以一次性禁用所有作业

尝试了以下操作,但在服务器启动时出现错误。

pom.xml

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath />
</parent>

application.properties

#global flag for all jobs
spring.enable.scheduling=false
jobs.greet.cron=0/2 * * * * ?
jobs.email.cron=0/10 * * * * ?

DemoApplication.java

@SpringBootApplication
@EnableScheduling
@ConditionalOnProperty(name = "spring.enable.scheduling", matchIfMissing = true, havingValue = "true")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

GreetJob.java

@Component
@Log4j2
public class GreetJob {

@Scheduled(cron = "${jobs.greet.cron}")
public void greet() {
log.info("Starting greet now...");
}

}

EmailJob.java

@Component
@Log4j2
public class EmailJob {

@Scheduled(cron = "${jobs.email.cron}")
public void sendEmail() {
log.info("Sending email now...");
}

}

异常(exception):

2020-05-23 15:51:04,536 ERROR org.springframework.boot.SpringApplication [restartedMain] Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.example.DemoApplication.main(DemoApplication.java:27)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
... 13 common frames omitted

最佳答案

默认情况下,没有这样的选项,但是如果您将 @EnableScheduling 放在使用 @Conditional< 人工创建的配置上,则可以为该工具提供“全局”标志 就可以了。这样,您就不需要通过在其上放置 @Conditional 来更改已定义的计划作业(在您的情况下为 GreetJobEmailJob),并且将为此设施提供单一管理点:

因此,要创建“人工”配置,请创建以下内容:

@Configuration
@EnableScheduling
@ConditionalOnProperty(name="jobs.enabled", havingValue = "true")
public class ConditionalEnableScheduling {
}

这样,如果您使用 --jobs.enabled 启动应用程序,条件将“触发”并且配置将加载,以便初始化“调度基础设施”

一般说明(只是为了答案的完整性):

  • 从主类中删除@EnableScheduling
  • 确保扫描此配置(例如将其放在主类旁边)

关于java - Spring Boot 中是否有一个全局标志来禁用所有计划作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61977922/

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