package com.dk.common.infrastructure.xxl; import com.alibaba.fastjson.JSONObject; import com.dk.common.exception.BaseBusinessException; import com.dk.common.infrastructure.config.XxlJobInfoConfig; import com.dk.common.infrastructure.constant.Constant; import com.dk.common.infrastructure.enums.ErrorCodeEnum; import com.dk.common.model.xxl.XxlJobInfo; import com.dk.common.response.ResponseCodeEnum; import com.dk.common.response.ResponseResultVO; import com.dk.common.util.HttpUtils; import com.dk.common.util.UrlUtils; import com.xxl.job.core.handler.annotation.XxlJob; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * @author : 洪旭东 * @desc : XxlJobUtils * xxl job 定时任务工具类 * @date : 2022-06-29 11:36 */ @Slf4j @Component public class XxlJobUtils { @Autowired private XxlJobInfoConfig xxlJobInfoConfig; /** * @desc : 创建任务 * 客户提醒 * * second 间隔秒数 * planId 计划id * type 任务类型 * * @author : 洪旭东 * @date : 2022-06-29 14:15 */ public int create(int second, Long planId, String type) { Map> followPlan = xxlJobInfoConfig.getInfo(); int jobGroup = xxlJobInfoConfig.getJobGroup(); XxlJobInfo xxlJobInfo = new XxlJobInfo(); xxlJobInfo.setJobGroup(jobGroup); xxlJobInfo.setJobDesc(followPlan.get(type).get("job-desc").toString()+planId); xxlJobInfo.setAuthor("dk"); xxlJobInfo.setScheduleType("FIX_RATE"); xxlJobInfo.setScheduleConf(String.valueOf(second)); xxlJobInfo.setGlueType("BEAN"); xxlJobInfo.setExecutorHandler(followPlan.get(type).get("executo-handler").toString()); xxlJobInfo.setExecutorRouteStrategy("FIRST"); xxlJobInfo.setMisfireStrategy("DO_NOTHING"); xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION"); xxlJobInfo.setExecutorTimeout(0); xxlJobInfo.setExecutorFailRetryCount(0); xxlJobInfo.setGlueRemark("GLUE代码初始化"); xxlJobInfo.setExecutorParam(String.valueOf(planId)); Map header = new HashMap<>(2); // header.put("Cookie","XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d"); ResponseResultVO resultVO = HttpUtils.post(xxlJobInfoConfig.getServerUrl() + "/hg/scrm/add", xxlJobInfo, header); if (resultVO.getCode() == ResponseCodeEnum.SUCCESS.getCode()) { if (resultVO.getData().getInteger("code").equals(ResponseCodeEnum.SUCCESS.getCode())) { return resultVO.getData().getInteger("content"); } else { //提醒时间有问题,任务间隔时间为负数 if (resultVO.getData()!=null && Constant.StringConstant.XXL_JOB_ERROR_MESSAGE.getName().equals(resultVO.getData().getString("msg"))){ // TODO // throw new BaseBusinessException(ErrorCodeEnum.CUSTOMER_FOLLOW_NEXT_TIME_OVER.getCode(), // ErrorCodeEnum.CUSTOMER_FOLLOW_NEXT_TIME_OVER.getMessage()); } throw new BaseBusinessException(resultVO.getData().getInteger("code"), resultVO.getData().getString("msg")); } } else { throw new BaseBusinessException(resultVO.getCode(), resultVO.getMessage()); } } public int create(String cron, Long primaryKeyId, String type) { Map> info = xxlJobInfoConfig.getInfo(); int jobGroup = xxlJobInfoConfig.getJobGroup(); XxlJobInfo xxlJobInfo = new XxlJobInfo(); xxlJobInfo.setJobGroup(jobGroup); xxlJobInfo.setJobDesc(info.get(type).get("job-desc").toString()+primaryKeyId); xxlJobInfo.setAuthor("dk"); xxlJobInfo.setScheduleType("CRON"); xxlJobInfo.setScheduleConf(cron); xxlJobInfo.setGlueType("BEAN"); xxlJobInfo.setExecutorHandler(info.get(type).get("executo-handler").toString()); xxlJobInfo.setExecutorRouteStrategy("FIRST"); xxlJobInfo.setMisfireStrategy("DO_NOTHING"); xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION"); xxlJobInfo.setExecutorTimeout(0); xxlJobInfo.setExecutorFailRetryCount(0); xxlJobInfo.setGlueRemark("GLUE代码初始化"); xxlJobInfo.setExecutorParam(String.valueOf(primaryKeyId)); Map header = new HashMap<>(2); // header.put("Cookie","XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d"); ResponseResultVO resultVO = HttpUtils.post(xxlJobInfoConfig.getServerUrl() + "/hg/scrm/add", xxlJobInfo, header); if (resultVO.getCode() == ResponseCodeEnum.SUCCESS.getCode()) { if (resultVO.getData().getInteger("code").equals(ResponseCodeEnum.SUCCESS.getCode())) { return resultVO.getData().getInteger("content"); } else { //提醒时间有问题,任务间隔时间为负数 if (resultVO.getData()!=null && Constant.StringConstant.XXL_JOB_ERROR_MESSAGE.getName().equals(resultVO.getData().getString("msg"))){ //TODO // throw new BaseBusinessException(ErrorCodeEnum.CUSTOMER_FOLLOW_NEXT_TIME_OVER.getCode(), // ErrorCodeEnum.CUSTOMER_FOLLOW_NEXT_TIME_OVER.getMessage()); } throw new BaseBusinessException(resultVO.getData().getInteger("code"), resultVO.getData().getString("msg")); } } else { throw new BaseBusinessException(resultVO.getCode(), resultVO.getMessage()); } } /** * @desc : 删除xxl job * @author : 洪旭东 * @date : 2022-06-30 15:50 */ public void delete(int xxlJobId) { Map header = new HashMap<>(2); // header.put("Cookie","XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d"); ResponseResultVO resultVO = HttpUtils.post(xxlJobInfoConfig.getServerUrl() + "/hg/scrm/remove/" + xxlJobId, null, header); if (resultVO.getCode() == ResponseCodeEnum.SUCCESS.getCode()) { if (!resultVO.getData().getInteger("code").equals(ResponseCodeEnum.SUCCESS.getCode())) { throw new BaseBusinessException(resultVO.getData().getInteger("code"), resultVO.getData().getString("msg")); } } else { throw new BaseBusinessException(resultVO.getCode(), resultVO.getMessage()); } } /** * @desc : 查询详情 * @author : 洪旭东 * @date : 2022-07-22 17:23 */ public XxlJobInfo detail(int xxlJobId) { Map header = new HashMap<>(2); // header.put("Cookie","XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d"); ResponseResultVO resultVO = HttpUtils.get(xxlJobInfoConfig.getServerUrl() + "/hg/scrm/detail/" + xxlJobId); if (resultVO.getCode() == ResponseCodeEnum.SUCCESS.getCode()) { if (resultVO.getData()==null) { throw new BaseBusinessException(ResponseCodeEnum.SELECT_NULL); } return JSONObject.parseObject(JSONObject.toJSONString(resultVO.getData()), XxlJobInfo.class); } else { throw new BaseBusinessException(resultVO.getCode(), resultVO.getMessage()); } } /** * @desc : 登录xxl job * 暂时不需要 * @author : 洪旭东 * @date : 2022-06-30 15:58 */ public String loginXxlJob(){ ResponseResultVO resultVO = HttpUtils.post("http://127.0.0.1:8888/xxl-job-admin/hg/scrm/login?userName=admin&password=123456", null); return resultVO.getData().getString("content"); } public void update(XxlJobInfo xxlJobInfo) { Map header = new HashMap<>(2); // header.put("Cookie","XXL_JOB_LOGIN_IDENTITY=7b226964223a312c22757365726e616d65223a2261646d696e222c2270617373776f7264223a226531306164633339343962613539616262653536653035376632306638383365222c22726f6c65223a312c227065726d697373696f6e223a6e756c6c7d"); ResponseResultVO resultVO = HttpUtils.post(xxlJobInfoConfig.getServerUrl() + "/hg/scrm/update", xxlJobInfo); if (resultVO.getCode() == ResponseCodeEnum.SUCCESS.getCode()) { if (!resultVO.getData().getInteger("code").equals(ResponseCodeEnum.SUCCESS.getCode())) { throw new BaseBusinessException(resultVO.getData().getInteger("code"), resultVO.getData().getString("msg")); } } else { throw new BaseBusinessException(resultVO.getCode(), resultVO.getMessage()); } } }