From 156ef31a99bd6bc9c87280020084116741511366 Mon Sep 17 00:00:00 2001 From: youjie <272855983@qq.com> Date: Thu, 23 May 2024 10:38:46 +0800 Subject: [PATCH] 增加配置参数 通过IP地址限制定时任务执行 --- huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java | 16 ++++++++++++++++ huaheng-wms-core/src/main/java/org/jeecg/modules/quartz/service/impl/QuartzJobServiceImpl.java | 10 ++++++---- huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/job/MyTriggerListener.java | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java | 1 + huaheng-wms-core/src/main/resources/application.yml | 4 ++-- 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/job/MyTriggerListener.java diff --git a/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java b/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java index 9a84f4e..27fbe0e 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java @@ -6,6 +6,10 @@ import java.util.Enumeration; import javax.transaction.SystemException; import org.jeecg.common.util.oConvertUtils; +import org.jeecg.modules.wms.monitor.job.MyTriggerListener; +import org.jeecg.utils.SpringUtils; +import org.quartz.Scheduler; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -14,6 +18,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.FilterType; @@ -53,6 +58,7 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { + "WEB External: \thttp://" + ip + ":" + port + path + "/index.html\n\t" + "API External: \thttp://" + ip + ":" + port + path + "/\n\n\t" + "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n\n\t" + "The following profiles are active: [" + profiles + "]\n" + "----------------------------------------------------------"); + } public static InetAddress getLocalHostExactAddress() { @@ -76,4 +82,14 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { } return null; } + + @Bean + public CommandLineRunner run() { + return args -> { + // 在这里编写启动时需要执行的代码 + MyTriggerListener myTriggerListener = new MyTriggerListener(); + SpringUtils.getBean(Scheduler.class).getListenerManager().addTriggerListener(myTriggerListener); + System.out.println("Spring Boot 已启动完成,可以在这里执行自定义的启动逻辑..."); + }; + } } diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/quartz/service/impl/QuartzJobServiceImpl.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/quartz/service/impl/QuartzJobServiceImpl.java index 1cfd76a..7c11dcd 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/modules/quartz/service/impl/QuartzJobServiceImpl.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/quartz/service/impl/QuartzJobServiceImpl.java @@ -1,7 +1,8 @@ package org.jeecg.modules.quartz.service.impl; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import lombok.extern.slf4j.Slf4j; +import java.util.Date; +import java.util.List; + import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.exception.JeecgBootException; import org.jeecg.common.util.DateUtils; @@ -13,8 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Date; -import java.util.List; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; + +import lombok.extern.slf4j.Slf4j; /** * @Description: 定时任务在线管理 diff --git a/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/job/MyTriggerListener.java b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/job/MyTriggerListener.java new file mode 100644 index 0000000..b306b55 --- /dev/null +++ b/huaheng-wms-core/src/main/java/org/jeecg/modules/wms/monitor/job/MyTriggerListener.java @@ -0,0 +1,52 @@ +package org.jeecg.modules.wms.monitor.job; + +import org.jeecg.JeecgSystemApplication; +import org.jeecg.modules.wms.config.parameterConfiguration.service.IParameterConfigurationService; +import org.jeecg.utils.SpringUtils; +import org.jeecg.utils.StringUtils; +import org.jeecg.utils.constant.QuantityConstant; +import org.quartz.JobExecutionContext; +import org.quartz.Trigger; +import org.quartz.TriggerListener; + +import lombok.extern.slf4j.Slf4j; + +/** + * @author 游杰 + */ +@Slf4j +public class MyTriggerListener implements TriggerListener { + + @Override + public String getName() { + return "MyTriggerListener"; + } + + @Override + public void triggerFired(Trigger trigger, JobExecutionContext context) { + + } + + @Override + public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) { + String localIp = JeecgSystemApplication.getLocalHostExactAddress().getHostAddress(); + String ip = SpringUtils.getBean(IParameterConfigurationService.class).getValueByCode(QuantityConstant.RULE_JOB_IP); + if (StringUtils.isNotEmpty(ip)) { + if (!ip.equals(localIp)) { + return true; + } + } + return false; + } + + @Override + public void triggerMisfired(Trigger trigger) { + + } + + @Override + public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) { + + } + +} diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java index afa5faf..a342fdb 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/constant/QuantityConstant.java @@ -535,6 +535,7 @@ public class QuantityConstant { public static final String RULE_SHIPMENT_ZONE = "shipmentZoneRule"; public static final String SHIPMENT_WHOLE_TASK_CLEAR_INVENTORY = "shipmentWholeTaskClearInventory"; public static final String RULE_SHIPMENT_CONTAINER_ADVICE = "shipmentContainerAdvice"; + public static final String RULE_JOB_IP = "jobIp"; public static final int STATUS_CLOSE = 0; public static final int STATUS_OPEN = 1; diff --git a/huaheng-wms-core/src/main/resources/application.yml b/huaheng-wms-core/src/main/resources/application.yml index a2b83c2..e2f5d0c 100644 --- a/huaheng-wms-core/src/main/resources/application.yml +++ b/huaheng-wms-core/src/main/resources/application.yml @@ -2,11 +2,11 @@ spring: application: name: huaheng-wms-core profiles: - active: test + active: dev huaheng: system: version: '@project.version@' artifactId: '@project.artifactId@' - checkSystemActivationCode: true + checkSystemActivationCode: false -- libgit2 0.22.2