自动生成job
This commit is contained in:
+56
@@ -13,6 +13,7 @@ import com.mhd.basic.domain.contractManage.repository.mapper.ContractManageMappe
|
||||
import com.mhd.basic.domain.contractManage.repository.po.ContractManagePO;
|
||||
import com.mhd.basic.domain.contractManage.repository.todo.ContractManageDO;
|
||||
import com.mhd.basic.domain.contractManage.repository.util.HttpWebServiceUtil;
|
||||
import com.mhd.basic.domain.contractManage.repository.util.XxlJobOpenApiUtil;
|
||||
import com.mhd.basic.domain.contractManage.service.ContractManageDomainService;
|
||||
import com.mhd.basic.domain.contractManageDetail.entity.ContractManageDetail;
|
||||
import com.mhd.basic.domain.contractManageDetail.repository.mapper.ContractManageDetailMapper;
|
||||
@@ -50,6 +51,7 @@ import com.mhd.system.service.ISysDictTypeService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
@@ -97,6 +99,9 @@ public class ContractManageApplicationService {
|
||||
@Autowired
|
||||
private IContractManageParametersService contractManageParametersService;
|
||||
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
private static final String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjozNTYzLCJ0b3BPcmdhbml6YXRpb25JZCI6MjgyNywidXNlcl9rZXkiOiI0N2MzY2U4My0wMmFjLTQ0NTQtOTdiYS1lYmQwMDc2MzNiNDMiLCJ1c2VybmFtZSI6ImNhbmdtYSJ9.tnZyywwburfBo3Kizdhfk5eYdXbrrZ9976Z0W7GrS6UqSpKwfI2RwdRwfYx7DbD7Ri1Fdj_eOVxBkjvdFF226Q";
|
||||
|
||||
/**
|
||||
* 分页查询合同管理列表
|
||||
@@ -953,9 +958,60 @@ public class ContractManageApplicationService {
|
||||
dto.setTopOrganizationId(contractManageDO.getTopOrganizationId());
|
||||
ContractManageParametersDO parametersDO = contractManageParametersAssembler.toDO(dto);
|
||||
contractManageParametersDomainService.insert(parametersDO);
|
||||
if(StringUtils.isNotEmpty(contractManageDO.getSettlementCustomersCode())){
|
||||
AjaxResult ajaxResult = bmsServiceFeign.getCustomersInfoByCode(contractManageDO.getSettlementCustomersCode());
|
||||
if (!"200".equals(String.valueOf(ajaxResult.get("code")))){
|
||||
throw new ServiceException("获取结算客户信息失败");
|
||||
}
|
||||
SettlementCustomersPO settlementCustomersPO = JSON.parseObject(JSONObject.toJSONString(ajaxResult.get("data")), SettlementCustomersPO.class);
|
||||
if(null == settlementCustomersPO){
|
||||
throw new ServiceException("结算客户信息未找到");
|
||||
}
|
||||
Long settlementEntityId = settlementCustomersPO.getSettlementEntityId();
|
||||
if(null != settlementEntityId){
|
||||
Integer pushContractManageToBmsJobId = settlementCustomersPO.getPushContractManageToBmsJobId();
|
||||
if (null != pushContractManageToBmsJobId) {
|
||||
stopXxlJob(pushContractManageToBmsJobId);
|
||||
}
|
||||
Integer jobId = addXxlJob(settlementEntityId.toString());
|
||||
settlementCustomersPO.setPushContractManageToBmsJobId(jobId);
|
||||
AjaxResult updateResult = bmsServiceFeign.updatePushContractManageJobId(settlementCustomersPO.getSettlementCustomersId(), jobId);
|
||||
if (!"200".equals(String.valueOf(updateResult.get("code")))) {
|
||||
throw new ServiceException("更新结算对象定时任务ID失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Integer addXxlJob(String shipperId) {
|
||||
//1.实例化工具类,建议交给Spring管理
|
||||
XxlJobOpenApiUtil apiUtil = new XxlJobOpenApiUtil(adminAddresses,token);
|
||||
|
||||
//2.构建新增任务参数
|
||||
XxlJobOpenApiUtil.JobInfoAddReq req = new XxlJobOpenApiUtil.JobInfoAddReq();
|
||||
req.setJobGroup(5); //你的执行器组id
|
||||
req.setJobDesc("整租推送计费单据/流水任务,结算对象id:" + shipperId);
|
||||
req.setJobCron("0 0 1 * * ?");
|
||||
req.setExecutorHandler("pushContractManageToBms");
|
||||
req.setExecutorParam(shipperId); //传入业务单号
|
||||
|
||||
//3.调用创建任务接口 ,返回任务ID
|
||||
Integer xxlJobId = apiUtil.addJob(req);
|
||||
if(xxlJobId == null){
|
||||
// 创建定时任务失败,抛异常/重试,不要让单据直接保存成功
|
||||
throw new RuntimeException("创建夜间定时任务失败");
|
||||
}
|
||||
return xxlJobId;
|
||||
}
|
||||
|
||||
public void stopXxlJob(Integer shipperId) {
|
||||
//1.实例化工具类,建议交给Spring管理
|
||||
XxlJobOpenApiUtil apiUtil = new XxlJobOpenApiUtil(adminAddresses,token);
|
||||
//3.调用创建任务接口 ,返回任务ID
|
||||
apiUtil.pauseJob(shipperId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改合同管理
|
||||
*/
|
||||
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package com.mhd.basic.domain.contractManage.repository.util;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* XXL‑JOB OpenApi 工具类
|
||||
*/
|
||||
@Slf4j
|
||||
public class XxlJobOpenApiUtil {
|
||||
|
||||
/** xxl‑job admin地址,例:http://127.0.0.1:8080/xxl‑job‑admin */
|
||||
private final String adminUrl;
|
||||
/** 系统配置里的AccessToken */
|
||||
private final String accessToken;
|
||||
|
||||
public XxlJobOpenApiUtil(String adminUrl, String accessToken) {
|
||||
this.adminUrl = adminUrl.endsWith("/") ? adminUrl.substring(0, adminUrl.length() -1) : adminUrl;
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增定时任务
|
||||
* @param addReq 请求参数
|
||||
* @return jobId 任务ID,返回null代表调用失败
|
||||
*/
|
||||
public Integer addJob(JobInfoAddReq addReq) {
|
||||
String url = adminUrl + "/api/jobinfo/add";
|
||||
try {
|
||||
// 实体对象转map,解决form不能传bean的爆红
|
||||
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
||||
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", accessToken)
|
||||
.form(formMap) //传入map,不再传实体
|
||||
.execute();
|
||||
|
||||
String body = response.body();
|
||||
log.info("xxl‑job addJob resp:{}", body);
|
||||
XxlApiResp resp = JSONUtil.toBean(body, XxlApiResp.class);
|
||||
if (resp != null && resp.getCode() == 200) {
|
||||
return Integer.valueOf(resp.getContent().toString());
|
||||
}
|
||||
log.error("新增xxl‑job任务失败,resp:{}", body);
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
log.error("调用xxl‑job addJob异常", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除任务
|
||||
* @param jobId 任务id
|
||||
* @return true成功
|
||||
*/
|
||||
public boolean removeJob(Integer jobId) {
|
||||
String url = adminUrl + "/api/jobinfo/remove";
|
||||
try (HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", accessToken)
|
||||
.form("id", jobId)
|
||||
.execute()) {
|
||||
String body = response.body();
|
||||
XxlApiResp resp = JSONUtil.toBean(body, XxlApiResp.class);
|
||||
if (resp != null && resp.getCode() == 200) {
|
||||
return true;
|
||||
}
|
||||
log.error("删除xxl‑job任务失败 jobId:{},resp:{}", jobId, body);
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
log.error("调用xxl‑job removeJob异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停任务
|
||||
*/
|
||||
public boolean pauseJob(Integer jobId) {
|
||||
String url = adminUrl + "/api/jobinfo/pause";
|
||||
try (HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", accessToken)
|
||||
.form("id", jobId)
|
||||
.execute()) {
|
||||
String body = response.body();
|
||||
XxlApiResp resp = JSONUtil.toBean(body, XxlApiResp.class);
|
||||
return resp != null && resp.getCode() == 200;
|
||||
} catch (Exception e) {
|
||||
log.error("pauseJob异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//---------------- 实体 ----------------
|
||||
|
||||
@Data
|
||||
public static class JobInfoAddReq {
|
||||
/** 执行器组ID xxl_job_group.id */
|
||||
private Integer jobGroup;
|
||||
/** 任务描述 */
|
||||
private String jobDesc;
|
||||
/** 路由策略 ROUND:轮询 */
|
||||
private String executorRouteStrategy = "ROUND";
|
||||
/** cron表达式 23点 0 0 23 * * ? */
|
||||
private String jobCron;
|
||||
/** @XxlJob 注解handler名称 */
|
||||
private String executorHandler;
|
||||
/** 任务参数,这里存业务单号 */
|
||||
private String executorParam;
|
||||
/** 阻塞策略 SERIAL_EXECUTION单机串行 */
|
||||
private String executorBlockStrategy = "SERIAL_EXECUTION";
|
||||
/** 超时时间 0不限制 */
|
||||
private Integer executorTimeout = 0;
|
||||
/** 失败重试次数 */
|
||||
private Integer executorFailRetryCount = 0;
|
||||
/** BEAN模式 */
|
||||
private String glueType = "BEAN";
|
||||
private String glueSource = "";
|
||||
private String glueRemark = "";
|
||||
/** 1启用 0停止 */
|
||||
private Integer triggerStatus = 1;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class XxlApiResp {
|
||||
private Integer code;
|
||||
private Object content;
|
||||
private String msg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user