重构xxl-job service 的逻辑

pull/4/head
3y 3 years ago
parent 25c559aa2d
commit 1f6e9511e6

@ -23,7 +23,7 @@ public class AustinConstant {
/** /**
* cron * cron
*/ */
public final static String CRON_FORMAT = "ss mm HH dd MM ? yyyy"; public final static String CRON_FORMAT = "ss mm HH dd MM ? yyyy-yyyy";
/** /**

@ -38,11 +38,11 @@ public class XxlJobConstant {
/** /**
* *
*/ */
public static final Integer RETRY_COUNT = 2; public static final Integer RETRY_COUNT = 0;
/** /**
* () * ()
*/ */
public static final Integer DELAY_TIME = 5; public static final Integer DELAY_TIME = 10;
} }

@ -1,15 +1,18 @@
package com.java3y.austin.service.impl; package com.java3y.austin.service.impl;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.java3y.austin.constants.XxlJobConstant; import com.java3y.austin.constants.XxlJobConstant;
import com.java3y.austin.entity.XxlJobGroup; import com.java3y.austin.entity.XxlJobGroup;
import com.java3y.austin.entity.XxlJobInfo; import com.java3y.austin.entity.XxlJobInfo;
import com.java3y.austin.enums.RespStatusEnum; import com.java3y.austin.enums.RespStatusEnum;
import com.java3y.austin.service.CronTaskService; import com.java3y.austin.service.CronTaskService;
import com.java3y.austin.vo.BasicResultVO; import com.java3y.austin.vo.BasicResultVO;
import com.xxl.job.core.biz.model.ReturnT;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -42,70 +45,99 @@ public class CronTaskServiceImpl implements CronTaskService {
String path = xxlJobInfo.getId() == null ? xxlAddresses + XxlJobConstant.INSERT_URL String path = xxlJobInfo.getId() == null ? xxlAddresses + XxlJobConstant.INSERT_URL
: xxlAddresses + XxlJobConstant.UPDATE_URL; : xxlAddresses + XxlJobConstant.UPDATE_URL;
HttpResponse response = null; HttpResponse response;
ReturnT returnT = null;
try { try {
response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); response = HttpRequest.post(path).form(params).cookie(getCookie()).execute();
returnT = JSON.parseObject(response.body(), ReturnT.class);
// 插入时需要返回Id而更新时不需要 // 插入时需要返回Id而更新时不需要
if (path.contains(XxlJobConstant.INSERT_URL) && response.isOk()) { if (response.isOk() && ReturnT.SUCCESS_CODE == returnT.getCode()) {
Integer taskId = Integer.parseInt(String.valueOf(JSON.parseObject(response.body()).get("content"))); if (path.contains(XxlJobConstant.INSERT_URL)) {
Integer taskId = Integer.parseInt(String.valueOf(returnT.getContent()));
return BasicResultVO.success(taskId); return BasicResultVO.success(taskId);
} else if (response.isOk()) { } else if (path.contains(XxlJobConstant.UPDATE_URL)) {
return BasicResultVO.success(); return BasicResultVO.success();
} }
}
} catch (Exception e) { } catch (Exception e) {
log.error("CronTaskService#saveTask fail:{}", JSON.toJSONString(response.body())); log.error("CronTaskService#saveTask fail,e:{},param:{},response:{}", Throwables.getStackTraceAsString(e)
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); , JSON.toJSONString(xxlJobInfo), JSON.toJSONString(returnT));
} }
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(returnT));
} }
@Override @Override
public BasicResultVO deleteCronTask(Integer taskId) { public BasicResultVO deleteCronTask(Integer taskId) {
HashMap<String, Object> params = new HashMap<>();
params.put("id", taskId);
String path = xxlAddresses + XxlJobConstant.DELETE_URL; String path = xxlAddresses + XxlJobConstant.DELETE_URL;
HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute();
if (!response.isOk()) { HashMap<String, Object> params = MapUtil.newHashMap();
log.error("CronTaskService#deleteCronTask fail:{}", JSON.toJSONString(response.body())); params.put("id", taskId);
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body()));
} HttpResponse response;
ReturnT returnT = null;
try {
response = HttpRequest.post(path).form(params).cookie(getCookie()).execute();
returnT = JSON.parseObject(response.body(), ReturnT.class);
if (response.isOk() && ReturnT.SUCCESS_CODE == returnT.getCode()) {
return BasicResultVO.success(); return BasicResultVO.success();
} }
} catch (Exception e) {
log.error("CronTaskService#deleteCronTask fail,e:{},param:{},response:{}", Throwables.getStackTraceAsString(e)
, JSON.toJSONString(params), JSON.toJSONString(returnT));
}
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(returnT));
}
@Override @Override
public BasicResultVO startCronTask(Integer taskId) { public BasicResultVO startCronTask(Integer taskId) {
HashMap<String, Object> params = new HashMap<>(); String path = xxlAddresses + XxlJobConstant.RUN_URL;
HashMap<String, Object> params = MapUtil.newHashMap();
params.put("id", taskId); params.put("id", taskId);
String path = xxlAddresses + XxlJobConstant.RUN_URL; HttpResponse response;
HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); ReturnT returnT = null;
if (!response.isOk()) { try {
log.error("CronTaskService#startCronTask fail:{}", JSON.toJSONString(response.body())); response = HttpRequest.post(path).form(params).cookie(getCookie()).execute();
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); returnT = JSON.parseObject(response.body(), ReturnT.class);
} if (response.isOk() && ReturnT.SUCCESS_CODE == returnT.getCode()) {
return BasicResultVO.success(); return BasicResultVO.success();
} }
} catch (Exception e) {
log.error("CronTaskService#startCronTask fail,e:{},param:{},response:{}", Throwables.getStackTraceAsString(e)
, JSON.toJSONString(params), JSON.toJSONString(returnT));
}
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(returnT));
}
@Override @Override
public BasicResultVO stopCronTask(Integer taskId) { public BasicResultVO stopCronTask(Integer taskId) {
HashMap<String, Object> params = new HashMap<>(); String path = xxlAddresses + XxlJobConstant.STOP_URL;
HashMap<String, Object> params = MapUtil.newHashMap();
params.put("id", taskId); params.put("id", taskId);
String path = xxlAddresses + XxlJobConstant.STOP_URL; HttpResponse response;
HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute(); ReturnT returnT = null;
if (!response.isOk()) { try {
log.error("CronTaskService#stopCronTask fail:{}", JSON.toJSONString(response.body())); response = HttpRequest.post(path).form(params).cookie(getCookie()).execute();
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); returnT = JSON.parseObject(response.body(), ReturnT.class);
} if (response.isOk() && ReturnT.SUCCESS_CODE == returnT.getCode()) {
return BasicResultVO.success(); return BasicResultVO.success();
} }
} catch (Exception e) {
log.error("CronTaskService#stopCronTask fail,e:{},param:{},response:{}", Throwables.getStackTraceAsString(e)
, JSON.toJSONString(params), JSON.toJSONString(returnT));
}
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(returnT));
}
@Override @Override
public BasicResultVO getGroupId(String appName, String title) { public BasicResultVO getGroupId(String appName, String title) {
String path = xxlAddresses + XxlJobConstant.JOB_GROUP_PAGE_LIST; String path = xxlAddresses + XxlJobConstant.JOB_GROUP_PAGE_LIST;
HashMap<String, Object> params = new HashMap<>(); HashMap<String, Object> params = MapUtil.newHashMap();
params.put("appname", appName); params.put("appname", appName);
params.put("title", title); params.put("title", title);
@ -117,8 +149,8 @@ public class CronTaskServiceImpl implements CronTaskService {
return BasicResultVO.success(id); return BasicResultVO.success(id);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("CronTaskService#getGroupId fail:{}", JSON.toJSONString(response.body())); log.error("CronTaskService#getGroupId fail,e:{},param:{},response:{}", Throwables.getStackTraceAsString(e)
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); , JSON.toJSONString(params), JSON.toJSONString(response.body()));
} }
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body())); return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body()));
} }
@ -127,13 +159,22 @@ public class CronTaskServiceImpl implements CronTaskService {
public BasicResultVO createGroup(XxlJobGroup xxlJobGroup) { public BasicResultVO createGroup(XxlJobGroup xxlJobGroup) {
Map<String, Object> params = JSON.parseObject(JSON.toJSONString(xxlJobGroup), Map.class); Map<String, Object> params = JSON.parseObject(JSON.toJSONString(xxlJobGroup), Map.class);
String path = xxlAddresses + XxlJobConstant.JOB_GROUP_INSERT_URL; String path = xxlAddresses + XxlJobConstant.JOB_GROUP_INSERT_URL;
HttpResponse response = HttpRequest.post(path).form(params).cookie(getCookie()).execute();
if (!response.isOk()) { HttpResponse response;
log.error("CronTaskService#createGroup fail:{}", JSON.toJSONString(response.body())); ReturnT returnT = null;
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(response.body()));
} try {
response = HttpRequest.post(path).form(params).cookie(getCookie()).execute();
returnT = JSON.parseObject(response.body(), ReturnT.class);
if (response.isOk() && ReturnT.SUCCESS_CODE == returnT.getCode()) {
return BasicResultVO.success(); return BasicResultVO.success();
} }
} catch (Exception e) {
log.error("CronTaskService#createGroup fail,e:{},param:{},response:{}", Throwables.getStackTraceAsString(e)
, JSON.toJSONString(params), JSON.toJSONString(returnT));
}
return BasicResultVO.fail(RespStatusEnum.SERVICE_ERROR, JSON.toJSONString(returnT));
}
/** /**
* xxl cookie * xxl cookie
@ -141,13 +182,15 @@ public class CronTaskServiceImpl implements CronTaskService {
* @return String * @return String
*/ */
private String getCookie() { private String getCookie() {
Map<String, Object> hashMap = new HashMap<>(); Map<String, Object> params = MapUtil.newHashMap();
hashMap.put("userName", xxlUserName); params.put("userName", xxlUserName);
hashMap.put("password", xxlPassword); params.put("password", xxlPassword);
hashMap.put("randomCode", IdUtil.fastSimpleUUID()); params.put("randomCode", IdUtil.fastSimpleUUID());
String path = xxlAddresses + XxlJobConstant.LOGIN_URL; String path = xxlAddresses + XxlJobConstant.LOGIN_URL;
HttpResponse response = HttpRequest.post(path).form(hashMap).execute(); HttpResponse response = null;
try {
response = HttpRequest.post(path).form(params).execute();
if (response.isOk()) { if (response.isOk()) {
List<HttpCookie> cookies = response.getCookies(); List<HttpCookie> cookies = response.getCookies();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -156,7 +199,10 @@ public class CronTaskServiceImpl implements CronTaskService {
} }
return sb.toString(); return sb.toString();
} }
log.error("CronTaskService#getCookie fail:{}", JSON.parseObject(response.body())); } catch (Exception e) {
log.error("CronTaskService#createGroup getCookie,e:{},param:{},response:{}", Throwables.getStackTraceAsString(e)
, JSON.toJSONString(params), JSON.toJSONString(response));
}
return null; return null;
} }
} }

@ -80,7 +80,7 @@ public class MessageTemplateServiceImpl implements MessageTemplateService {
@Override @Override
public void copy(Long id) { public void copy(Long id) {
MessageTemplate messageTemplate = messageTemplateDao.findById(id).get(); MessageTemplate messageTemplate = messageTemplateDao.findById(id).get();
MessageTemplate clone = ObjectUtil.clone(messageTemplate).setId(null); MessageTemplate clone = ObjectUtil.clone(messageTemplate).setId(null).setCronTaskId(null);
messageTemplateDao.save(clone); messageTemplateDao.save(clone);
} }

Loading…
Cancel
Save