xxl配置类;
This commit is contained in:
@@ -0,0 +1,54 @@
|
|||||||
|
package com.mhd.basic.util;
|
||||||
|
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 配置类
|
||||||
|
*/
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+139
@@ -0,0 +1,139 @@
|
|||||||
|
package com.mhd.bms.domain.settlementCustomers.repository.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import cn.hutool.http.HttpResponse;
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XXL‑JOB OpenApi 工具类
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class XxlJobOpenApiUtil {
|
||||||
|
|
||||||
|
/** xxl‑job admin地址,例:http://127.0.0.1:8080/xxl‑job‑admin */
|
||||||
|
private final String adminUrl;
|
||||||
|
/** 系统配置里的AccessToken */
|
||||||
|
private final String accessToken;
|
||||||
|
|
||||||
|
public XxlJobOpenApiUtil(String adminUrl, String accessToken) {
|
||||||
|
this.adminUrl = adminUrl.endsWith("/") ? adminUrl.substring(0, adminUrl.length() -1) : adminUrl;
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增定时任务
|
||||||
|
* @param addReq 请求参数
|
||||||
|
* @return jobId 任务ID,返回null代表调用失败
|
||||||
|
*/
|
||||||
|
public Integer addJob(JobInfoAddReq addReq) {
|
||||||
|
String url = adminUrl + "/api/jobinfo/add";
|
||||||
|
try {
|
||||||
|
// 实体对象转map,解决form不能传bean的爆红
|
||||||
|
Map<String, Object> formMap = BeanUtil.beanToMap(addReq);
|
||||||
|
|
||||||
|
HttpResponse response = HttpRequest.post(url)
|
||||||
|
.header("XXL-JOB-ACCESS-TOKEN", accessToken)
|
||||||
|
.form(formMap) //传入map,不再传实体
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
String body = response.body();
|
||||||
|
log.info("xxl‑job addJob resp:{}", body);
|
||||||
|
XxlApiResp resp = JSONUtil.toBean(body, XxlApiResp.class);
|
||||||
|
if (resp != null && resp.getCode() == 200) {
|
||||||
|
return Integer.valueOf(resp.getContent().toString());
|
||||||
|
}
|
||||||
|
log.error("新增xxl‑job任务失败,resp:{}", body);
|
||||||
|
return null;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("调用xxl‑job addJob异常", e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除任务
|
||||||
|
* @param jobId 任务id
|
||||||
|
* @return true成功
|
||||||
|
*/
|
||||||
|
public boolean removeJob(Integer jobId) {
|
||||||
|
String url = adminUrl + "/api/jobinfo/remove";
|
||||||
|
try (HttpResponse response = HttpRequest.post(url)
|
||||||
|
.header("XXL‑JOB‑ACCESS‑TOKEN", accessToken)
|
||||||
|
.form("id", jobId)
|
||||||
|
.execute()) {
|
||||||
|
String body = response.body();
|
||||||
|
XxlApiResp resp = JSONUtil.toBean(body, XxlApiResp.class);
|
||||||
|
if (resp != null && resp.getCode() == 200) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
log.error("删除xxl‑job任务失败 jobId:{},resp:{}", jobId, body);
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("调用xxl‑job removeJob异常", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 暂停任务
|
||||||
|
*/
|
||||||
|
public boolean pauseJob(Integer jobId) {
|
||||||
|
String url = adminUrl + "/api/jobinfo/pause";
|
||||||
|
try (HttpResponse response = HttpRequest.post(url)
|
||||||
|
.header("XXL‑JOB‑ACCESS‑TOKEN", accessToken)
|
||||||
|
.form("id", jobId)
|
||||||
|
.execute()) {
|
||||||
|
String body = response.body();
|
||||||
|
XxlApiResp resp = JSONUtil.toBean(body, XxlApiResp.class);
|
||||||
|
return resp != null && resp.getCode() == 200;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("pauseJob异常", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------- 实体 ----------------
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class JobInfoAddReq {
|
||||||
|
/** 执行器组ID xxl_job_group.id */
|
||||||
|
private Integer jobGroup;
|
||||||
|
/** 任务描述 */
|
||||||
|
private String jobDesc;
|
||||||
|
/** 路由策略 ROUND:轮询 */
|
||||||
|
private String executorRouteStrategy = "ROUND";
|
||||||
|
/** cron表达式 23点 0 0 23 * * ? */
|
||||||
|
private String jobCron;
|
||||||
|
/** @XxlJob 注解handler名称 */
|
||||||
|
private String executorHandler;
|
||||||
|
/** 任务参数,这里存业务单号 */
|
||||||
|
private String executorParam;
|
||||||
|
/** 阻塞策略 SERIAL_EXECUTION单机串行 */
|
||||||
|
private String executorBlockStrategy = "SERIAL_EXECUTION";
|
||||||
|
/** 超时时间 0不限制 */
|
||||||
|
private Integer executorTimeout = 0;
|
||||||
|
/** 失败重试次数 */
|
||||||
|
private Integer executorFailRetryCount = 0;
|
||||||
|
/** BEAN模式 */
|
||||||
|
private String glueType = "BEAN";
|
||||||
|
private String glueSource = "";
|
||||||
|
private String glueRemark = "";
|
||||||
|
/** 1启用 0停止 */
|
||||||
|
private Integer triggerStatus = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class XxlApiResp {
|
||||||
|
private Integer code;
|
||||||
|
private Object content;
|
||||||
|
private String msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.mhd.bms.util;
|
||||||
|
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 配置类
|
||||||
|
*/
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,26 +34,4 @@ spring:
|
|||||||
file-extension: yml
|
file-extension: yml
|
||||||
# 共享配置
|
# 共享配置
|
||||||
shared-configs:
|
shared-configs:
|
||||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
# XXL-JOB 配置
|
|
||||||
xxl:
|
|
||||||
job:
|
|
||||||
# 调度中心地址(填你部署的地址)
|
|
||||||
admin:
|
|
||||||
addresses: http://192.168.1.6:8020/xxl-job-admin
|
|
||||||
# 执行器配置
|
|
||||||
executor:
|
|
||||||
# 执行器名称(自定义,后面调度中心要用到)
|
|
||||||
appname: wms-service-executor
|
|
||||||
# 执行器注册地址,默认0表示自动获取
|
|
||||||
address:
|
|
||||||
# 执行器IP,默认空自动获取
|
|
||||||
ip:
|
|
||||||
# 执行器端口号(每个微服务不能重复,建议9997/9998/9999)
|
|
||||||
port: 9997
|
|
||||||
# 执行器日志存放路径
|
|
||||||
logpath: /data/applogs/xxl-job/jobhandler
|
|
||||||
# 日志保留天数
|
|
||||||
logretentiondays: 30
|
|
||||||
# 调度中心通讯令牌(默认空,安全可设置)
|
|
||||||
accessToken: default_token
|
|
||||||
Reference in New Issue
Block a user