You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
2.3 KiB
Java
55 lines
2.3 KiB
Java
package com.supervision.utils;
|
|
|
|
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|
import org.apache.hc.core5.http.io.entity.StringEntity;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
/**
|
|
* @Author: JCccc
|
|
* @Date: 2022-6-22 9:51
|
|
* @Description:
|
|
*/
|
|
@Component
|
|
public class XxlJobUtil {
|
|
private static String cookie = "";
|
|
@Value("${xxl.job.admin.addresses}")
|
|
private String xxlJobAdminAddress;
|
|
|
|
public void executeTask(String taskName) throws Exception {
|
|
String taskQueryUrl = xxlJobAdminAddress + "/xxl-job-admin/api/jobinfo/load";
|
|
String taskExecuteUrl = xxlJobAdminAddress + "/xxl-job-admin/api/job/trigger";
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
// 查询任务
|
|
HttpGet httpGet = new HttpGet(taskQueryUrl + "?jobName=" + taskName);
|
|
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
|
|
String jsonResponse = EntityUtils.toString(response.getEntity());
|
|
// 处理任务信息,例如提取 jobId
|
|
// 假设 jsonResponse 中有一个字段 "jobId"
|
|
String jobId = extractJobId(jsonResponse);
|
|
|
|
// 执行任务
|
|
HttpPost httpPost = new HttpPost(taskExecuteUrl);
|
|
httpPost.setHeader("Content-Type", "application/json");
|
|
String jsonBody = "{\"jobId\": \"" + jobId + "\"}";
|
|
httpPost.setEntity(new StringEntity(jsonBody));
|
|
try (CloseableHttpResponse executeResponse = httpClient.execute(httpPost)) {
|
|
System.out.println("Task executed: " + EntityUtils.toString(executeResponse.getEntity()));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private String extractJobId(String jsonResponse) {
|
|
// 解析 JSON 响应并提取 jobId
|
|
// 这里需要使用 JSON 解析库,比如 Jackson 或 Gson
|
|
// 示例代码略
|
|
return "yourJobId"; // 请替换为实际提取的 jobId
|
|
}
|
|
} |