1.需求
可以根据自己配置的开关,动态的控制springboot含有@Scheduled的定时任务
2.解决方案
1.删除启动类的 @EnableScheduling
2.利用condition进行条件判断
public class SchedulerCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return Boolean.valueOf(context.getEnvironment().getProperty("com.myapp.config.scheduler.enabled")); //就是yml值
}
}
3.进行新的定时任务装配到IOC
@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();
}
}