我有两个 jobs
第一台每2秒运行一次,第二台每10秒运行一次,我有他们的。cron
表达式设置在 application.properties
并意识到每个工作都可以通过修改它的功能来禁用 cron
表现为 "-"
在 application.properties
文件。
但真的很想知道在 Spring Boot
这将使所有 jobs
单枪匹马?
试着按以下步骤操作,但在服务器启动时出现错误。
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath />
</parent>
应用程序.属性
#global flag for all jobs
spring.enable.scheduling=false
jobs.greet.cron=0/2 * * * * ?
jobs.email.cron=0/10 * * * * ?
实验程序.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...");
}
}
异常。
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
不,但我建议你把所有的job beans定义为有条件的,比如下面。
@ConditionalOnProperty(value="jobs.enabled", havingValue = "true", matchIfMissing = true)
@Component
@Log4j2
public class GreetJob {
...
}
在这种情况下,所有的工作豆都会被默认启用,即使属性... 启用 没有定义。请禁用 都 您可以在您的应用程序属性文件中定义属性,以便一次性完成作业
jobs.enabled=false
或者在命令行中定义这个属性。
-Djobs.enabled=false
默认情况下,没有这样的选项,但你可以为该设施提供 "全局 "标志,如果你在命令行中输入 @EnableScheduling
人为创造的配置上的 @Conditional
这样你就不需要改变已经定义的Scheduled Jobs了,只要把它放在 @Conditional
在他们身上(GreetJob
和 EmailJob
在您的情况下),并将为该设施提供一个单一的管理点。
所以,为了创建 "人工 "配置,创建以下内容。
@Configuration
@EnableScheduling
@ConditionalOnProperty(name="jobs.enabled", havingValue = "true")
public class ConditionalEnableScheduling {
}
这样,如果你启动应用程序的时候 --jobs.enabled
条件将 "触发",配置将加载,这样 "调度信息 "将被初始化。
一般说明(只是为了答案的完整性)。
@EnableScheduling
从主类