第一种实现方式:
关于如何实现在apollo上动态开启或关闭定时任务
直接上代码:
@Slf4j
@Configuration
public class QuartzConfig {
@Resource
//获取配置文件属性的类,这里有多种方式,用你喜欢的方式即可
private QuartzPropertiesUntil quartzPropertiesUntil;
@Resource
private Scheduler scheduler;
private static final String PREFIX = "quartz.";
@ApolloConfigChangeListener("1000")//quartz在appolo中的namespace
private void onRefresh(ConfigChangeEvent changeEvent) {
refreshKeys(changeEvent);
}
private void refreshKeys(ConfigChangeEvent changeEvent) {
for (String key : changeEvent.changedKeys()) {
if (StringUtils.startsWithIgnoreCase(key, PREFIX)) {
log.error("changed value {} ", changeEvent.getChange(key));
String newValue = changeEvent.getChange(key).getNewValue();
switch (key){
case "quartz.enabled":
boolean enabled = Boolean.parseBoolean(newValue);
isEnabled(enabled);
break;
case "quartz.enabled-job1":
boolean enabledJob1 = Boolean.parseBoolean(newValue);
isEnabledJob1(enabledJob1);
break;
case "quartz.enabled-job2":
boolean enabledJob2 = Boolean.parseBoolean(newValue);
isEnabledJob2(enabledJob2);
break;
default:
break;
}
}
}
}
//定时任务总开关
private void isEnabled(boolean enabled) {
if (enabled) {
try {
scheduler.resumeAll();
} catch (SchedulerException e) {
log.error("enabledSchedule start failed:" + e.getMessage());
}
} else {
try {
scheduler.pauseAll();
} catch (SchedulerException e) {
log.error("enabledSchedule pauseAll failed:" + e.getMessage());
}
}
}
@Bean
void enabledSchedule() {
Boolean enabled = quartzPropertiesUntil.getEnabled();
isEnabled(enabled);
}
@Bean
void enabledJob1() {
Boolean enabledJob1 = quartzPropertiesUntil.getEnabledJob1();
isEnabledJob1(enabledJob1);
}
//任务job1开关
private void isEnabledJob1(Boolean enabledJob1) {
JobKey jobKey = new JobKey(CacheConst.JOB1_TASK);//定时任务Job1标识常量
if (enabledeJob1) {
try {
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
log.error("resumeJob1 failed:" + e.getMessage());
}
} else {
try {
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
log.error("pauseJob1 failed:" + e.getMessage());
}
}
}
@Bean
void enabledJob2() {
Boolean enabledJob2 = quartzPropertiesUntil.getEnabledJob2();
isEnabledJob2(enabledJob2);
}
//任务job2开关
private void isEnabledJob2(Boolean enabledJob2) {
JobKey jobKey = new JobKey(CacheConst.JOB2_TASK);//定时任务Job2标识常量
if (enabledJob2) {
try {
scheduler.resumeJob(jobKey);
} catch (SchedulerException e) {
log.error("resumeJob2 failed:" + e.getMessage());
}
} else {
try {
scheduler.pauseJob(jobKey);
} catch (SchedulerException e) {
log.error("pauseJob2 failed:" + e.getMessage());
}
}
}
}
第二中实现方式:
关于如何实现在apollo上动态设置cron表达式
直接上代码:
@Slf4j
@Component(CacheConst.JOB1_TASK)//定时任务Job1标识常量
public class JOB1Task implements SchedulingConfigurer, Ordered {
@Resource
private QuartzPropertiesUntil quartzPropertiesUntil;
@Resource
private IService iService;//业务接口
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
Runnable runnable = () -> {
log.info("Job1Task-------- {}", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
iService.serviceMethod();//业务方法
};
Trigger trigger = triggerContext -> {
CronTrigger cronTrigger = new CronTrigger(quartzPropertiesUntil.getJob1Cron());
return cronTrigger.nextExecutionTime(triggerContext);
};
scheduledTaskRegistrar.addTriggerTask(runnable, trigger);
}
@Override
public int getOrder() {
return LOWEST_PRECEDENCE;
}
}
apollo是很好的分布式配置工具,个人觉得比spring自带的config要好用,好用是好用,但是有些配置修改是不能直接生效的,需要重启服务才能生效,这就失去了apollo的动态配置的作用,这个时候我们需要在项目代码中添加配置类来动态刷新配置,来弥补这个缺陷,quartz定时任务的配置就是一个例子。
在我们开发项目的时候,开发环境和生产环境所需要的日志输出级别一般是不一样的,而日志的输出级别在apollo中的配置也无法动态刷新,所以需要配置动态刷新类,下一篇发文我们来谈谈springboot结合apollo如何动态刷新日志级别