SpringBoot之@ConditionalOnProperty实战二:自定义定时器开关

场景:项目中有多个定时器,有些只在开发环境运行,有些需要在线上环境运行

 1、添加配置(application.properties)

#定时器开关
scheduling1.enabled=false
scheduling2.enabled=true

 2、启动类Test.application加启用定时器注解

@EnableScheduling

 3、定时器类加注解

@Component
@ConditionalOnProperty(prefix = "scheduling1", name = "enabled", havingValue = "true")
public class TestQuartz1 {
    /**
     * 1、定时器1
     * 每30分钟执行一次
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void work1() throws Exception{
        System.out.println("TestQuartz1");
    }
}
@Component
@ConditionalOnProperty(prefix = "scheduling2", name = "enabled", havingValue = "true")
public class TestQuartz2 {
    /**
     * 1、定时器2
     * 每10分钟执行一次
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void work2() throws Exception{
        System.out.println("TestQuartz2");
    }
}

推荐:SpringBoot之@ConditionalOnProperty实战一:线上环境关闭sql日志打印

原创文章 139 获赞 401 访问量 50万+

猜你喜欢

转载自blog.csdn.net/qq_31122833/article/details/105431087