java通过代码动态向xxl-job-admin添加任务怎么写
时间: 2023-07-01 16:04:29 浏览: 230
可以通过以下步骤实现在Java代码中动态向xxl-job-admin添加任务:
1. 引入xxl-job的依赖:
```xml
<dependency>
<groupId>com.xxl-job</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${xxl-job.version}</version>
</dependency>
```
2. 创建一个Http请求,向xxl-job-admin发送添加任务的请求。可以使用Apache HttpClient或者OkHttp等第三方库来发送Http请求。
```java
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://xxl-job-admin:8080/xxl-job-admin/jobinfo/add");
```
3. 构造请求参数,将任务的信息以JSON格式放入请求体中。
```java
JSONObject jobInfo = new JSONObject();
jobInfo.put("jobGroup", 1);
jobInfo.put("jobCron", "0 0/1 * * * ?");
jobInfo.put("jobDesc", "测试任务");
jobInfo.put("author", "test");
jobInfo.put("executorRouteStrategy", "FIRST");
jobInfo.put("executorHandler", "testHandler");
jobInfo.put("executorParam", "");
jobInfo.put("executorBlockStrategy", "SERIAL_EXECUTION");
jobInfo.put("executorFailStrategy", "FAIL_RETRY");
jobInfo.put("glueType", "BEAN");
jobInfo.put("glueRemark", "");
jobInfo.put("glueUpdatetime", new Date());
jobInfo.put("childJobId", "");
jobInfo.put("triggerStatus", 1);
StringEntity entity = new StringEntity(jobInfo.toJSONString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
```
4. 发送请求,并获取响应结果。
```java
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
JSONObject responseJson = JSONObject.parseObject(result);
int code = responseJson.getIntValue("code");
if (code == 200) {
System.out.println("任务添加成功!");
} else {
System.out.println("任务添加失败!");
}
}
```
完整代码示例:
```java
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;
public class AddJobDemo {
public static void main(String[] args) throws Exception {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://xxl-job-admin:8080/xxl-job-admin/jobinfo/add");
JSONObject jobInfo = new JSONObject();
jobInfo.put("jobGroup", 1);
jobInfo.put("jobCron", "0 0/1 * * * ?");
jobInfo.put("jobDesc", "测试任务");
jobInfo.put("author", "test");
jobInfo.put("executorRouteStrategy", "FIRST");
jobInfo.put("executorHandler", "testHandler");
jobInfo.put("executorParam", "");
jobInfo.put("executorBlockStrategy", "SERIAL_EXECUTION");
jobInfo.put("executorFailStrategy", "FAIL_RETRY");
jobInfo.put("glueType", "BEAN");
jobInfo.put("glueRemark", "");
jobInfo.put("glueUpdatetime", new Date());
jobInfo.put("childJobId", "");
jobInfo.put("triggerStatus", 1);
StringEntity entity = new StringEntity(jobInfo.toJSONString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
JSONObject responseJson = JSONObject.parseObject(result);
int code = responseJson.getIntValue("code");
if (code == 200) {
System.out.println("任务添加成功!");
} else {
System.out.println("任务添加失败!");
}
}
}
}
```
阅读全文