SpringBoot 开启关闭自动任务配置(EnableScheduling )

  • Removed @EnableScheduling annotation from my app
  • Added a new configuration class and conditional to enable/disable scheduling based on an application property

-

 @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();
    }
}

And the conditional class

public class SchedulerCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return Boolean.valueOf(context.getEnvironment().getProperty("com.myapp.config.scheduler.enabled"));
    }

}

Also, to disable web server on the backend server, just add the following to the application.properties file:

spring.main.web_environment=false

评论

暂无评论