From 02a0b7aab6b6a1f58ee3c5e1bac5fd180a727fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=A5=8E=E5=85=B4?= <2220574228@qq.com> Date: Wed, 26 Aug 2026 14:08:24 +0800 Subject: [PATCH] xxl-job --- .../mhd/system/api/XxlJobServiceFeign.java | 23 ++++++++ .../RemoteXxlJobFeignFallbackFactory.java | 44 +++++++++++++++ .../core/constant/ServiceNameConstants.java | 3 ++ mhd_wms/pom.xml | 5 ++ .../stockInOrder/StockInOrderApi.java | 34 ++++++++++++ .../java/com/mhd/wms/util/XxlJobConfig.java | 54 +++++++++++++++++++ mhd_wms/src/main/resources/bootstrap.yml | 24 ++++++++- 7 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/XxlJobServiceFeign.java create mode 100644 mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/factory/RemoteXxlJobFeignFallbackFactory.java create mode 100644 mhd_wms/src/main/java/com/mhd/wms/util/XxlJobConfig.java diff --git a/mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/XxlJobServiceFeign.java b/mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/XxlJobServiceFeign.java new file mode 100644 index 000000000..7010ba691 --- /dev/null +++ b/mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/XxlJobServiceFeign.java @@ -0,0 +1,23 @@ +package com.mhd.system.api; + + +import com.mhd.common.core.constant.ServiceNameConstants; +import com.mhd.common.core.domain.entity.R; +import com.mhd.common.core.domain.dto.YmsSysUserDTO; +import com.mhd.system.api.domain.YmsLoginUser; +import com.mhd.system.api.factory.RemoteBmsFeignFallbackFactory; +import com.mhd.system.api.factory.RemoteXxlJobFeignFallbackFactory; +import com.mhd.system.api.feign.FeignAutoConfiguration; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +@FeignClient(contextId = "XxlJobService", value = ServiceNameConstants.XXL_JOB_SERVICE,url = "http://127.0.0.1:8020",fallbackFactory = RemoteXxlJobFeignFallbackFactory.class, configuration = FeignAutoConfiguration.class) +public interface XxlJobServiceFeign { + + + @GetMapping("/user/openFeignTest") + public R> openFeignTest(@RequestParam("param") String string); + +} \ No newline at end of file diff --git a/mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/factory/RemoteXxlJobFeignFallbackFactory.java b/mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/factory/RemoteXxlJobFeignFallbackFactory.java new file mode 100644 index 000000000..90c72b51c --- /dev/null +++ b/mhd-api/mhd-api-system/src/main/java/com/mhd/system/api/factory/RemoteXxlJobFeignFallbackFactory.java @@ -0,0 +1,44 @@ +package com.mhd.system.api.factory; + +import com.mhd.common.core.domain.dto.BusinessDataPushDTO; +import com.mhd.common.core.domain.entity.R; +import com.mhd.common.core.domain.po.OrganizationPo; +import com.mhd.common.core.web.domain.AjaxResult; +import com.mhd.system.api.BmsServiceFeign; +import com.mhd.system.api.ProductServiceFeign; +import com.mhd.system.api.XxlJobServiceFeign; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + + +/** + * @Description + * @Author zg + * @Version 1.0 + * @Date 2023/11/3 14:37 + */ +@Component +public class RemoteXxlJobFeignFallbackFactory implements FallbackFactory { + + private static final Logger log = LoggerFactory.getLogger(RemoteXxlJobFeignFallbackFactory.class); + + @Override + public XxlJobServiceFeign create(Throwable throwable) { + log.error("系统服务调用失败:{}", throwable.getMessage()); + return new XxlJobServiceFeign() { + + @Override + public R> openFeignTest(String string) { + return null; + } + }; + } +} \ No newline at end of file diff --git a/mhd-common/mhd-common-core/src/main/java/com/mhd/common/core/constant/ServiceNameConstants.java b/mhd-common/mhd-common-core/src/main/java/com/mhd/common/core/constant/ServiceNameConstants.java index 326d15114..a2deb4eac 100644 --- a/mhd-common/mhd-common-core/src/main/java/com/mhd/common/core/constant/ServiceNameConstants.java +++ b/mhd-common/mhd-common-core/src/main/java/com/mhd/common/core/constant/ServiceNameConstants.java @@ -66,6 +66,9 @@ public class ServiceNameConstants //YMS系统 public static final String YMS_SERVICE = "mhd-yms-service"; + //YMS系统 + public static final String XXL_JOB_SERVICE = "xxl-job-service"; + } \ No newline at end of file diff --git a/mhd_wms/pom.xml b/mhd_wms/pom.xml index c8afa6a88..cc6b6b9f1 100644 --- a/mhd_wms/pom.xml +++ b/mhd_wms/pom.xml @@ -80,6 +80,11 @@ opencc4j 1.8.1 + + com.xuxueli + xxl-job-core + 2.4.0 + com.mhd diff --git a/mhd_wms/src/main/java/com/mhd/wms/interfaces/facadeApi/stockInOrder/StockInOrderApi.java b/mhd_wms/src/main/java/com/mhd/wms/interfaces/facadeApi/stockInOrder/StockInOrderApi.java index b90157e39..81871c182 100644 --- a/mhd_wms/src/main/java/com/mhd/wms/interfaces/facadeApi/stockInOrder/StockInOrderApi.java +++ b/mhd_wms/src/main/java/com/mhd/wms/interfaces/facadeApi/stockInOrder/StockInOrderApi.java @@ -1,11 +1,13 @@ package com.mhd.wms.interfaces.facadeApi.stockInOrder; +import com.mhd.common.core.domain.entity.R; import com.mhd.common.core.web.controller.BaseController; import com.mhd.common.core.utils.bean.BeanUtils; import com.mhd.common.core.web.domain.AjaxResult; import com.mhd.common.core.web.page.TableDataInfo; import com.mhd.common.core.exception.ServiceException; import com.mhd.common.core.utils.StringUtils; +import com.mhd.system.api.XxlJobServiceFeign; import com.mhd.wms.application.service.stockInOrder.StockInOrderApplicationService; import com.mhd.wms.domain.inMaterialDetail.entity.InMaterialDetail; import com.mhd.wms.domain.inMaterialDetail.repository.po.InMaterialDetailPO; @@ -20,6 +22,8 @@ import com.mhd.wms.interfaces.assember.stockOutOrder.StockOutOrderAssembler; import com.mhd.wms.interfaces.dto.inMaterialDetail.InMaterialDetailDTO; import com.mhd.wms.interfaces.dto.stockInOrder.StockInOrderDTO; import com.mhd.wms.interfaces.dto.stockOutOrder.StockOutOrderDTO; +import com.xxl.job.core.context.XxlJobHelper; +import com.xxl.job.core.handler.annotation.XxlJob; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -49,6 +53,8 @@ public class StockInOrderApi extends BaseController { private StockInOrderAssembler stockInOrderAssembler; @Resource private StockOutOrderAssembler stockOutOrderAssembler; + @Autowired + private XxlJobServiceFeign xxlJobServiceFeign; /** * 分页查询入库单列表 @@ -347,4 +353,32 @@ public class StockInOrderApi extends BaseController { return stockInOrderApplicationService.batchSignature(dto); } + @GetMapping("/xxlopenfeign") + public AjaxResult xxlopenfeign(@RequestParam String param) { + R> mapR = xxlJobServiceFeign.openFeignTest(param); + return AjaxResult.success(mapR); + } + + /** + * 1. 简单任务示例(Bean模式) + * 这里的 demoJobHandler 必须和调度中心配置一致 + */ + @XxlJob("demoJobHandler") + public void demoJobHandler() { + // 获取任务参数 + String param = XxlJobHelper.getJobParam(); + logger.info("XXL-JOB 开始执行,参数:{}", param); + + try { + // ========== 你的业务逻辑 ========== + logger.info("执行若依微服务定时任务..."); + + // 设置任务执行结果 + XxlJobHelper.handleSuccess("执行成功"); + } catch (Exception e) { + logger.error("XXL-JOB 执行失败", e); + XxlJobHelper.handleFail("执行失败:" + e.getMessage()); + } + } + } \ No newline at end of file diff --git a/mhd_wms/src/main/java/com/mhd/wms/util/XxlJobConfig.java b/mhd_wms/src/main/java/com/mhd/wms/util/XxlJobConfig.java new file mode 100644 index 000000000..9627d0e7b --- /dev/null +++ b/mhd_wms/src/main/java/com/mhd/wms/util/XxlJobConfig.java @@ -0,0 +1,54 @@ +package com.mhd.wms.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; + } +} \ No newline at end of file diff --git a/mhd_wms/src/main/resources/bootstrap.yml b/mhd_wms/src/main/resources/bootstrap.yml index fc7380c20..e178f54e2 100644 --- a/mhd_wms/src/main/resources/bootstrap.yml +++ b/mhd_wms/src/main/resources/bootstrap.yml @@ -34,4 +34,26 @@ spring: file-extension: yml # 共享配置 shared-configs: - - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} \ No newline at end of file + - 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 \ No newline at end of file