From 4fe84ac084dad43e2ee68abc1d170261cf288fd4 Mon Sep 17 00:00:00 2001 From: chaiyapeng <3535863041@qq.com> Date: Fri, 26 Jul 2024 20:07:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 46 ++++++++++++++ pom.xml | 37 +++++++++++ .../com/muyu/common/xxl/XXLJobConfig.java | 28 +++++++++ .../com/muyu/common/xxl/XxlJobProperties.java | 63 +++++++++++++++++++ .../common/xxl/demo/XxlJobDemoService.java | 38 +++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 3 + 6 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/muyu/common/xxl/XXLJobConfig.java create mode 100644 src/main/java/com/muyu/common/xxl/XxlJobProperties.java create mode 100644 src/main/java/com/muyu/common/xxl/demo/XxlJobDemoService.java create mode 100644 src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09bdfea --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +###################################################################### +# Build Tools + +.gradle +/build/ +!gradle/wrapper/gradle-wrapper.jar + +target/ +!.mvn/wrapper/maven-wrapper.jar + +###################################################################### +# IDE + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### JRebel ### +rebel.xml +### NetBeans ### +nbproject/private/ +build/* +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ + +###################################################################### +# Others +*.log +*.xml.versionsBackup +*.swp + +!*/build/*.java +!*/build/*.html +!*/build/*.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4af9214 --- /dev/null +++ b/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + com.muyu + cloud-common + 3.6.3 + + + cloud-common-xxl + + XllJob定时任务 + + + 17 + 17 + UTF-8 + + + + + + + com.muyu + cloud-common-core + + + + + com.xuxueli + xxl-job-core + + + + \ No newline at end of file diff --git a/src/main/java/com/muyu/common/xxl/XXLJobConfig.java b/src/main/java/com/muyu/common/xxl/XXLJobConfig.java new file mode 100644 index 0000000..0bf4362 --- /dev/null +++ b/src/main/java/com/muyu/common/xxl/XXLJobConfig.java @@ -0,0 +1,28 @@ +package com.muyu.common.xxl; + +import com.muyu.common.core.utils.StringUtils; +import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; +import lombok.extern.log4j.Log4j2; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +@Log4j2 +@Component +public class XXLJobConfig { + @Bean + public XxlJobSpringExecutor xxlJobExecutor(XxlJobProperties xxlJobProperties) { + if (StringUtils.isEmpty(xxlJobProperties.getAdminAddresses())){ + throw new RuntimeException("请在bootstrap.yml当中配置shared-configs项,xxl-job共享配置[application-xxl-config]"); + } + XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); + xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses()); + xxlJobSpringExecutor.setAppname(xxlJobProperties.getAppName()); + xxlJobSpringExecutor.setIp(xxlJobProperties.getIp()); + xxlJobSpringExecutor.setPort(xxlJobProperties.getPort()); + xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken()); + xxlJobSpringExecutor.setLogPath(xxlJobProperties.getLogPath()); + xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays()); + log.info(">>>>>>>>>>> xxl-job config init success."); + return xxlJobSpringExecutor; + } +} diff --git a/src/main/java/com/muyu/common/xxl/XxlJobProperties.java b/src/main/java/com/muyu/common/xxl/XxlJobProperties.java new file mode 100644 index 0000000..c1428f7 --- /dev/null +++ b/src/main/java/com/muyu/common/xxl/XxlJobProperties.java @@ -0,0 +1,63 @@ +package com.muyu.common.xxl; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Data +@Configuration +@ConfigurationProperties(prefix = "xxl-job") +public class XxlJobProperties { + + /** + * 调度中心部署根地址 [选填]: + * 如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调"; + * 为空则关闭自动注册; + */ + private String adminAddresses; + + /** + * 执行器通讯TOKEN [选填]: + * 非空时启用; + */ + private String accessToken; + + /** + * 执行器AppName [选填]: + * 执行器心跳注册分组依据;为空则关闭自动注册 + */ + private String appName; + + /** + * 执行器注册 [选填]: + * 优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。 + * 从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。 + */ + private String address; + + /** + * 执行器IP [选填]: + * 默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用; + * 地址信息用于 "执行器注册" 和 "调度中心请求并触发任务"; + */ + private String ip; + + /** + * 执行器端口号 [选填]: + * 小于等于0则自动获取;默认端口为9999, + * 单机部署多个执行器时,注意要配置不同执行器端口; + */ + private int port; + + /** + * 执行器运行日志文件存储磁盘路径 [选填]: + * 需要对该路径拥有读写权限;为空则使用默认路径; + */ + private String logPath; + + /** + * 执行器日志文件保存天数 [选填]: + * 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能; + */ + private int logRetentionDays; +} diff --git a/src/main/java/com/muyu/common/xxl/demo/XxlJobDemoService.java b/src/main/java/com/muyu/common/xxl/demo/XxlJobDemoService.java new file mode 100644 index 0000000..51cd2a4 --- /dev/null +++ b/src/main/java/com/muyu/common/xxl/demo/XxlJobDemoService.java @@ -0,0 +1,38 @@ +package com.muyu.common.xxl.demo; + +import com.xxl.job.core.context.XxlJobHelper; +import com.xxl.job.core.handler.annotation.XxlJob; +import lombok.extern.log4j.Log4j2; +import org.springframework.stereotype.Component; + +@Log4j2 +@Component +public class XxlJobDemoService { + + /** + * 无参测试 + */ + @XxlJob("xxl-job-demo-no-param") + public void xxlJobDemoNoParam(){ + log.info("调度名称:[{}] - 无参", "xxl-job-demo-no-param"); + } + + /** + * 有参测试 + */ + @XxlJob("xxl-job-demo-one-param") + public void xxlJobDemoOneParam(){ + String param = XxlJobHelper.getJobParam(); + log.info("调度名称:[{}] - 参数:[{}]", "xxl-job-demo-one-param", param); + } + /** + * 分片测试 + */ + @XxlJob("xxl-job-slice-demo") + public void xxlJobSliceDemo(){ + int shardTotal = XxlJobHelper.getShardTotal(); + int shardIndex = XxlJobHelper.getShardIndex(); + String param = XxlJobHelper.getJobParam(); + log.info("调度名称:[{}-[{}/{}(分片位置/分片总数)]] - 参数:[{}]", "xxl-job-slice-demo",shardIndex, shardTotal, param); + } +} diff --git a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..1404a51 --- /dev/null +++ b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,3 @@ +com.muyu.common.xxl.XXLJobConfig +com.muyu.common.xxl.XxlJobProperties +com.muyu.common.xxl.demo.XxlJobDemoService \ No newline at end of file