package com.highdatas.mdm.job; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.highdatas.mdm.entity.SysAssemble; import com.highdatas.mdm.service.ISysAssembleService; import com.highdatas.mdm.util.Constant; import com.highdatas.mdm.util.DbUtils; import com.highdatas.mdm.util.HttpUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.http.*; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @Slf4j @ConfigurationProperties(prefix = "xxljob") @Component public class JobClient { @Value("${xxljob.url}") String url; @Value("${xxljob.local}") String local; @Autowired ISysAssembleService assembleService; @Autowired RestTemplate client; String prefix ; private JobClient() { this.prefix = "/xxl-job-admin/"; } public boolean login() { String url = this.url + prefix + "login"; Map params = new LinkedHashMap<>(); params.put("userName","mdmadmin"); params.put("password","datacvg123"); params.put("ifRemember","false"); Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); String s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, null); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); return success; } private boolean parse(JSONObject result) { boolean success = true; Integer code = result.getInteger(Constant.Code); if (code == 200) { success = true; }else { success = false; } return success; } public boolean saveJobGroup() { try { String url = this.url + prefix + "xxlcustapi/saveJobGroup"; Map params = new LinkedHashMap<>(); params.put("appName","MDMApp"); params.put("order", "1"); params.put("title","数据汇集定时任务执行器"); params.put("modelId","MDM"); params.put("addressType","1"); params.put("addressList",local); String s = null; Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求saveJobGroup失败:" + result.getString("msg")); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } public boolean saveAutoJobGroup() { try { String url = this.url + prefix + "xxlcustapi/saveJobGroup"; Map params = new LinkedHashMap<>(); params.put("appName","MDMApp"); params.put("order", "1"); params.put("title","数据汇集定时任务执行器"); params.put("modelId","MDM"); params.put("addressType","0"); // params.put("addressList",local); String s = null; Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求saveJobGroup失败:" + result.getString("msg")); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } public boolean removeJobGroup() { try { String url = this.url + prefix + "xxlcustapi/saveJobGroup"; Map params = new LinkedHashMap<>(); params.put("modelid","MDM"); Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); String s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, null); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求saveJobGroup失败:" + result.getString("msg")); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } public boolean addJob(SysAssemble assemble) { try { String jobId = createJobId(assemble.getId()); String url = this.url + prefix + "xxlcustapi/addJob"; Map params = new LinkedHashMap<>(); params.put("modelId","MDM"); params.put("modelJobId", jobId); params.put("jobCron",assemble.getCron()); params.put("author",assemble.getUserId()); params.put("executorHandler","assembleJob"); params.put("executorParam",assemble.getId()); params.put("cronGen_display",assemble.getCron()); params.put("executorBlockStrategy","DISCARD_LATER"); params.put("executorTimeout","0"); params.put("executorFailRetryCount","0"); params.put("triggerStatus","1"); params.put("glueType","BEAN"); params.put("executorRouteStrategy","FIRST"); params.put("jobDesc","创建汇集job" + jobId); Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); String s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求addJob失败:" + result.getString("msg")); }else{ assemble.setJobId(jobId).updateById(); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } private String createJobId(String id) { if (StringUtils.isEmpty(id)) { id = DbUtils.getUUID(); } try { String jobId = "MDM-" + id; return jobId; } catch (Exception e) { e.printStackTrace(); return id; } } public boolean updateJob(SysAssemble assemble) { try { String jobId = createJobId(assemble.getId()); String url = this.url + prefix + "xxlcustapi/updateJob"; Map params = new LinkedHashMap<>(); params.put("modelId","MDM"); params.put("modelJobId",assemble.getJobId()); params.put("jobCron",assemble.getCron()); params.put("author",assemble.getUserId()); params.put("executorHandler","assembleJob"); params.put("executorParam",assemble.getId()); params.put("cronGen_display",assemble.getCron()); params.put("executorBlockStrategy","DISCARD_LATER"); params.put("executorTimeout","0"); params.put("executorFailRetryCount","3"); params.put("triggerStatus","1"); params.put("glueType","BEAN"); params.put("executorRouteStrategy","FIRST"); params.put("jobDesc","更新汇集job" + jobId); Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); String s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求updateJob失败:" + result.getString("msg")); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } public boolean deleteJob(String jobId) { try { String url = this.url + prefix + "xxlcustapi/removeJob"; Map params = new LinkedHashMap<>(); params.put("modelId","MDM"); params.put("modelJobId", jobId); Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); String s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求deleteJob失败:" + result.getString("msg")); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } public boolean queryJobLogById(SysAssemble assemble) { if (StringUtils.isEmpty(assemble.getJobId())) { return false; } try { String url = this.url + prefix + "xxlcustapi/queryJobLogById"; Map params = new LinkedHashMap<>(); params.put("modelId","MDM"); params.put("modelJobId", assemble.getJobId()); Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); String s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求queryJobLogById失败:" + result.getString("msg")); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } public boolean trigger(SysAssemble assemble) { if (StringUtils.isEmpty(assemble.getJobId())) { return false; } try { String url = this.url + prefix + "xxlcustapi/trigger"; Map params = new LinkedHashMap<>(); params.put("modelId","MDM"); params.put("modelJobId", assemble.getJobId()); params.put("executorParam", assemble.getId()); Map paramMap = new HashMap<>(); paramMap.putAll(params); String urlParamsByMap = HttpUtils.getUrlParamsByMap(paramMap); String s = HttpRestClient(url, HttpMethod.POST, null, urlParamsByMap, MediaType.APPLICATION_JSON); JSONObject result = (JSONObject) JSON.parse(s); boolean success = parse(result); if (!success){ log.error("请求trigger失败:" + result.getString("msg")); } return success; }catch (Exception e){ e.printStackTrace(); return false; } } private String HttpRestClient(String url, HttpMethod method, Map formParams, String getParams, MediaType mediaType) { if (!StringUtils.isEmpty(getParams)) { url = url + Constant.QUESTION + getParams; } client.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); HttpHeaders headers = new HttpHeaders(); if (mediaType == null){ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); }else { headers.setContentType(mediaType); } HttpEntity> requestEntity = new HttpEntity>(formParams, headers); // 执行HTTP请求 ResponseEntity response = client.exchange(url, method, requestEntity, String.class); return response.getBody(); } }