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.core.util.StrUtil;
|
||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpResponse;
|
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.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用XXL‑JOB后台页面接口(抓包获取 /jobinfo/*)
|
* 调用XXL‑JOB后台页面接口
|
||||||
* 鉴权依赖JSESSIONID Cookie,非官方openapi;老版本xxl‑job无新增任务openapi
|
* 列表接口返回JSON,不再返回HTML
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class XxlJobOpenApiUtil {
|
public class XxlJobOpenApiUtil {
|
||||||
@@ -23,10 +23,6 @@ public class XxlJobOpenApiUtil {
|
|||||||
private final String adminUrl;
|
private final String adminUrl;
|
||||||
private final String cookie;
|
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) {
|
public XxlJobOpenApiUtil(String adminUrl, String cookie) {
|
||||||
this.adminUrl = StrUtil.removeSuffix(adminUrl, "/");
|
this.adminUrl = StrUtil.removeSuffix(adminUrl, "/");
|
||||||
this.cookie = cookie;
|
this.cookie = cookie;
|
||||||
@@ -42,13 +38,13 @@ public class XxlJobOpenApiUtil {
|
|||||||
try {
|
try {
|
||||||
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form(formMap)
|
.form(formMap)
|
||||||
.execute();
|
.execute();
|
||||||
int status = response.getStatus();
|
int status = response.getStatus();
|
||||||
log.info("[XxlJob] addJob status:{}", status);
|
log.info("[XxlJob] addJob status:{}", status);
|
||||||
// 新增成功302重定向到列表页
|
// 新增成功302重定向到列表页
|
||||||
return status == 200;
|
return status == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] addJob异常", e);
|
log.error("[XxlJob] addJob异常", e);
|
||||||
return false;
|
return false;
|
||||||
@@ -57,6 +53,7 @@ public class XxlJobOpenApiUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据【任务描述+执行器参数】查询jobId
|
* 根据【任务描述+执行器参数】查询jobId
|
||||||
|
* 适配返回JSON格式的pageList接口
|
||||||
* @param jobDesc 任务描述
|
* @param jobDesc 任务描述
|
||||||
* @param executorParam 任务参数(业务shipperId)
|
* @param executorParam 任务参数(业务shipperId)
|
||||||
* @return jobId,查不到返回null
|
* @return jobId,查不到返回null
|
||||||
@@ -65,25 +62,34 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/pageList";
|
String url = adminUrl + "/jobinfo/pageList";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("jobGroup", "")
|
.form("jobGroup", "")
|
||||||
.form("jobDesc", jobDesc)
|
.form("jobDesc", jobDesc)
|
||||||
.form("executorHandler", "")
|
.form("executorHandler", "")
|
||||||
.form("executorParam", executorParam)
|
.form("executorParam", executorParam)
|
||||||
.form("triggerStatus", "-1")
|
.form("triggerStatus", "-1")
|
||||||
|
.form("start", 0)
|
||||||
|
.form("length", 10)
|
||||||
.execute();
|
.execute();
|
||||||
String html = response.body();
|
|
||||||
if (StrUtil.isBlank(html)) {
|
String jsonStr = response.body();
|
||||||
|
if (StrUtil.isBlank(jsonStr)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Document doc = Jsoup.parse(html);
|
JSONObject jsonObj = JSON.parseObject(jsonStr);
|
||||||
Elements trList = doc.select("tr");
|
JSONArray jsonArray = jsonObj.getJSONArray("data");
|
||||||
for (Element tr : trList) {
|
if(jsonArray == null){
|
||||||
String dataId = tr.attr("data-id");
|
return null;
|
||||||
if (StrUtil.isBlank(dataId)) {
|
}
|
||||||
continue;
|
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;
|
return null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -92,6 +98,7 @@ public class XxlJobOpenApiUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 暂停任务 POST /jobinfo/pause
|
* 暂停任务 POST /jobinfo/pause
|
||||||
* @param jobId xxl任务id
|
* @param jobId xxl任务id
|
||||||
@@ -104,10 +111,10 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/pause";
|
String url = adminUrl + "/jobinfo/pause";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("id", jobId)
|
.form("id", jobId)
|
||||||
.execute();
|
.execute();
|
||||||
return response.getStatus() == 200;
|
return response.getStatus() == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] pauseJob jobId:{}异常", jobId, e);
|
log.error("[XxlJob] pauseJob jobId:{}异常", jobId, e);
|
||||||
return false;
|
return false;
|
||||||
@@ -126,10 +133,10 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/start";
|
String url = adminUrl + "/jobinfo/start";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("id", jobId)
|
.form("id", jobId)
|
||||||
.execute();
|
.execute();
|
||||||
return response.getStatus() == 200;
|
return response.getStatus() == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] startJob jobId:{}异常", jobId, e);
|
log.error("[XxlJob] startJob jobId:{}异常", jobId, e);
|
||||||
return false;
|
return false;
|
||||||
@@ -148,10 +155,10 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/remove";
|
String url = adminUrl + "/jobinfo/remove";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("id", jobId)
|
.form("id", jobId)
|
||||||
.execute();
|
.execute();
|
||||||
return response.getStatus() == 200;
|
return response.getStatus() == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] removeJob jobId:{}异常", jobId, e);
|
log.error("[XxlJob] removeJob jobId:{}异常", jobId, e);
|
||||||
return false;
|
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
|
@Data
|
||||||
public static class JobInfoAddReq {
|
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.core.util.StrUtil;
|
||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpResponse;
|
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.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用XXL‑JOB后台页面接口(抓包获取 /jobinfo/*)
|
* 调用XXL‑JOB后台页面接口
|
||||||
* 鉴权依赖JSESSIONID Cookie,非官方openapi;老版本xxl‑job无新增任务openapi
|
* 列表接口返回JSON,不再返回HTML
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class XxlJobOpenApiUtil {
|
public class XxlJobOpenApiUtil {
|
||||||
@@ -23,10 +23,6 @@ public class XxlJobOpenApiUtil {
|
|||||||
private final String adminUrl;
|
private final String adminUrl;
|
||||||
private final String cookie;
|
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) {
|
public XxlJobOpenApiUtil(String adminUrl, String cookie) {
|
||||||
this.adminUrl = StrUtil.removeSuffix(adminUrl, "/");
|
this.adminUrl = StrUtil.removeSuffix(adminUrl, "/");
|
||||||
this.cookie = cookie;
|
this.cookie = cookie;
|
||||||
@@ -42,13 +38,13 @@ public class XxlJobOpenApiUtil {
|
|||||||
try {
|
try {
|
||||||
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form(formMap)
|
.form(formMap)
|
||||||
.execute();
|
.execute();
|
||||||
int status = response.getStatus();
|
int status = response.getStatus();
|
||||||
log.info("[XxlJob] addJob status:{}", status);
|
log.info("[XxlJob] addJob status:{}", status);
|
||||||
// 新增成功302重定向到列表页
|
// 新增成功302重定向到列表页
|
||||||
return status == 200;
|
return status == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] addJob异常", e);
|
log.error("[XxlJob] addJob异常", e);
|
||||||
return false;
|
return false;
|
||||||
@@ -57,6 +53,7 @@ public class XxlJobOpenApiUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据【任务描述+执行器参数】查询jobId
|
* 根据【任务描述+执行器参数】查询jobId
|
||||||
|
* 适配返回JSON格式的pageList接口
|
||||||
* @param jobDesc 任务描述
|
* @param jobDesc 任务描述
|
||||||
* @param executorParam 任务参数(业务shipperId)
|
* @param executorParam 任务参数(业务shipperId)
|
||||||
* @return jobId,查不到返回null
|
* @return jobId,查不到返回null
|
||||||
@@ -65,25 +62,34 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/pageList";
|
String url = adminUrl + "/jobinfo/pageList";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("jobGroup", "")
|
.form("jobGroup", "")
|
||||||
.form("jobDesc", jobDesc)
|
.form("jobDesc", jobDesc)
|
||||||
.form("executorHandler", "")
|
.form("executorHandler", "")
|
||||||
.form("executorParam", executorParam)
|
.form("executorParam", executorParam)
|
||||||
.form("triggerStatus", "-1")
|
.form("triggerStatus", "-1")
|
||||||
|
.form("start", 0)
|
||||||
|
.form("length", 10)
|
||||||
.execute();
|
.execute();
|
||||||
String html = response.body();
|
|
||||||
if (StrUtil.isBlank(html)) {
|
String jsonStr = response.body();
|
||||||
|
if (StrUtil.isBlank(jsonStr)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Document doc = Jsoup.parse(html);
|
JSONObject jsonObj = JSON.parseObject(jsonStr);
|
||||||
Elements trList = doc.select("tr");
|
JSONArray jsonArray = jsonObj.getJSONArray("data");
|
||||||
for (Element tr : trList) {
|
if(jsonArray == null){
|
||||||
String dataId = tr.attr("data-id");
|
return null;
|
||||||
if (StrUtil.isBlank(dataId)) {
|
}
|
||||||
continue;
|
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;
|
return null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -92,6 +98,7 @@ public class XxlJobOpenApiUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 暂停任务 POST /jobinfo/pause
|
* 暂停任务 POST /jobinfo/pause
|
||||||
* @param jobId xxl任务id
|
* @param jobId xxl任务id
|
||||||
@@ -104,10 +111,10 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/pause";
|
String url = adminUrl + "/jobinfo/pause";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("id", jobId)
|
.form("id", jobId)
|
||||||
.execute();
|
.execute();
|
||||||
return response.getStatus() == 200;
|
return response.getStatus() == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] pauseJob jobId:{}异常", jobId, e);
|
log.error("[XxlJob] pauseJob jobId:{}异常", jobId, e);
|
||||||
return false;
|
return false;
|
||||||
@@ -126,10 +133,10 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/start";
|
String url = adminUrl + "/jobinfo/start";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("id", jobId)
|
.form("id", jobId)
|
||||||
.execute();
|
.execute();
|
||||||
return response.getStatus() == 200;
|
return response.getStatus() == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] startJob jobId:{}异常", jobId, e);
|
log.error("[XxlJob] startJob jobId:{}异常", jobId, e);
|
||||||
return false;
|
return false;
|
||||||
@@ -148,10 +155,10 @@ public class XxlJobOpenApiUtil {
|
|||||||
String url = adminUrl + "/jobinfo/remove";
|
String url = adminUrl + "/jobinfo/remove";
|
||||||
try {
|
try {
|
||||||
HttpResponse response = HttpRequest.post(url)
|
HttpResponse response = HttpRequest.post(url)
|
||||||
.header("Authorization", cookie)
|
.header("Cookie", cookie)
|
||||||
.form("id", jobId)
|
.form("id", jobId)
|
||||||
.execute();
|
.execute();
|
||||||
return response.getStatus() == 200;
|
return response.getStatus() == 302;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[XxlJob] removeJob jobId:{}异常", jobId, e);
|
log.error("[XxlJob] removeJob jobId:{}异常", jobId, e);
|
||||||
return false;
|
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
|
@Data
|
||||||
public static class JobInfoAddReq {
|
public static class JobInfoAddReq {
|
||||||
|
|||||||
Reference in New Issue
Block a user