問題描述
如何在 Spring Boot IntegrationTest 上禁用計劃自動啟動?
How can I disable the schedule auto-start on Spring Boot IntegrationTest?
謝謝.
推薦答案
請注意,外部組件可能會自動啟用調度(參見 HystrixStreamAutoConfiguration 和 MetricExportAutoConfiguration 來自 Spring 框架).因此,如果您嘗試在指定 @EnableScheduling
的 @Configuration
類上使用 @ConditionalOnProperty
或 @Profile
,那么由于外部組件,無論如何都會啟用調度.
Be aware that external components could be enabling scheduling automatically (see HystrixStreamAutoConfiguration and MetricExportAutoConfiguration from the Spring Framework). So if you try and use @ConditionalOnProperty
or @Profile
on the @Configuration
class that specifies @EnableScheduling
, then scheduling will be enabled anyway due to external components.
有一個 @Configuration
類可以通過 @EnableScheduling
進行調度,然后將你的調度作業放在單獨的類中,每個類都使用 @ConditionalOnProperty
來啟用/禁用包含@Scheduled 任務的類.
Have one @Configuration
class that enables scheduling via @EnableScheduling
, but then have your scheduled jobs in separate classes, each of those using @ConditionalOnProperty
to enable/disable the classes that contain the @Scheduled tasks.
不要將 @Scheduled
和 @EnableScheduling
放在同一個類中,否則您將遇到外部組件啟用它的問題,因此 @ConditionalOnProperty
被忽略.
Don't have the @Scheduled
and @EnableScheduling
in the same class, or you will have the issue where external components are enabling it anyway, so the @ConditionalOnProperty
is ignored.
例如:
@Configuration
@EnableScheduling
public class MyApplicationSchedulingConfiguration {
}
然后在一個單獨的類中
@Named
@ConditionalOnProperty(value = "scheduling.enabled", havingValue = "true", matchIfMissing = false)
public class MyApplicationScheduledTasks {
@Scheduled(fixedRate = 60 * 60 * 1000)
public void runSomeTaskHourly() {
doStuff();
}
}
這個解決方案的問題是每個計劃的作業都需要在它自己的類中,并指定 @ConditionalOnProperty
.如果您錯過了該注釋,那么作業將運行.
The issue with this solution is that every scheduled job needs to be in it's own class with @ConditionalOnProperty
specified. If you miss that annotation, then the job will run.
擴展 ThreadPoolTask??Scheduler
并覆蓋 TaskScheduler
方法.在這些方法中,您可以檢查作業是否應該運行.
Extend the ThreadPoolTaskScheduler
and override the TaskScheduler
methods. In these methods you can perform a check to see if the job should run.
然后,在您使用@EnableScheduling 的@Configuration 類中,您還創建一個名為taskScheduler 的@Bean,它返回您的自定義線程池任務調度程序.
Then, in your @Configuration class where you use @EnableScheduling, you also create a @Bean called taskScheduler which returns your custom thread pool task scheduler).
例如:
public class ConditionalThreadPoolTaskScheduler extends ThreadPoolTaskScheduler {
@Inject
private Environment environment;
// Override the TaskScheduler methods
@Override
public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
if (!canRun()) {
return null;
}
return super.schedule(task, trigger);
}
@Override
public ScheduledFuture<?> schedule(Runnable task, Date startTime) {
if (!canRun()) {
return null;
}
return super.schedule(task, startTime);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) {
if (!canRun()) {
return null;
}
return super.scheduleAtFixedRate(task, startTime, period);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) {
if (!canRun()) {
return null;
}
return super.scheduleAtFixedRate(task, period);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, Date startTime, long delay) {
if (!canRun()) {
return null;
}
return super.scheduleWithFixedDelay(task, startTime, delay);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long delay) {
if (!canRun()) {
return null;
}
return super.scheduleWithFixedDelay(task, delay);
}
private boolean canRun() {
if (environment == null) {
return false;
}
if (!Boolean.valueOf(environment.getProperty("scheduling.enabled"))) {
return false;
}
return true;
}
}
使用我們的自定義調度程序創建 taskScheduler bean 并啟用調度的配置類
Configuration class that creates the taskScheduler bean using our custom scheduler, and enables scheduling
@Configuration
@EnableScheduling
public class MyApplicationSchedulingConfiguration {
@Bean
public TaskScheduler taskScheduler() {
return new ConditionalThreadPoolTaskScheduler();
}
}
上面的潛在問題是您已經創建了對內部 Spring 類的依賴,因此如果將來有更改,您必須修復兼容性.
The potential issue with the above is that you've created a dependency on an internal Spring class, so if there are changes in the future, you'd have to fix compatibility.
這篇關于在 Spring Boot IntegrationTest 上禁用 @Schedule的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!