场景:项目中有多个定时器,有些只在开发环境运行,有些需要在线上环境运行
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");
}
}