动态修改定时任务的时间

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值就行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值