malei05 3 weeks ago
parent
commit
e4fec505c1

+ 0 - 87
src/main/java/net/yyc/quartz/util/JavaClassTaskInvok.java

@@ -1,87 +0,0 @@
-/*
- *    Copyright (c) 2018-2025, hnqz All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * Neither the name of the pig4cloud.com developer nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- * Author: hnqz
- */
-
-package net.yyc.quartz.util;
-
-import cn.hutool.core.util.StrUtil;
-import net.yyc.quartz.constants.QuartzEnum;
-import net.yyc.quartz.entity.SysJob;
-import net.yyc.quartz.exception.TaskException;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Component;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-/**
- * 定时任务java class反射实现
- *
- * @author 郑健楠
- */
-@Component("javaClassTaskInvok")
-@Slf4j
-public class JavaClassTaskInvok implements ITaskInvok {
-
-	@Override
-	public void invokMethod(SysJob sysJob) throws TaskException {
-		Object obj;
-		Class clazz;
-		Method method;
-		Object returnValue;
-		try {
-			if (StrUtil.isNotEmpty(sysJob.getMethodParamsValue())) {
-				clazz = Class.forName(sysJob.getClassName());
-				obj = clazz.newInstance();
-				method = clazz.getDeclaredMethod(sysJob.getMethodName(), String.class);
-				returnValue = method.invoke(obj, sysJob.getMethodParamsValue());
-			}
-			else {
-				clazz = Class.forName(sysJob.getClassName());
-				obj = clazz.newInstance();
-				method = clazz.getDeclaredMethod(sysJob.getMethodName());
-				returnValue = method.invoke(obj);
-			}
-			if (StrUtil.isEmpty(returnValue.toString())
-					|| QuartzEnum.JOB_LOG_STATUS_FAIL.getType().equals(returnValue.toString())) {
-				log.error("定时任务javaClassTaskInvok异常,执行任务:{}", sysJob.getClassName());
-				throw new TaskException("定时任务javaClassTaskInvok业务执行失败,任务:" + sysJob.getClassName());
-			}
-		}
-		catch (ClassNotFoundException e) {
-			log.error("定时任务java反射类没有找到,执行任务:{}", sysJob.getClassName());
-			throw new TaskException("定时任务java反射类没有找到,执行任务:" + sysJob.getClassName());
-		}
-		catch (IllegalAccessException e) {
-			log.error("定时任务java反射类异常,执行任务:{}", sysJob.getClassName());
-			throw new TaskException("定时任务java反射类异常,执行任务:" + sysJob.getClassName());
-		}
-		catch (InstantiationException e) {
-			log.error("定时任务java反射类异常,执行任务:{}", sysJob.getClassName());
-			throw new TaskException("定时任务java反射类异常,执行任务:" + sysJob.getClassName());
-		}
-		catch (NoSuchMethodException e) {
-			log.error("定时任务java反射执行方法名异常,执行任务:{}", sysJob.getClassName());
-			throw new TaskException("定时任务java反射执行方法名异常,执行任务:" + sysJob.getClassName());
-		}
-		catch (InvocationTargetException e) {
-			log.error("定时任务java反射执行异常,执行任务:{}", sysJob.getClassName());
-			throw new TaskException("定时任务java反射执行异常,执行任务:" + sysJob.getClassName());
-		}
-
-	}
-
-}

+ 0 - 134
src/main/java/net/yyc/quartz/util/RestTaskInvok.java

@@ -1,134 +0,0 @@
-/*
- *    Copyright (c) 2018-2025, hnqz All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * Neither the name of the pig4cloud.com developer nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- * Author: hnqz
- */
-
-package net.yyc.quartz.util;
-
-import cn.hutool.core.util.StrUtil;
-import cn.hutool.http.HttpRequest;
-import cn.hutool.http.HttpUtil;
-import lombok.extern.slf4j.Slf4j;
-import net.yyc.common.core.constant.CommonConstants;
-import net.yyc.common.core.constant.SecurityConstants;
-import net.yyc.quartz.entity.SysJob;
-import net.yyc.quartz.exception.TaskException;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.cloud.client.ServiceInstance;
-import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
-import org.springframework.stereotype.Component;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * 定时任务 REST 反射实现
- * 支持 service-name 模式(http://service-name/path)和 host:port 模式(http://host:port/path)
- *
- * @author 郑健楠
- */
-@Slf4j
-@Component
-public class RestTaskInvok implements ITaskInvok {
-
-	@Autowired
-	private LoadBalancerClient loadBalancer;
-
-	@Override
-	public void invokMethod(SysJob sysJob) throws TaskException {
-		try {
-			String executePath = sysJob.getExecutePath();
-			String resolvedUrl = resolveUrl(executePath);
-
-			HttpRequest request = HttpUtil.createPost(resolvedUrl);
-
-			// 租户 header
-			if (sysJob.getTenantId() != null) {
-				request.header(CommonConstants.TENANT_ID, sysJob.getTenantId().toString());
-			}
-
-			// 内部调用 header(绕过安全拦截器)
-			request.header(SecurityConstants.FROM, SecurityConstants.FROM_IN);
-
-			// 预留扩展 header(可由子类或配置覆盖)
-			addExtraHeaders(request, sysJob);
-
-			// 表单参数(params 对应 Controller @RequestParam)
-			String methodParams = sysJob.getMethodParamsValue();
-			if (StrUtil.isNotBlank(methodParams)) {
-				Map<String, Object> formData = new HashMap<>();
-				formData.put("params", methodParams);
-				request.form(formData);
-			}
-
-			request.execute();
-			log.debug("REST任务调用完成, url={}, params={}", resolvedUrl, methodParams);
-		}
-		catch (Exception e) {
-			log.error("定时任务restTaskInvok异常,执行任务:{}", sysJob.getExecutePath());
-			throw new TaskException("定时任务restTaskInvok业务执行失败,任务:" + sysJob.getExecutePath());
-		}
-	}
-
-	/**
-	 * 解析 URL,支持两种模式:
-	 * 1. service-name 模式:http://service-name/path → 通过 LoadBalancer 解析为实际地址
-	 * 2. host:port 模式:http://host:port/path → 直接使用
-	 *
-	 * @param executePath 原始执行路径
-	 * @return 解析后的实际 URL
-	 */
-	private String resolveUrl(String executePath) {
-		if (executePath == null || !executePath.startsWith("http://")) {
-			return executePath;
-		}
-
-		try {
-			// 提取服务名和路径,例如: http://hnqz-upms-biz/admin/schedule-task/xxx
-			String path = executePath.substring(7);
-			int slashIndex = path.indexOf('/');
-
-			if (slashIndex == -1) {
-				return executePath;
-			}
-
-			String serviceName = path.substring(0, slashIndex);
-			String servicePath = path.substring(slashIndex);
-
-			// 尝试通过 LoadBalancer 解析服务名
-			ServiceInstance instance = loadBalancer.choose(serviceName);
-			if (instance != null) {
-				return instance.getUri().toString() + servicePath;
-			}
-		} catch (Exception e) {
-			log.warn("服务名解析失败,使用原路径: {}", executePath, e);
-		}
-
-		// host:port 模式直接返回原路径
-		return executePath;
-	}
-
-	/**
-	 * 扩展点:子类可覆盖此方法添加额外的 header
-	 * 目前预留,未来可通过 SysJob 新增字段(如 invokeHeaders)实现动态配置
-	 *
-	 * @param request HTTP 请求
-	 * @param sysJob  任务实体
-	 */
-	protected void addExtraHeaders(HttpRequest request, SysJob sysJob) {
-		// 预留扩展点,可通过 sysJob.invokeHeaders 或配置文件动态添加 header
-	}
-
-}

+ 129 - 0
src/main/java/net/yyc/quartz/util/RestTaskInvoke.java

@@ -0,0 +1,129 @@
+/*
+ *    Copyright (c) 2018-2025, hnqz All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the pig4cloud.com developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: hnqz
+ */
+
+package net.yyc.quartz.util;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpUtil;
+import lombok.extern.slf4j.Slf4j;
+import net.yyc.common.core.constant.CommonConstants;
+import net.yyc.common.core.constant.SecurityConstants;
+import net.yyc.quartz.entity.SysJob;
+import net.yyc.quartz.exception.TaskException;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 定时任务 REST 反射实现
+ */
+@Slf4j
+@Component
+public class RestTaskInvoke implements ITaskInvok {
+    @Resource
+    private LoadBalancerClient loadBalancer;
+
+    @Override
+    public void invokMethod(SysJob sysJob) throws TaskException {
+        try {
+            String executePath = sysJob.getExecutePath();
+            String resolvedUrl = resolveUrl(executePath);
+
+            HttpRequest request = HttpUtil.createPost(resolvedUrl);
+
+            // 租户 header
+            if (sysJob.getTenantId() != null) {
+                request.header(CommonConstants.TENANT_ID, sysJob.getTenantId().toString());
+            }
+
+            // 内部调用 header(绕过安全拦截器)
+            request.header(SecurityConstants.FROM, SecurityConstants.FROM_IN);
+
+            // 预留扩展 header(可由子类或配置覆盖)
+            addExtraHeaders(request, sysJob);
+
+            // 表单参数(params 对应 Controller @RequestParam)
+            String methodParams = sysJob.getMethodParamsValue();
+            if (StrUtil.isNotBlank(methodParams)) {
+                Map<String, Object> formData = new HashMap<>();
+                formData.put("params", methodParams);
+                request.form(formData);
+            }
+
+            request.execute();
+            log.debug("REST任务调用完成, url={}, params={}", resolvedUrl, methodParams);
+        } catch (Exception e) {
+            log.error("定时任务restTaskInvok异常,执行任务:{}", sysJob.getExecutePath());
+            throw new TaskException("定时任务restTaskInvok业务执行失败,任务:" + sysJob.getExecutePath());
+        }
+    }
+
+    /**
+     * 解析 URL,支持两种模式:
+     * 1. service-name 模式:http://service-name/path → 通过 LoadBalancer 解析为实际地址
+     * 2. host:port 模式:http://host:port/path → 直接使用
+     *
+     * @param executePath 原始执行路径
+     * @return 解析后的实际 URL
+     */
+    private String resolveUrl(String executePath) {
+        if (executePath == null || !executePath.startsWith("http://")) {
+            return executePath;
+        }
+
+        try {
+            // 提取服务名和路径,例如: http://hnqz-upms-biz/admin/schedule-task/xxx
+            String path = executePath.substring(7);
+            int slashIndex = path.indexOf('/');
+
+            if (slashIndex == -1) {
+                return executePath;
+            }
+
+            String serviceName = path.substring(0, slashIndex);
+            String servicePath = path.substring(slashIndex);
+
+            // 尝试通过 LoadBalancer 解析服务名
+            ServiceInstance instance = loadBalancer.choose(serviceName);
+            if (instance != null) {
+                return instance.getUri().toString() + servicePath;
+            }
+        } catch (Exception e) {
+            log.warn("服务名解析失败,使用原路径: {}", executePath, e);
+        }
+
+        // host:port 模式直接返回原路径
+        return executePath;
+    }
+
+    /**
+     * 扩展点:子类可覆盖此方法添加额外的 header
+     * 目前预留,未来可通过 SysJob 新增字段(如 invokeHeaders)实现动态配置
+     *
+     * @param request HTTP 请求
+     * @param sysJob  任务实体
+     */
+    protected void addExtraHeaders(HttpRequest request, SysJob sysJob) {
+        // 预留扩展点,可通过 sysJob.invokeHeaders 或配置文件动态添加 header
+    }
+
+}

+ 2 - 2
src/main/java/net/yyc/quartz/util/SpringBeanTaskInvok.java → src/main/java/net/yyc/quartz/util/SpringBeanTaskInvoke.java

@@ -34,9 +34,9 @@ import java.lang.reflect.Method;
  *
  * @author 郑健楠
  */
-@Component("springBeanTaskInvok")
+@Component("springBeanTaskInvoke")
 @Slf4j
-public class SpringBeanTaskInvok implements ITaskInvok {
+public class SpringBeanTaskInvoke implements ITaskInvok {
 
 	@Override
 	public void invokMethod(SysJob sysJob) throws TaskException {

+ 5 - 13
src/main/java/net/yyc/quartz/util/TaskInvokFactory.java

@@ -27,19 +27,11 @@ public class TaskInvokFactory {
 		}
 
 		ITaskInvok iTaskInvok = null;
-		if (JobTypeQuartzEnum.JAVA.getType().equals(jobType)) {
-			iTaskInvok = SpringContextHolder.getBean("javaClassTaskInvok");
-		}
-		else if (JobTypeQuartzEnum.SPRING_BEAN.getType().equals(jobType)) {
-			iTaskInvok = SpringContextHolder.getBean("springBeanTaskInvok");
-		}
-		else if (JobTypeQuartzEnum.REST.getType().equals(jobType)) {
-			iTaskInvok = SpringContextHolder.getBean("restTaskInvok");
-		}
-		else if (JobTypeQuartzEnum.JAR.getType().equals(jobType)) {
-			iTaskInvok = SpringContextHolder.getBean("jarTaskInvok");
-		}
-		else if (StrUtil.isBlank(jobType)) {
+		if (JobTypeQuartzEnum.SPRING_BEAN.getType().equals(jobType)) {
+			iTaskInvok = SpringContextHolder.getBean("springBeanTaskInvoke");
+		} else if (JobTypeQuartzEnum.REST.getType().equals(jobType)) {
+			iTaskInvok = SpringContextHolder.getBean("restTaskInvoke");
+		} else if (StrUtil.isBlank(jobType)) {
 			log.info("定时任务类型无对应反射方式,反射类型:{}", jobType);
 			throw new TaskException("");
 		}