前言
由于XXL-Job本身不支持批量执行的功能,想要批量执行就比较困扰,以下就是基于Java编写的封装了批量开启Job和关闭Job的工具类
如上,每次需要点击操作并点击确定才能更改一个job状态
打开F12找到对应的开启或关闭的接口,复制对应的url和Cookie
使用时将后台程序访问url和cookie填入代码中,然后填入对应想要开启或者关闭的任务ID组即可,横杠表示区间,逗号表示并列,如5-2,10表示依次执行5,4,3,2,10号任务执行
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
/**
* @Author fanjw
* @Description
* @Date 2024/3/22 13:19
*/
@Slf4j
public class XXLJobUtil {
static String url = "【Your_XXL_Job_URL】";//xxl-job后台服务地址
static String stopUri = "/jobinfo/stop";//停止job接口
static String openUri = "/jobinfo/open";//启动job接口
static Map<String, String> header = new HashMap<>();
static{
header.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
header.put("Cookie", "【Your_XXL_Job_COOKIE】");
}
public static void main(String[] args) {
jobExcludeBatch("2325-2291,412-386");
}
/**
* 批量任务执行
* @param input 输入的任务ID组,横杠表示区间,逗号表示并列,如5-2,10表示依次执行5,4,3,2,10号任务执行
*/
private static void jobExcludeBatch(String input){
List<Integer> idList = parseString(input);
int currentNum=0,total=idList.size();
log.info("批量任务开始执行");
for (Integer integer : idList) {
currentNum++;
log.info("当前id={},当前执行进度:{}/{}",integer,currentNum,total);
jobExclude(integer);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.info("批量任务执行完毕!");
}
/**
* 单次任务执行
* @param id
*/
private static void jobExclude(Integer id) {
String param = "id=" + id;
String result = "";
try {
result = XXLJobUtil.sendPost(url + stopUri, header, param + "");
HttpResponseDto response = JSONObject.parseObject(result, HttpResponseDto.class);
if (response != null && response.getCode() == 200) {
return;
} else {
log.error("error ! id={},result={}", id, result);
}
}catch (Exception e){
log.error("Exception ! id={},result={}", id, result);
e.printStackTrace();
}
}
public static List<Integer> parseString(String input) {
List<Integer> resultList = new ArrayList<>();
Map<Integer, Boolean> map = new HashMap<>();
String[] parts = input.split(",");
for (String part : parts) {
if (part.contains("-")) {
String[] rangeParts = part.split("-");
int start = Integer.parseInt(rangeParts[0]);
int end = Integer.parseInt(rangeParts[1]);
if (start <= end) {
for (int i = start; i <= end; i++) {
if (!map.containsKey(i)) {
map.put(i, true);
resultList.add(i);
}
}
} else {
for (int i = start; i >= end; i--) {
if (!map.containsKey(i)) {
map.put(i, true);
resultList.add(i);
}
}
}
} else {
int num = Integer.parseInt(part);
if (!map.containsKey(num)) {
map.put(num, true);
resultList.add(num);
}
}
}
return resultList;
}
/**
*
* @param url 请求路径(非null)
* @param headers 请求头(可为null)
* @param params 请求体body(可为null)
* @return 响应体
*/
public static String sendGet(String url, Map<String,String> headers, String params) {
String result = "";
BufferedReader in = null;
try {
String urlName = url;
if(params!=null){
urlName =url + "?" + params;
}
URL realUrl = new URL(urlName);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
//conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
if(headers!=null) {
Set<String> keySets = headers.keySet();
for (String key : keySets) {
conn.setRequestProperty(key, headers.get(key));
}
}
conn.connect();
Map<String, List<String>> map = conn.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (Exception e) {
System.out.println("发送get请求异常" + e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
/**
*
* @param url 请求路径(非null)
* @param headers 请求头(可为null)
* @param params 请求体body(可为null)
* @return 响应体
*/
public static String sendPost(String url, Map<String, String> headers, String params) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL readUrl = new URL(url);
URLConnection conn = readUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
if(headers!=null) {
Set<String> keySets = headers.keySet();
for (String key : keySets) {
conn.setRequestProperty(key, headers.get(key));
}
}
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
if(params!=null) {
out.print(params);
}
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
} catch (Exception e) {
System.out.println("发送POST请求异常" + e);
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
/**
* 响应返回对象
* @param <T>
*/
@Data
public static class HttpResponseDto<T>{
Integer code;
String msg;
T content;
}
}
测试执行结果如下: