定时任务,redis锁

package com.egoo.vl.crm.baselog.task;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.eeip.eeif.framework.base.util.request.RestTemplateUtil;
import com.egoo.eeip.eeif.starter.cache.lock.RedisLock;
import com.egoo.vl.crm.baselog.dao.history.history.HistoryBycontentDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author zhangjun
 */
@Slf4j
@Component
@ConditionalOnProperty(prefix = "cron.taskOfSendingSMS", name = "enabled", havingValue = "false", matchIfMissing = true)
public class TaskOfSendingSMS {

    @Autowired
    private RedisLock redisLock;

    @Resource
    private HistoryBycontentDao historyBycontentDao;
    /**
     * 发送短信定时任务分布式锁key
     */
    private static final String TASK_OF_SENDING_SMS_KEY = "task_of_sending_sms_redis_key";
    /**
     * 发送短信定时任务分布式锁value
     */
    private static final String TASK_OF_SENDING_SMS_VALUE = "1";
    @Scheduled(cron = "${cron.taskOfSendingSms.value:0 0/30 0 * * ?}")
    public void SendingSmS(){
        baseLogTask(null,null);
    }

    public boolean baseLogTask(String startDate,String endDate) {

        log.info("taskOfSendingSms trace cache task,start...");

        try {
            if (redisLock.lock(TASK_OF_SENDING_SMS_KEY, TASK_OF_SENDING_SMS_VALUE)) {
                if (StrUtil.isBlank(startDate) && StrUtil.isBlank(endDate)){
                    startDate = null;
                    endDate = null;
                }
                List<Map<String, Object>> list = historyBycontentDao.getSessionidByLastThirtyMinutes(startDate,endDate);
                JSONObject jsonObject = new JSONObject();
                JSONArray jsonArray = new JSONArray();

                for (int i = 0; i < list.size(); i++) {
                    Map<String, Object> map = list.get(i);
                    String custNumber = (String) map.get("customernumber");
                    String agentid = (String) map.get("agentid");
                    String sessionid = (String) map.get("sessionid");
                    String tenantId = (String) map.get("tenantId");
                    String smsContent ="【易谷网络】您好,请对本次服务进行评价。" +"https://devcc.myegoo.com.cn/scnxEvaluate/index.html#/?sessionId="+sessionid+"agentId="+agentid+"tenantid="+tenantId;
                    jsonObject.put("targetPhone",custNumber);
                    jsonObject.put("agentId",agentid);
                    jsonObject.put("smsContent",smsContent);
                    jsonObject.put("sessionid",sessionid);
                }
                jsonArray.add(jsonObject);
                JSONObject body = new JSONObject();
                body.put("body", jsonArray);
                log.info("请求体body:{}", body.toJSONString());
                list.forEach(map-> RestTemplateUtil.postMessage(new RestTemplate(),"https://devcc.myegoo.com.cn/idp/api/sendMsg",body));
                return true;
            }
        } catch (Exception e) {
            log.error("taskOfSendingSms trace cache task,message:{},e:{}", e.getMessage(), e);
        } finally {
            redisLock.unlock(TASK_OF_SENDING_SMS_KEY);
        }
        return false;
    }
}

Contoeller层

package com.egoo.vl.crm.baselog.controller.baselog;
import com.egoo.vl.crm.baselog.task.TaskOfSendingSMS;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by IntelliJ IDEA.
 * @author : zhangjun
 * Date: 2022/7/25
 */
@RestController
@RequestMapping(value = "/v1/timeing/task", produces = "application/json")
@Api(value = "定时发送短信接口", tags = "定时发送短信接口", description = "定时发送短信接口")
@Slf4j
public class ScheduledTaskOfSendingSMSController {
    @Autowired
    private TaskOfSendingSMS taskOfSendingSMS;
    @GetMapping("/sendingSms")
    public String getScheduledTaskOfSendingSMS(@RequestParam("startDate") String startDate,@RequestParam("endDate") String endDate) {
        boolean b = taskOfSendingSMS.baseLogTask(startDate, endDate);
        if (b){
            return "定时任务执行成功:";
        }
        return "定时任务执行失败";
    }
}

 

posted @ 2022-08-09 10:32  青衫故人1118  阅读(150)  评论(0编辑  收藏  举报