<tfoot id='rKSIc'></tfoot>
      <i id='rKSIc'><tr id='rKSIc'><dt id='rKSIc'><q id='rKSIc'><span id='rKSIc'><b id='rKSIc'><form id='rKSIc'><ins id='rKSIc'></ins><ul id='rKSIc'></ul><sub id='rKSIc'></sub></form><legend id='rKSIc'></legend><bdo id='rKSIc'><pre id='rKSIc'><center id='rKSIc'></center></pre></bdo></b><th id='rKSIc'></th></span></q></dt></tr></i><div id='rKSIc'><tfoot id='rKSIc'></tfoot><dl id='rKSIc'><fieldset id='rKSIc'></fieldset></dl></div>
    1. <small id='rKSIc'></small><noframes id='rKSIc'>

      • <bdo id='rKSIc'></bdo><ul id='rKSIc'></ul>
    2. <legend id='rKSIc'><style id='rKSIc'><dir id='rKSIc'><q id='rKSIc'></q></dir></style></legend>

      在 Spring Boot IntegrationTest 上禁用 @Schedule

      时间:2023-10-01

        <legend id='OC9tl'><style id='OC9tl'><dir id='OC9tl'><q id='OC9tl'></q></dir></style></legend>
            <tbody id='OC9tl'></tbody>

            <bdo id='OC9tl'></bdo><ul id='OC9tl'></ul>
          • <tfoot id='OC9tl'></tfoot>

              • <i id='OC9tl'><tr id='OC9tl'><dt id='OC9tl'><q id='OC9tl'><span id='OC9tl'><b id='OC9tl'><form id='OC9tl'><ins id='OC9tl'></ins><ul id='OC9tl'></ul><sub id='OC9tl'></sub></form><legend id='OC9tl'></legend><bdo id='OC9tl'><pre id='OC9tl'><center id='OC9tl'></center></pre></bdo></b><th id='OC9tl'></th></span></q></dt></tr></i><div id='OC9tl'><tfoot id='OC9tl'></tfoot><dl id='OC9tl'><fieldset id='OC9tl'></fieldset></dl></div>
              • <small id='OC9tl'></small><noframes id='OC9tl'>

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

                问题描述

                如何在 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模板网!

                上一篇:TestNG 依赖于不同类的方法 下一篇:在 Maven 集成测试期间启动外部进程

                相关文章

                最新文章

                  <bdo id='4P5Jd'></bdo><ul id='4P5Jd'></ul>

                <i id='4P5Jd'><tr id='4P5Jd'><dt id='4P5Jd'><q id='4P5Jd'><span id='4P5Jd'><b id='4P5Jd'><form id='4P5Jd'><ins id='4P5Jd'></ins><ul id='4P5Jd'></ul><sub id='4P5Jd'></sub></form><legend id='4P5Jd'></legend><bdo id='4P5Jd'><pre id='4P5Jd'><center id='4P5Jd'></center></pre></bdo></b><th id='4P5Jd'></th></span></q></dt></tr></i><div id='4P5Jd'><tfoot id='4P5Jd'></tfoot><dl id='4P5Jd'><fieldset id='4P5Jd'></fieldset></dl></div>

                1. <small id='4P5Jd'></small><noframes id='4P5Jd'>

                2. <legend id='4P5Jd'><style id='4P5Jd'><dir id='4P5Jd'><q id='4P5Jd'></q></dir></style></legend>
                    <tfoot id='4P5Jd'></tfoot>