malei05 3 nedēļas atpakaļ
vecāks
revīzija
68f1f1228d

+ 3 - 3
src/main/java/net/yyc/quartz/controller/SysJobController.java

@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import net.yyc.common.core.util.R;
+import net.yyc.common.security.annotation.Inner;
 import net.yyc.common.security.util.SecurityUtils;
 import net.yyc.quartz.entity.SysJob;
 import net.yyc.quartz.entity.SysJobLog;
@@ -171,11 +172,10 @@ public class SysJobController {
 
 	/**
 	 * 刷新全部定时任务
-	 * @return
 	 */
+	@Inner()
 	@PostMapping("/refresh-jobs")
-	@PreAuthorize("@pms.hasPermission('job_sys_job_refresh_job')")
-	public R refreshJobs() {
+	public R<?> refreshJobs() {
 		log.info("刷新全部定时任务");
 		sysJobService.list().forEach((sysjob) -> {
 			if (JOB_STATUS_RELEASE.getType().equals(sysjob.getJobStatus())

+ 0 - 99
src/main/java/net/yyc/quartz/util/HttpUtil.java

@@ -1,99 +0,0 @@
-package net.yyc.quartz.util;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.nio.charset.Charset;
-
-import org.apache.http.client.config.RequestConfig;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
-
-public class HttpUtil {
-	private static CloseableHttpClient httpClient;
-
-	static {
-		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
-		cm.setMaxTotal(100);
-		cm.setDefaultMaxPerRoute(20);
-		cm.setDefaultMaxPerRoute(50);
-		httpClient = HttpClients.custom().setConnectionManager(cm).build();
-	}
-
-	public static String get(String url) {
-		CloseableHttpResponse response = null;
-		BufferedReader in = null;
-		String result = "";
-		try {
-			HttpGet httpGet = new HttpGet(url);
-			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
-			httpGet.setConfig(requestConfig);
-			httpGet.setConfig(requestConfig);
-			httpGet.addHeader("Content-type", "application/json; charset=utf-8");
-			httpGet.setHeader("Accept", "application/json");
-			response = httpClient.execute(httpGet);
-			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
-			StringBuffer sb = new StringBuffer("");
-			String line = "";
-			String NL = System.getProperty("line.separator");
-			while ((line = in.readLine()) != null) {
-				sb.append(line + NL);
-			}
-			in.close();
-			result = sb.toString();
-		} catch (IOException e) {
-			e.printStackTrace();
-		} finally {
-			try {
-				if (null != response) {
-					response.close();
-				}
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-		}
-		return result;
-	}
-
-	public static String post(String url, String jsonString) {
-		CloseableHttpResponse response = null;
-		BufferedReader in = null;
-		String result = "";
-		try {
-			HttpPost httpPost = new HttpPost(url);
-			RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
-			httpPost.setConfig(requestConfig);
-			httpPost.setConfig(requestConfig);
-			httpPost.addHeader("Content-type", "application/json; charset=utf-8");
-			httpPost.setHeader("Accept", "application/json");
-			httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
-			response = httpClient.execute(httpPost);
-			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
-			StringBuffer sb = new StringBuffer("");
-			String line = "";
-			String NL = System.getProperty("line.separator");
-			while ((line = in.readLine()) != null) {
-				sb.append(line + NL);
-			}
-			in.close();
-			result = sb.toString();
-		} catch (IOException e) {
-			e.printStackTrace();
-		} finally {
-			try {
-				if (null != response) {
-					response.close();
-				}
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-		}
-		return result;
-	}
-
-}

+ 2 - 4
src/main/java/net/yyc/quartz/util/ITaskInvok.java → src/main/java/net/yyc/quartz/util/ITaskInvoke.java

@@ -25,13 +25,11 @@ import net.yyc.quartz.exception.TaskException;
  *
  * @author 郑健楠
  */
-public interface ITaskInvok {
+public interface ITaskInvoke {
 
 	/**
 	 * 执行反射方法
-	 * @param sysJob 配置类
-	 * @throws TaskException
 	 */
-	void invokMethod(SysJob sysJob) throws TaskException;
+	void invokeMethod(SysJob sysJob) throws TaskException;
 
 }

+ 0 - 62
src/main/java/net/yyc/quartz/util/JarTaskInvok.java

@@ -1,62 +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.entity.SysJob;
-import net.yyc.quartz.exception.TaskException;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Component;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * 定时任务可执行jar反射实现
- *
- * @author 郑健楠
- */
-@Slf4j
-@Component("jarTaskInvok")
-public class JarTaskInvok implements ITaskInvok {
-
-	@Override
-	public void invokMethod(SysJob sysJob) throws TaskException {
-		ProcessBuilder processBuilder = new ProcessBuilder();
-		File jar = new File(sysJob.getExecutePath());
-		processBuilder.directory(jar.getParentFile());
-		List<String> commands = new ArrayList<>();
-		commands.add("java");
-		commands.add("-jar");
-		commands.add(sysJob.getExecutePath());
-		if (StrUtil.isNotEmpty(sysJob.getMethodParamsValue())) {
-			commands.add(sysJob.getMethodParamsValue());
-		}
-		processBuilder.command(commands);
-		try {
-			processBuilder.start();
-		}
-		catch (IOException e) {
-			log.error("定时任务jar反射执行异常,执行任务:{}", sysJob.getExecutePath());
-			throw new TaskException("定时任务jar反射执行异常,执行任务:" + sysJob.getExecutePath());
-		}
-	}
-
-}

+ 0 - 47
src/main/java/net/yyc/quartz/util/R.java

@@ -1,47 +0,0 @@
-package net.yyc.quartz.util;
-
-import lombok.Data;
-
-import java.io.Serializable;
-
-/**
- * 通用响应结果
- */
-@Data
-public class R<T> implements Serializable {
-    private static final long serialVersionUID = 1L;
-
-    private int code;
-    private String msg;
-    private T data;
-
-    public R() {
-    }
-
-    public R(int code, String msg) {
-        this.code = code;
-        this.msg = msg;
-    }
-
-    public R(int code, String msg, T data) {
-        this.code = code;
-        this.msg = msg;
-        this.data = data;
-    }
-
-    public static <T> R<T> ok() {
-        return new R<>(0, "操作成功");
-    }
-
-    public static <T> R<T> ok(T data) {
-        return new R<>(0, "操作成功", data);
-    }
-
-    public static <T> R<T> failed() {
-        return new R<>(1, "操作失败");
-    }
-
-    public static <T> R<T> failed(String msg) {
-        return new R<>(1, msg);
-    }
-}

+ 0 - 122
src/main/java/net/yyc/quartz/util/RedisLock.java

@@ -1,122 +0,0 @@
-package net.yyc.quartz.util;
-
-import com.google.common.collect.Lists;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.data.redis.core.script.DefaultRedisScript;
-import org.springframework.util.Assert;
-import org.springframework.util.StringUtils;
-
-import java.time.Duration;
-import java.util.Objects;
-import java.util.Random;
-import java.util.UUID;
-
-/**
- * Redis 分布式锁(与 upms-biz {@code com.qunzhixinxi.hnqz.admin.util.RedisLock} 行为一致,供 quartz 使用同一 Redis 互斥)
- */
-@Slf4j
-public class RedisLock {
-
-	private final RedisTemplate redisTemplate;
-
-	private static final DefaultRedisScript<Long> UNLOCK_LUA;
-
-	static {
-		StringBuilder script = new StringBuilder();
-		script.append("if redis.call(\"get\",KEYS[1]) == ARGV[1] ");
-		script.append("then ");
-		script.append("    return redis.call(\"del\",KEYS[1]) ");
-		script.append("else ");
-		script.append("    return 0 ");
-		script.append("end ");
-		UNLOCK_LUA = new DefaultRedisScript<>();
-		UNLOCK_LUA.setResultType(Long.class);
-		UNLOCK_LUA.setScriptText(script.toString());
-	}
-
-	private String lockKey;
-	private String lockValue;
-	private int expireTime = 60;
-	private long timeOut = 100;
-	private volatile boolean locked = false;
-	private final Random random = new Random();
-
-	public RedisLock(RedisTemplate redisTemplate, String lockKey) {
-		this.redisTemplate = redisTemplate;
-		this.lockKey = lockKey + "_lock";
-	}
-
-	public RedisLock(RedisTemplate redisTemplate, String lockKey, int expireTime) {
-		this(redisTemplate, lockKey);
-		this.expireTime = expireTime;
-	}
-
-	public RedisLock(RedisTemplate redisTemplate, String lockKey, long timeOut) {
-		this(redisTemplate, lockKey);
-		this.timeOut = timeOut;
-	}
-
-	public RedisLock(RedisTemplate<? extends Object, ? extends Object> redisTemplate, String lockKey, int expireTime, long timeOut) {
-		this(redisTemplate, lockKey, expireTime);
-		this.timeOut = timeOut;
-	}
-
-	public boolean tryLock() {
-		lockValue = UUID.randomUUID().toString();
-		long timeout = timeOut * 1000000;
-		long nowTime = System.nanoTime();
-		while ((System.nanoTime() - nowTime) < timeout) {
-			if (this.set(lockKey, lockValue, expireTime)) {
-				locked = true;
-				return locked;
-			}
-			this.sleep(10, 50000);
-		}
-		return locked;
-	}
-
-	public boolean lock() {
-		lockValue = UUID.randomUUID().toString();
-		locked = this.set(lockKey, lockValue, expireTime);
-		return locked;
-	}
-
-	public boolean lockBlock() {
-		lockValue = UUID.randomUUID().toString();
-		while (true) {
-			if (this.set(lockKey, lockValue, expireTime)) {
-				locked = true;
-				return locked;
-			}
-			this.sleep(10, 50000);
-		}
-	}
-
-	public boolean unlock() {
-		if (locked) {
-			Long ret = (Long) redisTemplate.execute(UNLOCK_LUA, Lists.newArrayList(lockKey), lockValue);
-			boolean success = Objects.equals(1L, ret);
-			locked = !success;
-			return success;
-		}
-		return true;
-	}
-
-	private boolean set(final String key, final String value, final long seconds) {
-		Assert.isTrue(!StringUtils.isEmpty(key), "key不能为空");
-		boolean ret = Objects.equals(true, redisTemplate.opsForValue().setIfAbsent(key, value, Duration.ofSeconds(seconds)));
-		if (ret) {
-			log.info("获取锁{}的时间:{}", key, System.currentTimeMillis());
-		}
-		return ret;
-	}
-
-	private void sleep(long millis, int nanos) {
-		try {
-			Thread.sleep(millis, random.nextInt(nanos));
-		} catch (InterruptedException e) {
-			log.info("获取分布式锁休眠被中断:", e);
-		}
-	}
-}

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

@@ -39,12 +39,12 @@ import java.util.Map;
  */
 @Slf4j
 @Component
-public class RestTaskInvoke implements ITaskInvok {
+public class RestTaskInvoke implements ITaskInvoke {
     @Resource
     private LoadBalancerClient loadBalancer;
 
     @Override
-    public void invokMethod(SysJob sysJob) throws TaskException {
+    public void invokeMethod(SysJob sysJob) throws TaskException {
         try {
             String executePath = sysJob.getExecutePath();
             String resolvedUrl = resolveUrl(executePath);

+ 0 - 29
src/main/java/net/yyc/quartz/util/SecurityUtils.java

@@ -1,29 +0,0 @@
-package net.yyc.quartz.util;
-
-import lombok.Data;
-
-import java.io.Serializable;
-
-/**
- * 安全工具类(简化版)
- */
-public class SecurityUtils {
-
-    /**
-     * 获取当前登录用户信息
-     */
-    public static UserPrincipal getUser() {
-        // 简化实现,实际项目中可对接真实认证系统
-        UserPrincipal user = new UserPrincipal();
-        user.setUsername("system");
-        return user;
-    }
-
-    @Data
-    public static class UserPrincipal implements Serializable {
-        private static final long serialVersionUID = 1L;
-        private Long userId;
-        private String username;
-        private String roles;
-    }
-}

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

@@ -36,10 +36,10 @@ import java.lang.reflect.Method;
  */
 @Component("springBeanTaskInvoke")
 @Slf4j
-public class SpringBeanTaskInvoke implements ITaskInvok {
+public class SpringBeanTaskInvoke implements ITaskInvoke {
 
     @Override
-    public void invokMethod(SysJob sysJob) throws TaskException {
+    public void invokeMethod(SysJob sysJob) throws TaskException {
         Object target;
         Method method;
         Object returnValue;

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

@@ -20,23 +20,23 @@ public class TaskInvokFactory {
 	 * @return
 	 * @throws TaskException
 	 */
-	public static ITaskInvok getInvoker(String jobType) throws TaskException {
+	public static ITaskInvoke getInvoker(String jobType) throws TaskException {
 		if (StrUtil.isBlank(jobType)) {
 			log.info("获取TaskInvok传递参数有误,jobType:{}", jobType);
 			throw new TaskException("");
 		}
 
-		ITaskInvok iTaskInvok = null;
+		ITaskInvoke iTaskInvoke = null;
 		if (JobTypeQuartzEnum.SPRING_BEAN.getType().equals(jobType)) {
-			iTaskInvok = SpringContextHolder.getBean("springBeanTaskInvoke");
+			iTaskInvoke = SpringContextHolder.getBean("springBeanTaskInvoke");
 		} else if (JobTypeQuartzEnum.REST.getType().equals(jobType)) {
-			iTaskInvok = SpringContextHolder.getBean("restTaskInvoke");
+			iTaskInvoke = SpringContextHolder.getBean("restTaskInvoke");
 		} else if (StrUtil.isBlank(jobType)) {
 			log.info("定时任务类型无对应反射方式,反射类型:{}", jobType);
 			throw new TaskException("");
 		}
 
-		return iTaskInvok;
+		return iTaskInvoke;
 	}
 
 }

+ 2 - 2
src/main/java/net/yyc/quartz/util/TaskInvokeUtil.java

@@ -77,11 +77,11 @@ public class TaskInvokeUtil {
         sysJobLog.setTenantId(sysJob.getTenantId());
         try {
             // 执行任务
-            ITaskInvok iTaskInvok = TaskInvokFactory.getInvoker(sysJob.getJobType());
+            ITaskInvoke iTaskInvoke = TaskInvokFactory.getInvoker(sysJob.getJobType());
             // 设置uuid
             sysJob.setUuid(executionId);
             // 直接执行任务
-            iTaskInvok.invokMethod(sysJob);
+            iTaskInvoke.invokeMethod(sysJob);
             // 记录成功状态
             sysJobLog.setJobMessage(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getDescription());
             sysJobLog.setJobLogStatus(QuartzEnum.JOB_LOG_STATUS_SUCCESS.getType());