jobid查询接口;
This commit is contained in:
+46
-29
@@ -4,18 +4,18 @@ import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 调用XXL‑JOB后台页面接口(抓包获取 /jobinfo/*)
|
||||
* 鉴权依赖JSESSIONID Cookie,非官方openapi;老版本xxl‑job无新增任务openapi
|
||||
* 调用XXL‑JOB后台页面接口
|
||||
* 列表接口返回JSON,不再返回HTML
|
||||
*/
|
||||
@Slf4j
|
||||
public class XxlJobOpenApiUtil {
|
||||
@@ -23,10 +23,6 @@ public class XxlJobOpenApiUtil {
|
||||
private final String adminUrl;
|
||||
private final String cookie;
|
||||
|
||||
/**
|
||||
* @param adminUrl xxl‑job admin地址 示例:http://127.0.0.1:8080/xxl-job-admin
|
||||
* @param cookie 浏览器登录后的完整Cookie(JSESSIONID=xxx)
|
||||
*/
|
||||
public XxlJobOpenApiUtil(String adminUrl, String cookie) {
|
||||
this.adminUrl = StrUtil.removeSuffix(adminUrl, "/");
|
||||
this.cookie = cookie;
|
||||
@@ -42,13 +38,13 @@ public class XxlJobOpenApiUtil {
|
||||
try {
|
||||
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form(formMap)
|
||||
.execute();
|
||||
int status = response.getStatus();
|
||||
log.info("[XxlJob] addJob status:{}", status);
|
||||
// 新增成功302重定向到列表页
|
||||
return status == 200;
|
||||
return status == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] addJob异常", e);
|
||||
return false;
|
||||
@@ -57,6 +53,7 @@ public class XxlJobOpenApiUtil {
|
||||
|
||||
/**
|
||||
* 根据【任务描述+执行器参数】查询jobId
|
||||
* 适配返回JSON格式的pageList接口
|
||||
* @param jobDesc 任务描述
|
||||
* @param executorParam 任务参数(业务shipperId)
|
||||
* @return jobId,查不到返回null
|
||||
@@ -65,25 +62,34 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/pageList";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("jobGroup", "")
|
||||
.form("jobDesc", jobDesc)
|
||||
.form("executorHandler", "")
|
||||
.form("executorParam", executorParam)
|
||||
.form("triggerStatus", "-1")
|
||||
.form("start", 0)
|
||||
.form("length", 10)
|
||||
.execute();
|
||||
String html = response.body();
|
||||
if (StrUtil.isBlank(html)) {
|
||||
|
||||
String jsonStr = response.body();
|
||||
if (StrUtil.isBlank(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
Document doc = Jsoup.parse(html);
|
||||
Elements trList = doc.select("tr");
|
||||
for (Element tr : trList) {
|
||||
String dataId = tr.attr("data-id");
|
||||
if (StrUtil.isBlank(dataId)) {
|
||||
continue;
|
||||
JSONObject jsonObj = JSON.parseObject(jsonStr);
|
||||
JSONArray jsonArray = jsonObj.getJSONArray("data");
|
||||
if(jsonArray == null){
|
||||
return null;
|
||||
}
|
||||
List<JobPageItem> dataList = jsonArray.toList(JobPageItem.class);
|
||||
if (dataList == null || dataList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
// 精确匹配jobDesc和executorParam,返回第一条id
|
||||
for (JobPageItem item : dataList) {
|
||||
if (StrUtil.equals(item.jobDesc, jobDesc) && StrUtil.equals(item.executorParam, executorParam)) {
|
||||
return item.id;
|
||||
}
|
||||
return Integer.parseInt(dataId);
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
@@ -92,6 +98,7 @@ public class XxlJobOpenApiUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 暂停任务 POST /jobinfo/pause
|
||||
* @param jobId xxl任务id
|
||||
@@ -104,10 +111,10 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/pause";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("id", jobId)
|
||||
.execute();
|
||||
return response.getStatus() == 200;
|
||||
return response.getStatus() == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] pauseJob jobId:{}异常", jobId, e);
|
||||
return false;
|
||||
@@ -126,10 +133,10 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/start";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("id", jobId)
|
||||
.execute();
|
||||
return response.getStatus() == 200;
|
||||
return response.getStatus() == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] startJob jobId:{}异常", jobId, e);
|
||||
return false;
|
||||
@@ -148,10 +155,10 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/remove";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("id", jobId)
|
||||
.execute();
|
||||
return response.getStatus() == 200;
|
||||
return response.getStatus() == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] removeJob jobId:{}异常", jobId, e);
|
||||
return false;
|
||||
@@ -159,7 +166,17 @@ public class XxlJobOpenApiUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求实体,与浏览器F12抓包payload字段一一对应
|
||||
* pageList 返回data数组中每一项实体
|
||||
*/
|
||||
@Data
|
||||
public static class JobPageItem {
|
||||
private Integer id;
|
||||
private String jobDesc;
|
||||
private String executorParam;
|
||||
}
|
||||
|
||||
/**
|
||||
* add接口请求实体,与浏览器F12抓包payload字段一一对应
|
||||
*/
|
||||
@Data
|
||||
public static class JobInfoAddReq {
|
||||
|
||||
+46
-29
@@ -4,18 +4,18 @@ import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 调用XXL‑JOB后台页面接口(抓包获取 /jobinfo/*)
|
||||
* 鉴权依赖JSESSIONID Cookie,非官方openapi;老版本xxl‑job无新增任务openapi
|
||||
* 调用XXL‑JOB后台页面接口
|
||||
* 列表接口返回JSON,不再返回HTML
|
||||
*/
|
||||
@Slf4j
|
||||
public class XxlJobOpenApiUtil {
|
||||
@@ -23,10 +23,6 @@ public class XxlJobOpenApiUtil {
|
||||
private final String adminUrl;
|
||||
private final String cookie;
|
||||
|
||||
/**
|
||||
* @param adminUrl xxl‑job admin地址 示例:http://127.0.0.1:8080/xxl-job-admin
|
||||
* @param cookie 浏览器登录后的完整Cookie(JSESSIONID=xxx)
|
||||
*/
|
||||
public XxlJobOpenApiUtil(String adminUrl, String cookie) {
|
||||
this.adminUrl = StrUtil.removeSuffix(adminUrl, "/");
|
||||
this.cookie = cookie;
|
||||
@@ -42,13 +38,13 @@ public class XxlJobOpenApiUtil {
|
||||
try {
|
||||
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form(formMap)
|
||||
.execute();
|
||||
int status = response.getStatus();
|
||||
log.info("[XxlJob] addJob status:{}", status);
|
||||
// 新增成功302重定向到列表页
|
||||
return status == 200;
|
||||
return status == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] addJob异常", e);
|
||||
return false;
|
||||
@@ -57,6 +53,7 @@ public class XxlJobOpenApiUtil {
|
||||
|
||||
/**
|
||||
* 根据【任务描述+执行器参数】查询jobId
|
||||
* 适配返回JSON格式的pageList接口
|
||||
* @param jobDesc 任务描述
|
||||
* @param executorParam 任务参数(业务shipperId)
|
||||
* @return jobId,查不到返回null
|
||||
@@ -65,25 +62,34 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/pageList";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("jobGroup", "")
|
||||
.form("jobDesc", jobDesc)
|
||||
.form("executorHandler", "")
|
||||
.form("executorParam", executorParam)
|
||||
.form("triggerStatus", "-1")
|
||||
.form("start", 0)
|
||||
.form("length", 10)
|
||||
.execute();
|
||||
String html = response.body();
|
||||
if (StrUtil.isBlank(html)) {
|
||||
|
||||
String jsonStr = response.body();
|
||||
if (StrUtil.isBlank(jsonStr)) {
|
||||
return null;
|
||||
}
|
||||
Document doc = Jsoup.parse(html);
|
||||
Elements trList = doc.select("tr");
|
||||
for (Element tr : trList) {
|
||||
String dataId = tr.attr("data-id");
|
||||
if (StrUtil.isBlank(dataId)) {
|
||||
continue;
|
||||
JSONObject jsonObj = JSON.parseObject(jsonStr);
|
||||
JSONArray jsonArray = jsonObj.getJSONArray("data");
|
||||
if(jsonArray == null){
|
||||
return null;
|
||||
}
|
||||
List<JobPageItem> dataList = jsonArray.toList(JobPageItem.class);
|
||||
if (dataList == null || dataList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
// 精确匹配jobDesc和executorParam,返回第一条id
|
||||
for (JobPageItem item : dataList) {
|
||||
if (StrUtil.equals(item.jobDesc, jobDesc) && StrUtil.equals(item.executorParam, executorParam)) {
|
||||
return item.id;
|
||||
}
|
||||
return Integer.parseInt(dataId);
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
@@ -92,6 +98,7 @@ public class XxlJobOpenApiUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 暂停任务 POST /jobinfo/pause
|
||||
* @param jobId xxl任务id
|
||||
@@ -104,10 +111,10 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/pause";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("id", jobId)
|
||||
.execute();
|
||||
return response.getStatus() == 200;
|
||||
return response.getStatus() == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] pauseJob jobId:{}异常", jobId, e);
|
||||
return false;
|
||||
@@ -126,10 +133,10 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/start";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("id", jobId)
|
||||
.execute();
|
||||
return response.getStatus() == 200;
|
||||
return response.getStatus() == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] startJob jobId:{}异常", jobId, e);
|
||||
return false;
|
||||
@@ -148,10 +155,10 @@ public class XxlJobOpenApiUtil {
|
||||
String url = adminUrl + "/jobinfo/remove";
|
||||
try {
|
||||
HttpResponse response = HttpRequest.post(url)
|
||||
.header("Authorization", cookie)
|
||||
.header("Cookie", cookie)
|
||||
.form("id", jobId)
|
||||
.execute();
|
||||
return response.getStatus() == 200;
|
||||
return response.getStatus() == 302;
|
||||
} catch (Exception e) {
|
||||
log.error("[XxlJob] removeJob jobId:{}异常", jobId, e);
|
||||
return false;
|
||||
@@ -159,7 +166,17 @@ public class XxlJobOpenApiUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求实体,与浏览器F12抓包payload字段一一对应
|
||||
* pageList 返回data数组中每一项实体
|
||||
*/
|
||||
@Data
|
||||
public static class JobPageItem {
|
||||
private Integer id;
|
||||
private String jobDesc;
|
||||
private String executorParam;
|
||||
}
|
||||
|
||||
/**
|
||||
* add接口请求实体,与浏览器F12抓包payload字段一一对应
|
||||
*/
|
||||
@Data
|
||||
public static class JobInfoAddReq {
|
||||
|
||||
Reference in New Issue
Block a user