xxl-job-rest
This commit is contained in:
@@ -36,12 +36,12 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>com.alibaba</groupId>
|
<!-- <groupId>com.alibaba</groupId>-->
|
||||||
<artifactId>fastjson</artifactId>
|
<!-- <artifactId>fastjson</artifactId>-->
|
||||||
<version>1.2.78</version>
|
<!-- <version>1.2.78</version>-->
|
||||||
<scope>compile</scope>
|
<!-- <scope>compile</scope>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp3</groupId>
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package com.wyl.springbootxxjob;
|
||||||
|
|
||||||
|
import com.wyl.springbootxxjob.config.JobServerConfig;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@Import(JobServerConfig.class)
|
||||||
|
public @interface EnableXxljobRest {
|
||||||
|
}
|
||||||
+32
-24
@@ -1,46 +1,54 @@
|
|||||||
package com.wyl.springbootxxjob.config;
|
package com.wyl.springbootxxjob.config;
|
||||||
|
|
||||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
import com.wyl.springbootxxjob.service.DynamicXxlJobService;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
|
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Getter
|
@Getter
|
||||||
|
@Slf4j
|
||||||
public class JobServerConfig {
|
public class JobServerConfig {
|
||||||
|
|
||||||
@Value("${job.server.admin.addresses}")
|
@Value("${xxl-job.http.serve.admin.addresses}")
|
||||||
private String adminAddresses;
|
private String adminAddresses;
|
||||||
|
|
||||||
@Value("${job.server.accessToken}")
|
@Value("${xxl-job.http.job.server.userName}")
|
||||||
private String accessToken;
|
|
||||||
|
|
||||||
@Value("${job.server.executor.appname}")
|
|
||||||
private String appname;
|
|
||||||
|
|
||||||
@Value("${job.server.executor.address}")
|
|
||||||
private String address;
|
|
||||||
|
|
||||||
@Value("${job.server.userName}")
|
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
@Value("${job.server.password}")
|
@Value("${xxl-job.http.job.server.password}")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@Value("${job.server.jobGroup}")
|
@Bean("xxJobRestTemplate")
|
||||||
private String jobGroup;
|
public RestTemplate restTemplate() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
|
||||||
|
return restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
DynamicXxlJobService dynamicXxlJobService() {
|
||||||
log.info(">>>>>>>>>>> job-server config init.");
|
return new DynamicXxlJobService();
|
||||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
}
|
||||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
|
||||||
xxlJobSpringExecutor.setAppname(appname);
|
/**
|
||||||
xxlJobSpringExecutor.setAddress(address);
|
* 使用OkHttpClient作为底层客户端
|
||||||
xxlJobSpringExecutor.setAccessToken(accessToken);
|
*
|
||||||
return xxlJobSpringExecutor;
|
* @return
|
||||||
|
*/
|
||||||
|
private ClientHttpRequestFactory getClientHttpRequestFactory() {
|
||||||
|
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(5, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(5, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
return new OkHttp3ClientHttpRequestFactory(okHttpClient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
package com.wyl.springbootxxjob.config;
|
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
|
||||||
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class RestTemplateConfig {
|
|
||||||
|
|
||||||
@ConditionalOnMissingBean(RestTemplate.class)
|
|
||||||
@Bean
|
|
||||||
public RestTemplate restTemplate() {
|
|
||||||
RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
|
|
||||||
return restTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用OkHttpClient作为底层客户端
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private ClientHttpRequestFactory getClientHttpRequestFactory() {
|
|
||||||
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS)
|
|
||||||
.writeTimeout(5, TimeUnit.SECONDS)
|
|
||||||
.readTimeout(5, TimeUnit.SECONDS)
|
|
||||||
.build();
|
|
||||||
return new OkHttp3ClientHttpRequestFactory(okHttpClient);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package com.wyl.springbootxxjob.job;
|
||||||
|
|
||||||
|
import com.xxl.job.core.context.XxlJobHelper;
|
||||||
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.sound.midi.Soundbank;
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XxlJob开发示例(Bean模式)
|
||||||
|
* <p>
|
||||||
|
* 开发步骤:
|
||||||
|
* 1、任务开发:在Spring Bean实例中,开发Job方法;
|
||||||
|
* 2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
|
||||||
|
* 3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志;
|
||||||
|
* 4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果;
|
||||||
|
*
|
||||||
|
* @author xuxueli 2019-12-11 21:52:51
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class SampleXxlJob {
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1、简单任务示例(Bean模式)
|
||||||
|
*/
|
||||||
|
@XxlJob("wylDemoHandler")
|
||||||
|
public void demoJobHandler() throws Exception {
|
||||||
|
XxlJobHelper.log("XXL-JOB, Hello World.");
|
||||||
|
System.out.println(XxlJobHelper.getJobParam());
|
||||||
|
System.out.println(XxlJobHelper.getJobId());
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
XxlJobHelper.log("beat at:" + i);
|
||||||
|
TimeUnit.SECONDS.sleep(2);
|
||||||
|
}
|
||||||
|
// default success
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
package com.wyl.springbootxxjob.job;
|
||||||
|
|
||||||
|
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* xxl-job config
|
||||||
|
*
|
||||||
|
* @author xuxueli 2017-04-28
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class XxlJobConfig {
|
||||||
|
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
|
||||||
|
|
||||||
|
@Value("${xxl.job.admin.addresses}")
|
||||||
|
private String adminAddresses;
|
||||||
|
|
||||||
|
@Value("${xxl.job.accessToken}")
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
|
@Value("${xxl.job.executor.appname}")
|
||||||
|
private String appname;
|
||||||
|
|
||||||
|
@Value("${xxl.job.executor.address}")
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
@Value("${xxl.job.executor.ip}")
|
||||||
|
private String ip;
|
||||||
|
|
||||||
|
@Value("${xxl.job.executor.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Value("${xxl.job.executor.logpath}")
|
||||||
|
private String logPath;
|
||||||
|
|
||||||
|
@Value("${xxl.job.executor.logretentiondays}")
|
||||||
|
private int logRetentionDays;
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||||
|
logger.info(">>>>>>>>>>> xxl-job config init.");
|
||||||
|
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||||
|
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
||||||
|
xxlJobSpringExecutor.setAppname(appname);
|
||||||
|
xxlJobSpringExecutor.setAddress(address);
|
||||||
|
xxlJobSpringExecutor.setIp(ip);
|
||||||
|
xxlJobSpringExecutor.setPort(port);
|
||||||
|
xxlJobSpringExecutor.setAccessToken(accessToken);
|
||||||
|
xxlJobSpringExecutor.setLogPath(logPath);
|
||||||
|
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
|
||||||
|
return xxlJobSpringExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
|
||||||
|
*
|
||||||
|
* 1、引入依赖:
|
||||||
|
* <dependency>
|
||||||
|
* <groupId>org.springframework.cloud</groupId>
|
||||||
|
* <artifactId>spring-cloud-commons</artifactId>
|
||||||
|
* <version>${version}</version>
|
||||||
|
* </dependency>
|
||||||
|
*
|
||||||
|
* 2、配置文件,或者容器启动变量
|
||||||
|
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
|
||||||
|
*
|
||||||
|
* 3、获取IP
|
||||||
|
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
package com.wyl.springbootxxjob.obj;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: wangyl
|
||||||
|
* @date: 2022/4/27
|
||||||
|
* @description: 创建任务对象
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class JobEntity {
|
||||||
|
/**
|
||||||
|
* 任务id 修改是需要填写
|
||||||
|
*/
|
||||||
|
Integer id;
|
||||||
|
/**
|
||||||
|
* 执行器主键ID
|
||||||
|
*/
|
||||||
|
String jobGroup = "";
|
||||||
|
/**
|
||||||
|
* job描述
|
||||||
|
*/
|
||||||
|
String jobDesc = "";
|
||||||
|
/**
|
||||||
|
* 作者
|
||||||
|
*/
|
||||||
|
String author = "xxl-job-rest";
|
||||||
|
/**
|
||||||
|
* 调度类型
|
||||||
|
*/
|
||||||
|
String scheduleType = "CRON";
|
||||||
|
/**
|
||||||
|
* 调度配置,值含义取决于调度类型
|
||||||
|
*/
|
||||||
|
String scheduleConf = "";
|
||||||
|
/**
|
||||||
|
* 调度过期策略
|
||||||
|
*/
|
||||||
|
String cronGenDisplay = "";
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
String scheduleConfCRON = "";
|
||||||
|
/**
|
||||||
|
* GLUE类型 #com.xxl.job.core.glue.GlueTypeEnum
|
||||||
|
*/
|
||||||
|
String glueType = "BEAN";
|
||||||
|
/**
|
||||||
|
* 执行器,任务Handler名称
|
||||||
|
*/
|
||||||
|
String executorHandler = "";
|
||||||
|
/**
|
||||||
|
* 执行器,任务参数
|
||||||
|
*/
|
||||||
|
String executorParam = "";
|
||||||
|
/**
|
||||||
|
* 执行器路由策略
|
||||||
|
*/
|
||||||
|
String executorRouteStrategy = "FIRST";
|
||||||
|
/**
|
||||||
|
* 调度过期策略
|
||||||
|
*/
|
||||||
|
String misfireStrategy = "DO_NOTHING";
|
||||||
|
/**
|
||||||
|
* 阻塞处理策略
|
||||||
|
*/
|
||||||
|
String executorBlockStrategy = "SERIAL_EXECUTION";
|
||||||
|
/**
|
||||||
|
* 任务执行超时时间,单位秒
|
||||||
|
*/
|
||||||
|
String executorTimeout = "0";
|
||||||
|
/**
|
||||||
|
* 失败重试次数
|
||||||
|
*/
|
||||||
|
String executorFailRetryCount = "0";
|
||||||
|
/**
|
||||||
|
* GLUE备注
|
||||||
|
*/
|
||||||
|
String glueRemark = "";
|
||||||
|
|
||||||
|
public MultiValueMap<String, String> makeParam() {
|
||||||
|
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
||||||
|
paramMap.add("jobGroup", jobGroup);
|
||||||
|
paramMap.add("jobDesc", jobDesc);
|
||||||
|
paramMap.add("author", author);
|
||||||
|
paramMap.add("scheduleType", scheduleType);
|
||||||
|
paramMap.add("scheduleConf", scheduleConf);
|
||||||
|
paramMap.add("cronGen_display", cronGenDisplay);
|
||||||
|
paramMap.add("glueType", glueType);
|
||||||
|
paramMap.add("executorHandler", executorHandler);
|
||||||
|
paramMap.add("executorParam", executorParam);
|
||||||
|
paramMap.add("executorRouteStrategy", executorRouteStrategy);
|
||||||
|
paramMap.add("misfireStrategy", misfireStrategy);
|
||||||
|
paramMap.add("executorBlockStrategy", executorBlockStrategy);
|
||||||
|
paramMap.add("executorTimeout", executorTimeout);
|
||||||
|
paramMap.add("executorFailRetryCount", executorFailRetryCount);
|
||||||
|
paramMap.add("glueRemark", glueRemark);
|
||||||
|
paramMap.add("schedule_conf_CRON", scheduleConfCRON);
|
||||||
|
if (id != null) {
|
||||||
|
paramMap.add("id", id.toString());
|
||||||
|
}
|
||||||
|
return paramMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
package com.wyl.springbootxxjob.obj;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: wangyl
|
||||||
|
* @date: 2022/4/27
|
||||||
|
* @description: 立即执行任务参数
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class JobTriggerEntity {
|
||||||
|
/**
|
||||||
|
* 任务id
|
||||||
|
*/
|
||||||
|
Integer id;
|
||||||
|
/**
|
||||||
|
* 执行参数
|
||||||
|
*/
|
||||||
|
String executorParam;
|
||||||
|
/**
|
||||||
|
* 执行任务机器ip列表
|
||||||
|
*/
|
||||||
|
String addressList;
|
||||||
|
|
||||||
|
public MultiValueMap<String, String> makeParam() {
|
||||||
|
MultiValueMap<String, String> hashMap = new LinkedMultiValueMap<>();
|
||||||
|
hashMap.add("id", id.toString());
|
||||||
|
hashMap.add("executorParam", executorParam);
|
||||||
|
hashMap.add("addressList", addressList);
|
||||||
|
return hashMap;
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
package com.wyl.springbootxxjob.obj;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: wangyl
|
||||||
|
* @date: 2022/4/27
|
||||||
|
* @description: xx-job 登录对象
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class LoginEntity {
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
String userName = "";
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
String password = "";
|
||||||
|
|
||||||
|
public MultiValueMap<String, String> makeParam() {
|
||||||
|
MultiValueMap<String, String> hashMap = new LinkedMultiValueMap<>();
|
||||||
|
hashMap.add("userName", userName);
|
||||||
|
hashMap.add("password", password);
|
||||||
|
return hashMap;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.wyl.springbootxxjob.obj;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author: wangyl
|
||||||
|
* @date: 2022/4/27
|
||||||
|
* @description: xx-job请求返回对象
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Result<T> {
|
||||||
|
/**
|
||||||
|
* 操作吗
|
||||||
|
*/
|
||||||
|
private Integer code;
|
||||||
|
/**
|
||||||
|
* 错误信息
|
||||||
|
*/
|
||||||
|
private String msg;
|
||||||
|
/**
|
||||||
|
* 结果信息
|
||||||
|
*/
|
||||||
|
private T content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 操作成功
|
||||||
|
*/
|
||||||
|
public Boolean succeed() {
|
||||||
|
return 200 == code;
|
||||||
|
}
|
||||||
|
}
|
||||||
+180
-88
@@ -1,139 +1,231 @@
|
|||||||
package com.wyl.springbootxxjob.service;
|
package com.wyl.springbootxxjob.service;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.wyl.springbootxxjob.config.JobServerConfig;
|
import com.wyl.springbootxxjob.config.JobServerConfig;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import com.wyl.springbootxxjob.obj.JobEntity;
|
||||||
import org.springframework.http.HttpEntity;
|
import com.wyl.springbootxxjob.obj.JobTriggerEntity;
|
||||||
import org.springframework.http.HttpHeaders;
|
import com.wyl.springbootxxjob.obj.LoginEntity;
|
||||||
import org.springframework.http.MediaType;
|
import com.wyl.springbootxxjob.obj.Result;
|
||||||
import org.springframework.http.ResponseEntity;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@Slf4j
|
||||||
public class DynamicXxlJobService {
|
public class DynamicXxlJobService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录url
|
||||||
|
*/
|
||||||
|
static private String LOGIN_URL;
|
||||||
|
/**
|
||||||
|
* 创建任务的url
|
||||||
|
*/
|
||||||
|
static private String CREATE_JOB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建任务的url
|
||||||
|
*/
|
||||||
|
static private String UPDATE_JOB;
|
||||||
|
/**
|
||||||
|
* 删除任务的url
|
||||||
|
*/
|
||||||
|
static private String REMOVE_JOB;
|
||||||
|
/**
|
||||||
|
* 启动任务的url
|
||||||
|
*/
|
||||||
|
static private String START_JOB;
|
||||||
|
/**
|
||||||
|
* 停止任务的url
|
||||||
|
*/
|
||||||
|
static private String STOP_JOB;
|
||||||
|
/**
|
||||||
|
* 立即执行任务url
|
||||||
|
*/
|
||||||
|
static private String TRIGGER_JOB;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
LOGIN_URL = jobServerConfig.getAdminAddresses() + "/login";
|
||||||
|
CREATE_JOB = jobServerConfig.getAdminAddresses() + "/jobinfo/add";
|
||||||
|
UPDATE_JOB = jobServerConfig.getAdminAddresses() + "/jobinfo/update";
|
||||||
|
REMOVE_JOB = jobServerConfig.getAdminAddresses() + "/jobinfo/remove";
|
||||||
|
START_JOB = jobServerConfig.getAdminAddresses() + "/jobinfo/start";
|
||||||
|
STOP_JOB = jobServerConfig.getAdminAddresses() + "/jobinfo/stop";
|
||||||
|
TRIGGER_JOB = jobServerConfig.getAdminAddresses() + "/jobinfo/trigger";
|
||||||
|
}
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private JobServerConfig jobServerConfig;
|
private JobServerConfig jobServerConfig;
|
||||||
|
|
||||||
@Autowired
|
@Resource(name = "xxJobRestTemplate")
|
||||||
RestTemplate restTemplate;
|
RestTemplate restTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建固定任务
|
* 创建定时任务
|
||||||
*
|
*
|
||||||
* @param desc 任务描述
|
* @param jobEntity
|
||||||
* @param corn cron 表达式
|
* @return java.lang.Integer
|
||||||
* @param param param
|
* @Date 2022/4/27
|
||||||
* @return jobId
|
* @Author wangyl
|
||||||
*/
|
*/
|
||||||
private int createJob(String cookie, String desc, String corn, String param) {
|
public Integer createJob(JobEntity jobEntity) {
|
||||||
|
MultiValueMap<String, String> creatJonParam = jobEntity.makeParam();
|
||||||
int jobId = -1;
|
ResponseEntity<Result> responseEntity = this.postFrom(CREATE_JOB, creatJonParam, true);
|
||||||
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
Result body = responseEntity.getBody();
|
||||||
paramMap.add("jobGroup", jobServerConfig.getJobGroup());
|
if (!body.succeed()) {
|
||||||
paramMap.add("jobDesc", desc);
|
log.error("创建任务失败,参数为;" + creatJonParam + "错误为:" + body.getMsg());
|
||||||
paramMap.add("author", "system_patrol");
|
throw new RuntimeException();
|
||||||
paramMap.add("scheduleType", "CRON");
|
|
||||||
paramMap.add("scheduleConf", corn);
|
|
||||||
paramMap.add("cronGen_display", corn);
|
|
||||||
paramMap.add("glueType", "BEAN");
|
|
||||||
paramMap.add("executorHandler", "patrolGenerateHandler");
|
|
||||||
paramMap.add("executorParam", param);
|
|
||||||
paramMap.add("executorRouteStrategy", "FIRST");
|
|
||||||
paramMap.add("misfireStrategy", "DO_NOTHING");
|
|
||||||
paramMap.add("executorBlockStrategy", "SERIAL_EXECUTION");
|
|
||||||
paramMap.add("executorTimeout", "0");
|
|
||||||
paramMap.add("executorFailRetryCount", "0");
|
|
||||||
paramMap.add("glueRemark", "GLUE代码初始化");
|
|
||||||
paramMap.add("cookie", cookie);
|
|
||||||
String s = post("", paramMap);
|
|
||||||
JSONObject jsonObject = JSON.parseObject(s);
|
|
||||||
int code = jsonObject.getIntValue("code");
|
|
||||||
if (code == 200) {
|
|
||||||
jobId = jsonObject.getIntValue("content");
|
|
||||||
}
|
}
|
||||||
|
Object jobId = body.getContent();
|
||||||
return jobId;
|
return Integer.valueOf(jobId.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新定时任务信息
|
||||||
|
* @param jobEntity
|
||||||
|
* @return boolean
|
||||||
|
* @Date 2022/4/28
|
||||||
|
* @Author wangyl
|
||||||
|
*/
|
||||||
|
public boolean updateJob(JobEntity jobEntity) {
|
||||||
|
MultiValueMap<String, String> creatJonParam = jobEntity.makeParam();
|
||||||
|
ResponseEntity<Result> responseEntity = this.postFrom(UPDATE_JOB, creatJonParam, true);
|
||||||
|
Result body = responseEntity.getBody();
|
||||||
|
if (!body.succeed()) {
|
||||||
|
log.error("更新任务失败,参数为;" + creatJonParam + "错误为:" + body.getMsg());
|
||||||
|
throw new RuntimeException(body.getMsg());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean triggerJob(JobTriggerEntity jobTriggerEntity) {
|
||||||
|
MultiValueMap<String, String> jobTriggerParam = jobTriggerEntity.makeParam();
|
||||||
|
ResponseEntity<Result> responseEntity = this.postFrom(TRIGGER_JOB, jobTriggerParam, true);
|
||||||
|
Result body = responseEntity.getBody();
|
||||||
|
if (!body.succeed()) {
|
||||||
|
log.error("立即执行任务失败,参数为;" + jobTriggerEntity + "错误为:" + body.getMsg());
|
||||||
|
throw new RuntimeException();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动固定任务
|
* 启动固定任务
|
||||||
* @param cookie cookie
|
*
|
||||||
* @param jobId jobId
|
* @param jobId jobId
|
||||||
* @return 启动固定任务
|
* @return 启动固定任务
|
||||||
*/
|
*/
|
||||||
private boolean start(String cookie, int jobId) {
|
public boolean startJob(Integer jobId) {
|
||||||
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
||||||
paramMap.add("id", String.valueOf(jobId));
|
paramMap.add("id", String.valueOf(jobId));
|
||||||
paramMap.add("cookie", "cookie");
|
ResponseEntity<Result> responseEntity = postFrom(START_JOB, paramMap, true);
|
||||||
String s = post("", paramMap);
|
Result body = responseEntity.getBody();
|
||||||
JSONObject jsonObject = JSON.parseObject(s);
|
if (!body.succeed()) {
|
||||||
int code = jsonObject.getIntValue("code");
|
log.error("job:" + jobId + "启动失败,失败理由:" + body.getMsg());
|
||||||
if (code == 200) {
|
throw new RuntimeException(body.getMsg());//todo 替换异常对象
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止任务
|
||||||
|
*
|
||||||
|
* @param jobId
|
||||||
|
* @return boolean
|
||||||
|
* @Date 2022/4/27
|
||||||
|
* @Author wangyl
|
||||||
|
*/
|
||||||
|
public boolean stopJob(Integer jobId) {
|
||||||
|
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
||||||
|
paramMap.add("id", String.valueOf(jobId));
|
||||||
|
ResponseEntity<Result> responseEntity = postFrom(STOP_JOB, paramMap, true);
|
||||||
|
Result body = responseEntity.getBody();
|
||||||
|
if (!body.succeed()) {
|
||||||
|
log.error("job:" + jobId + "停止失败,失败理由:" + body.getMsg());
|
||||||
|
throw new RuntimeException(body.getMsg());//todo 替换异常对象
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除固定任务
|
* 删除指定任务
|
||||||
* @param cookie cookie
|
*
|
||||||
* @param jobId jobId
|
* @param jobId 任务id
|
||||||
* @return 启动固定任务
|
* @return boolean
|
||||||
|
* @Date 2022/4/27
|
||||||
|
* @Author wangyl
|
||||||
*/
|
*/
|
||||||
public boolean remove(String cookie, int jobId) {
|
public boolean removeJob(Integer jobId) {
|
||||||
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
|
||||||
paramMap.add("id", String.valueOf(jobId));
|
paramMap.add("id", String.valueOf(jobId));
|
||||||
String s = post("", paramMap);
|
ResponseEntity<Result> responseEntity = postFrom(REMOVE_JOB, paramMap, true);
|
||||||
JSONObject jsonObject = JSON.parseObject(s);
|
Result body = responseEntity.getBody();
|
||||||
int code = jsonObject.getInteger("code");
|
if (!body.succeed()) {
|
||||||
if (code == 200) {
|
log.error("job:" + jobId + "删除失败,失败理由:" + body.getMsg());
|
||||||
return true;
|
throw new RuntimeException(body.getMsg());//todo 替换异常对象
|
||||||
}
|
}
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取cookie
|
* 登录xx-job获取cookies
|
||||||
* @return 返回cookie值
|
*
|
||||||
|
* @param
|
||||||
|
* @return java.util.List<java.lang.String>
|
||||||
|
* @Date 2022/4/27
|
||||||
|
* @Author wangyl
|
||||||
*/
|
*/
|
||||||
public String getCookie() {
|
public List<String> getCookie() {
|
||||||
String path = jobServerConfig.getAdminAddresses() + "/login";
|
LoginEntity loginEntity = new LoginEntity();
|
||||||
|
loginEntity.setUserName(jobServerConfig.getUserName());
|
||||||
|
loginEntity.setPassword(jobServerConfig.getPassword());
|
||||||
|
MultiValueMap<String, String> loginParam = loginEntity.makeParam();
|
||||||
|
ResponseEntity<Result> responseEntity = this.postFrom(LOGIN_URL, loginParam, false);
|
||||||
|
Result result = responseEntity.getBody();
|
||||||
|
if (!result.succeed()) {
|
||||||
|
log.error("登录xx-job失败", result.getMsg());
|
||||||
|
throw new RuntimeException(result.getMsg());//todo 替换异常对象
|
||||||
|
}
|
||||||
|
List<String> cookies = responseEntity.getHeaders().get("Set-Cookie");
|
||||||
|
return cookies;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送post的 form 请求
|
||||||
|
*
|
||||||
|
* @param url 请求url
|
||||||
|
* @param hashMap 请求参数
|
||||||
|
* @return java.lang.String
|
||||||
|
* @Date 2022/4/27
|
||||||
|
* @Author wangyl
|
||||||
|
*/
|
||||||
|
public ResponseEntity<Result> postFrom(String url, MultiValueMap<String, String> hashMap, Boolean needCookies) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
List<String> cookies = new ArrayList<>();
|
||||||
|
if (needCookies && CollectionUtils.isEmpty(cookies)) {
|
||||||
|
cookies = getCookie();
|
||||||
|
headers.put(HttpHeaders.COOKIE, cookies);
|
||||||
|
}
|
||||||
// 请求头设置,x-www-form-urlencoded格式的数据
|
// 请求头设置,x-www-form-urlencoded格式的数据
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
||||||
|
|
||||||
//提交参数设置
|
|
||||||
MultiValueMap<String, String> hashMap = new LinkedMultiValueMap<>();
|
|
||||||
// 组装请求体
|
|
||||||
hashMap.add("userName", jobServerConfig.getUserName());
|
|
||||||
hashMap.add("password", jobServerConfig.getPassword());
|
|
||||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(hashMap, headers);
|
|
||||||
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(path, request, String.class);
|
|
||||||
return stringResponseEntity.toString();
|
|
||||||
//
|
|
||||||
// List<HttpCookie> cookies = response.getCookies();
|
|
||||||
// StringBuilder sb = new StringBuilder();
|
|
||||||
// for (HttpCookie cookie : cookies) {
|
|
||||||
// sb.append(cookie.toString());
|
|
||||||
// }
|
|
||||||
// return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String post(String url, MultiValueMap<String, String> hashMap) {
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(hashMap, headers);
|
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(hashMap, headers);
|
||||||
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, request, String.class);
|
ResponseEntity<Result> responseEntity = restTemplate.postForEntity(url, request, Result.class);
|
||||||
return stringResponseEntity.getBody();
|
if (!(responseEntity.getStatusCode() == HttpStatus.OK)) {
|
||||||
|
log.error("请求url:" + url + ",param:" + hashMap + " result:" + responseEntity);
|
||||||
|
throw new RuntimeException("");//TODO 修改为异常
|
||||||
|
}
|
||||||
|
return responseEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
job.server.admin.addresses=http://192.168.123.102:9090/xxl-job-admin
|
xxl-job.http.serve.admin.addresses=http://192.168.3.10:8080/xxl-job-admin/
|
||||||
job.server.accessToken=1
|
xxl-job.http.job.server.userName = admin
|
||||||
job.server.executor.appname= asd
|
xxl-job.http.job.server.password = 123456
|
||||||
job.server.executor.address= 192.168.123.148
|
|
||||||
job.server.userName=admin
|
|
||||||
job.server.password=123456
|
|
||||||
job.server.jobGroup=1
|
|
||||||
|
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
|
||||||
|
xxl.job.admin.addresses=http://192.168.3.10:8080/xxl-job-admin
|
||||||
|
xxl.job.accessToken=
|
||||||
|
xxl.job.executor.appname=wyl-first-test
|
||||||
|
xxl.job.executor.address=
|
||||||
|
xxl.job.executor.ip=
|
||||||
|
xxl.job.executor.port=9999
|
||||||
|
xxl.job.executor.logpath=./logs
|
||||||
|
xxl.job.executor.logretentiondays=30
|
||||||
|
|||||||
+70
-4
@@ -1,17 +1,83 @@
|
|||||||
package com.wyl.springbootxxjob;
|
package com.wyl.springbootxxjob;
|
||||||
|
|
||||||
|
import com.wyl.springbootxxjob.obj.JobEntity;
|
||||||
|
import com.wyl.springbootxxjob.obj.JobTriggerEntity;
|
||||||
import com.wyl.springbootxxjob.service.DynamicXxlJobService;
|
import com.wyl.springbootxxjob.service.DynamicXxlJobService;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
class SpringBootXxjobApplicationTests extends BaseTest{
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class SpringBootXxjobApplicationTests extends BaseTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
DynamicXxlJobService dynamicXxlJobService;
|
DynamicXxlJobService dynamicXxlJobService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void getCookiesTest() {
|
||||||
dynamicXxlJobService.getCookie();
|
List<String> cookie = dynamicXxlJobService.getCookie();
|
||||||
|
System.out.println(cookie);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createJobTest() {
|
||||||
|
JobEntity jobEntity = new JobEntity();
|
||||||
|
jobEntity.setJobGroup("2");
|
||||||
|
jobEntity.setJobDesc("测试项目"+ LocalDateTime.now());
|
||||||
|
jobEntity.setAuthor("wyl");
|
||||||
|
jobEntity.setScheduleConf("* * * * * ?");
|
||||||
|
jobEntity.setCronGenDisplay("* * * * * ?");
|
||||||
|
jobEntity.setGlueType("BEAN");
|
||||||
|
jobEntity.setExecutorHandler("wylDemoHandler");
|
||||||
|
jobEntity.setExecutorRouteStrategy("FIRST");
|
||||||
|
jobEntity.setMisfireStrategy("DO_NOTHING");
|
||||||
|
jobEntity.setExecutorTimeout("0");
|
||||||
|
jobEntity.setExecutorFailRetryCount("0");
|
||||||
|
jobEntity.setGlueRemark("wyl测试新建项目");
|
||||||
|
Integer job = dynamicXxlJobService.createJob(jobEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void updateJobTest() {
|
||||||
|
JobEntity jobEntity = new JobEntity();
|
||||||
|
jobEntity.setId(7);
|
||||||
|
jobEntity.setJobGroup("2");
|
||||||
|
jobEntity.setJobDesc("测试项目"+ LocalDateTime.now()+"更新");
|
||||||
|
jobEntity.setAuthor("wyl");
|
||||||
|
jobEntity.setScheduleConf("* * * * * ?");
|
||||||
|
jobEntity.setCronGenDisplay("* * * * * ?");
|
||||||
|
jobEntity.setGlueType("BEAN");
|
||||||
|
jobEntity.setExecutorHandler("wylDemoHandler");
|
||||||
|
jobEntity.setExecutorRouteStrategy("FIRST");
|
||||||
|
jobEntity.setMisfireStrategy("DO_NOTHING");
|
||||||
|
jobEntity.setExecutorTimeout("0");
|
||||||
|
jobEntity.setExecutorFailRetryCount("0");
|
||||||
|
jobEntity.setGlueRemark("wyl测试新建项目");
|
||||||
|
dynamicXxlJobService.updateJob(jobEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void triggerJobTest() {
|
||||||
|
JobTriggerEntity jobTriggerEntity = new JobTriggerEntity();
|
||||||
|
jobTriggerEntity.setId(5);
|
||||||
|
jobTriggerEntity.setExecutorParam("123");
|
||||||
|
dynamicXxlJobService.triggerJob(jobTriggerEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void startJobTest() {
|
||||||
|
boolean b = dynamicXxlJobService.startJob(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void stopJobJobTest() {
|
||||||
|
boolean b = dynamicXxlJobService.stopJob(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void removeJobTest() {
|
||||||
|
boolean b = dynamicXxlJobService.removeJob(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user