动态修改定时任务的时间
程序员文章站
2022-03-10 09:03:18
package com.github.wxiaoqi.security.message.scheduled;import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse;import com.aliyuncs.exceptions.ClientException;import com.github.wxiaoqi.security.message.entity.AliyunSmsPipeline;import com....
package com.github.wxiaoqi.security.message.scheduled;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse;
import com.aliyuncs.exceptions.ClientException;
import com.github.wxiaoqi.security.message.entity.AliyunSmsPipeline;
import com.github.wxiaoqi.security.message.entity.AliyunSmsTemplate;
import com.github.wxiaoqi.security.message.mapper.AliyunSmsPipelineMapper;
import com.github.wxiaoqi.security.message.mapper.AliyunSmsTemplateMapper;
import com.github.wxiaoqi.security.message.utils.AliyunMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import tk.mybatis.mapper.entity.Example;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* 功能描述
*
* @param
* @author YOLO
* @date 2020-11-13
* @return
*/
@Component
@EnableScheduling
@Slf4j
public class CheckTemplateScheduled implements SchedulingConfigurer {
@Autowired
private AliyunSmsTemplateMapper aliyunSmsTemplateMapper;
@Autowired
private AliyunSmsPipelineMapper aliyunSmsPipelineMapper;
private String cron = "0 */1 * * * ?";
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(() -> {
Example example = new Example(AliyunSmsTemplate.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("templateStatus", 0);
List<AliyunSmsTemplate> aliyunSmsTemplateList = aliyunSmsTemplateMapper.selectByExample(example);
if (aliyunSmsTemplateList.size() > 0) {
for (AliyunSmsTemplate template : aliyunSmsTemplateList) {
AliyunSmsPipeline aliyunSmsPipeline = aliyunSmsPipelineMapper.selectByPrimaryKey(template.getAliyunSmsPipelineId());
if (aliyunSmsPipeline != null) {
QuerySmsTemplateResponse response = null;
try {
response = AliyunMessageUtil.querySmsTemplate(template.getTemplateCode(), aliyunSmsPipeline.getAccessKeyId(), aliyunSmsPipeline.getAccessKeySecret());
} catch (ClientException e) {
e.printStackTrace();
}
if (response.getTemplateStatus().equals(1)) {
log.info("模板code:" + template.getTemplateCode() + "审核通过");
template.setTemplateStatus(1);
} else if (response.getTemplateStatus().equals(2)) {
template.setTemplateStatus(2);
template.setReason(response.getReason());
log.error("模板code:" + template.getTemplateCode() + "审核未通过");
}
aliyunSmsTemplateMapper.updateByPrimaryKeySelective(template);
}
}
this.cron = "0 */5 * * * ?";
}else{
//没有待审核的没半小时检查一次
this.cron = "0 */30 * * * ?";
}
log.info("执行规则:"+cron);
log.info(format.format(new Date()) + "============定时查询审核状态执行成功");
}, (TriggerContext triggerContext) -> {
//设置执行时间,
CronTrigger cronTrigger = new CronTrigger(cron);
Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
return nextExecutionTime;
});
}
public String getCron() {
return cron;
}
public void setCron(String cron) {
this.cron = cron;
}
}
//其他业务只需要修改 CheckTemplateScheduled 的 cron值就行
本文地址:https://blog.csdn.net/qq_28687183/article/details/109817720