在Spring Boot IntegrationTest上禁用@Schedule [英] Disable @Schedule on Spring Boot IntegrationTest

查看:884
本文介绍了在Spring Boot IntegrationTest上禁用@Schedule的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Spring Boot IntegrationTest上禁用计划自动启动?

How can I disable the schedule auto-start on Spring Boot IntegrationTest?

谢谢。

推荐答案

请注意外部组件可能会自动启用调度(请参阅 HystrixStreamAutoConfiguration MetricExportAutoConfiguration 。因此,如果您尝试在 @Configuration @ConditionalOnProperty @Profile c $ c>指定 @EnableScheduling 的类,然后由于外部组件而无论如何都会启用调度。

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 class,或者你会遇到外部组件启用它的问题,因此忽略 @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 {
}

a d然后在一个单独的类中

and then in a separate class

@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类中,您还可以创建一个@Bean调用taskScheduler,返回自定义线程池任务调度程序。)

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆