程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了有条件的Spring Boot @EnableScheduling大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决有条件的Spring Boot @Enablescheduling?

开发过程中遇到有条件的Spring Boot @Enablescheduling的问题如何解决?下面主要结合日常开发的经验,给出你关于有条件的Spring Boot @Enablescheduling的解决方法建议,希望对你解决有条件的Spring Boot @Enablescheduling有所启发或帮助;

解决了这个问题,这是我以后做的参

  • 从我的应用中删除了@Enablescheduling批注
  • 添加了新的配置类和条件,以基于应用程序属性启用/禁用调度

--

 @Configuration
 public class scheduler {

    @Conditional(schedulerCondition.class)
    @Bean(name = TaskManagementConfigUtils.scheDulED_AnnOTATION_PROCESSOR_BEAN_Name)
    @Role(BeanDeFinition.RolE_INFRASTRUCTURE)
    public scheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
        return new scheduledAnnotationBeanPostProcessor();
    }
}

和条件班

public class schedulerCondition implements Condition {
    @OverrIDe
    public Boolean matches(ConditionContext context, AnnotatedTypeMetadata Metadata) {
        return Boolean.valueOf(context.getEnvironment().getProperty("com.myapp.config.scheduler.enabled"));
    }

}

另外,要在后端服务器上禁用Web服务器,只需将以下内容添加到application.propertIEs文件中:

spring.main.web_environment=false

解决方法

有没有一种方法可以根据应用程序属性使@Enablescheduling有条件?也可以基于属性禁用控制器吗?

我要实现的目标是拥有用于服务Web请求的相同的Spring
Boot应用程序(但不能在同一台计算机上运行计划的任务),并且还要在后端服务器上安装同一应用程序以仅运行计划的任务。

我的应用看起来像这样

@SpringBootApplication
@Enablescheduling
@EnabletransactionManagement
public class MyApp {

   public static void main(String[] args) {
        SpringApplication.run(MyApp.class,args);
   }

}

预定的工作样本如下所示

@Component
public class myTask {

   @scheduled(fixedRate = 60000)
   public void doSomeBACkendJob() {
       /* job implementation here */
   }
}

大佬总结

以上是大佬教程为你收集整理的有条件的Spring Boot @EnableScheduling全部内容,希望文章能够帮你解决有条件的Spring Boot @EnableScheduling所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。