依赖
<!--xxljob-->
<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.1</version>
</dependency>
<!--org.apache.commons.httpclient-->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
1、XxlJobClient-模拟登录
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xxljob.config.XxlJobClientConfigProperties;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @Author ygq
* @Date 2024年4月24日
*/
@Component
public class XxlJobClient {
private String COOKIE = "";
private HttpClient httpClient;
private static final String POST_FORM_CONTENT_TYPE = "application/x-www-form-urlencoded; charset=UTF-8";
private static final Header POST_FORM_CONTENT_TYPE_HEADER = new Header("Content-Type", POST_FORM_CONTENT_TYPE);
@Autowired
private XxlJobClientConfigProperties clientConfigProperties;
private final Logger log = LoggerFactory.getLogger(getClass());
@PostConstruct
public void init() throws IOException {
log.debug("xxl JOB 初始化配置:{}", clientConfigProperties.toString());
httpClient = new HttpClient();
login();
}
/**
* 登录获取 cookie
*
* @throws IOException
*/
private void login() throws IOException {
HttpMethod postMethod = new PostMethod(clientConfigProperties.getLoginUrl());
httpClient.executeMethod(postMethod);
if (postMethod.getStatusCode() == 200) {
Cookie[] cookies = httpClient.getState().getCookies();
StringBuilder tmpCookies = new StringBuilder();
for (Cookie c : cookies) {
tmpCookies.append(c.toString()).append(";");
}
COOKIE = tmpCookies.toString();
log.debug("xxlJob 登录成功");
} else {
log.debug("xxlJob 登录失败:{}", postMethod.getStatusCode());
}
}
/**
* 创建任务
*
* @param params
* @return
* @throws IOException
*/
public JSONObject createJob(JSONObject params) throws IOException {
return doPost(clientConfigProperties.getJobInfoAddUrl(), params);
}
/**
* 更新任务
*
* @param params
* @return
* @throws IOException
*/
public JSONObject updateJob(JSONObject params) throws IOException {
return doPost(clientConfigProperties.getJobInfoUpdateUrl(), params);
}
/**
* 根据任务 ID 加载
*
* @param id
* @return
* @throws IOException
*/
public JSONObject loadById(int id) throws IOException {
log.info("loadById: {}", id);
return doGet(String.format(clientConfigProperties.getJobInfoLoadByIdUrl(), id));
}
/**
* 删除任务
*
* @param id 任务 ID
* @return
* @throws IOException
*/
public JSONObject deleteJob(int id) throws IOException {
log.info("deleteJob: {}", id);
return doGet(String.format(clientConfigProperties.getJobInfoDeleteUrl(), id));
}
/**
* 开启任务
*
* @param id 任务 ID
* @return
* @throws IOException
*/
public JSONObject startJob(int id) throws IOException {
log.info("startJob: {}", id);
return doGet(String.format(clientConfigProperties.getJobInfoStartJobUrl(), id));
}
/**
* 执行一次
*
* @param id 任务 ID
* @return
* @throws IOException
*/
public JSONObject startJobOnce(JSONObject params) throws IOException {
return doGet(String.format(clientConfigProperties.getJhobInfoTriggerUrl(), params));
}
/**
* 停止任务
*
* @param id 任务 ID
* @return
* @throws IOException
*/
public JSONObject stopJob(int id) throws IOException {
log.info("stopJob: {}", id);
return doGet(String.format(clientConfigProperties.getJobInfoStopJobUrl(), id));
}
/**
* 创建执行器
*
* @param params
* @return
* @throws IOException
*/
public JSONObject createJobGroup(JSONObject params) throws IOException {
return doPost(clientConfigProperties.getJobGroupSaveUrl(), params);
}
/**
* 删除执行器
*
* @param params
* @return
* @throws IOException
*/
public JSONObject removeJobGroup(JSONObject params) throws IOException {
return doPost(clientConfigProperties.getJobGroupRemoveUrl(), params);
}
/**
* 执行器列表
*
* @param params
* @return
* @throws IOException
*/
public JSONObject jobGroupPageList(JSONObject params) throws IOException {
params.put("start", Optional.ofNullable(params.getInteger("start")).orElse(0));
params.put("length", Optional.ofNullable(params.getInteger("length")).orElse(10));
return doPost(clientConfigProperties.getJobGroupPageListUrl(), params);
}
/**
* 根据appname获取执行器Id
*
* @param appName
* @return
* @throws IOException
*/
public JSONObject getJobGroupByAppName(String appName) throws IOException {
return doPost(clientConfigProperties.getJobGroupGetByAppNameUrl()+"?appName="+appName, new JSONObject());
}
/**
* 任务列表
*
* @param params
* @return
* @throws IOException
*/
public JSONObject jobInfoPageList(JSONObject params) throws IOException {
params.put("start", Optional.ofNullable(params.getInteger("start")).orElse(0));
params.put("length", Optional.ofNullable(params.getInteger("length")).orElse(10));
return doPost(clientConfigProperties.getJobInfoPageListUrl(), p