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/";
|
}
|
/**
|
*
|
* @description: xxljob 登录接口
|
* @return 具体数据
|
*
|
*/
|
public boolean login() {
|
String url = this.url + prefix + "login";
|
Map<String, String> params = new LinkedHashMap<>();
|
params.put("userName","mdmadmin");
|
params.put("password","datacvg123");
|
params.put("ifRemember","false");
|
Map<String, Object> 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;
|
}
|
|
/**
|
*
|
* @description: xxljob 参数转换
|
* @return 是否请求成功
|
*
|
*/
|
private boolean parse(JSONObject result) {
|
boolean success = true;
|
Integer code = result.getInteger(Constant.Code);
|
if (code == 200) {
|
success = true;
|
}else {
|
success = false;
|
}
|
return success;
|
}
|
|
/**
|
*
|
* @description: xxljob 保存jobGroup
|
* @return 是否请求成功
|
*
|
*/
|
public boolean saveJobGroup() {
|
try {
|
String url = this.url + prefix + "xxlcustapi/saveJobGroup";
|
Map<String, String> 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<String, Object> 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;
|
}
|
|
}
|
/**
|
*
|
* @description: xxljob 保存自动获取ip的jobgroup
|
* @return 是否请求成功
|
*
|
*/
|
public boolean saveAutoJobGroup() {
|
try {
|
String url = this.url + prefix + "xxlcustapi/saveJobGroup";
|
Map<String, String> 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<String, Object> 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;
|
}
|
|
}
|
/**
|
*
|
* @description: xxljob 删除自动获取ip的jobgroup
|
* @return 是否请求成功
|
*
|
*/
|
public boolean removeJobGroup() {
|
try {
|
String url = this.url + prefix + "xxlcustapi/saveJobGroup";
|
Map<String, String> params = new LinkedHashMap<>();
|
params.put("modelid","MDM");
|
Map<String, Object> 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;
|
}
|
|
}
|
|
/**
|
*
|
* @description: xxljob 添加一个job
|
* @param assemble 汇集任务
|
* @return 是否请求成功
|
*
|
*/
|
public boolean addJob(SysAssemble assemble) {
|
try {
|
String jobId = createJobId(assemble.getId());
|
String url = this.url + prefix + "xxlcustapi/addJob";
|
Map<String, String> 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<String, Object> 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;
|
}
|
|
}
|
/**
|
*
|
* @description: xxljob 创建job id
|
* @return 是否请求成功
|
*
|
*/
|
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;
|
}
|
}
|
/**
|
*
|
* @description: xxljob 更新job
|
* @param assemble huiji renwu
|
* @return 是否请求成功
|
*
|
*/
|
public boolean updateJob(SysAssemble assemble) {
|
try {
|
String jobId = createJobId(assemble.getId());
|
String url = this.url + prefix + "xxlcustapi/updateJob";
|
Map<String, String> 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<String, Object> 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;
|
}
|
|
}
|
/**
|
*
|
* @description: xxljob 删除job
|
* @param jobId job ID
|
* @return 是否请求成功
|
*
|
*/
|
public boolean deleteJob(String jobId) {
|
|
try {
|
String url = this.url + prefix + "xxlcustapi/removeJob";
|
Map<String, String> params = new LinkedHashMap<>();
|
params.put("modelId","MDM");
|
params.put("modelJobId", jobId);
|
|
Map<String, Object> 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<String, String> params = new LinkedHashMap<>();
|
params.put("modelId","MDM");
|
params.put("modelJobId", assemble.getJobId());
|
|
Map<String, Object> 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;
|
}
|
|
}
|
|
/**
|
*
|
* @description: xxljob 触发一次
|
* @return 是否请求成功
|
*
|
*/
|
public boolean trigger(SysAssemble assemble) {
|
if (StringUtils.isEmpty(assemble.getJobId())) {
|
return false;
|
}
|
try {
|
String url = this.url + prefix + "xxlcustapi/trigger";
|
Map<String, String> params = new LinkedHashMap<>();
|
params.put("modelId","MDM");
|
params.put("modelJobId", assemble.getJobId());
|
params.put("executorParam", assemble.getId());
|
|
Map<String, Object> 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;
|
}
|
|
}
|
|
/**
|
*
|
* @description: xxljob 使用的http请求工具方法
|
* @return 是否请求成功
|
*
|
*/
|
private String HttpRestClient(String url, HttpMethod method, Map<String, String> 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<Map<String, String>> requestEntity = new HttpEntity<Map<String, String>>(formParams, headers);
|
// 执行HTTP请求
|
ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
|
|
return response.getBody();
|
}
|
}
|