cee572f2
RuoYi
若依 3.0
|
1
2
3
4
5
6
|
package com.ruoyi.quartz.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
|
c807de1f
YunaiV
初始化 quartz 模块~
|
7
|
|
cee572f2
RuoYi
若依 3.0
|
8
9
10
11
12
13
14
15
16
|
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.quartz.domain.SysJob;
/**
* 任务执行工具
*
* @author ruoyi
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
17
|
public class JobInvokeUtil {
|
cee572f2
RuoYi
若依 3.0
|
18
19
20
21
22
|
/**
* 执行方法
*
* @param sysJob 系统任务
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
23
|
public static void invokeMethod(SysJob sysJob) throws Exception {
|
cee572f2
RuoYi
若依 3.0
|
24
25
26
27
28
|
String invokeTarget = sysJob.getInvokeTarget();
String beanName = getBeanName(invokeTarget);
String methodName = getMethodName(invokeTarget);
List<Object[]> methodParams = getMethodParams(invokeTarget);
|
c807de1f
YunaiV
初始化 quartz 模块~
|
29
|
if (!isValidClassName(beanName)) {
|
cee572f2
RuoYi
若依 3.0
|
30
31
|
Object bean = SpringUtils.getBean(beanName);
invokeMethod(bean, methodName, methodParams);
|
c807de1f
YunaiV
初始化 quartz 模块~
|
32
|
} else {
|
cee572f2
RuoYi
若依 3.0
|
33
34
35
36
37
38
39
40
|
Object bean = Class.forName(beanName).newInstance();
invokeMethod(bean, methodName, methodParams);
}
}
/**
* 调用任务方法
*
|
c807de1f
YunaiV
初始化 quartz 模块~
|
41
42
|
* @param bean 目标对象
* @param methodName 方法名称
|
cee572f2
RuoYi
若依 3.0
|
43
44
45
46
|
* @param methodParams 方法参数
*/
private static void invokeMethod(Object bean, String methodName, List<Object[]> methodParams)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
|
c807de1f
YunaiV
初始化 quartz 模块~
|
47
48
|
InvocationTargetException {
if (StringUtils.isNotNull(methodParams) && methodParams.size() > 0) {
|
cee572f2
RuoYi
若依 3.0
|
49
50
|
Method method = bean.getClass().getDeclaredMethod(methodName, getMethodParamsType(methodParams));
method.invoke(bean, getMethodParamsValue(methodParams));
|
c807de1f
YunaiV
初始化 quartz 模块~
|
51
|
} else {
|
cee572f2
RuoYi
若依 3.0
|
52
53
54
55
56
57
58
|
Method method = bean.getClass().getDeclaredMethod(methodName);
method.invoke(bean);
}
}
/**
* 校验是否为为class包名
|
c807de1f
YunaiV
初始化 quartz 模块~
|
59
|
*
|
cee572f2
RuoYi
若依 3.0
|
60
61
62
|
* @param str 名称
* @return true是 false否
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
63
|
public static boolean isValidClassName(String invokeTarget) {
|
cee572f2
RuoYi
若依 3.0
|
64
65
66
67
68
|
return StringUtils.countMatches(invokeTarget, ".") > 1;
}
/**
* 获取bean名称
|
c807de1f
YunaiV
初始化 quartz 模块~
|
69
|
*
|
cee572f2
RuoYi
若依 3.0
|
70
71
72
|
* @param invokeTarget 目标字符串
* @return bean名称
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
73
|
public static String getBeanName(String invokeTarget) {
|
cee572f2
RuoYi
若依 3.0
|
74
75
76
77
78
79
|
String beanName = StringUtils.substringBefore(invokeTarget, "(");
return StringUtils.substringBeforeLast(beanName, ".");
}
/**
* 获取bean方法
|
c807de1f
YunaiV
初始化 quartz 模块~
|
80
|
*
|
cee572f2
RuoYi
若依 3.0
|
81
82
83
|
* @param invokeTarget 目标字符串
* @return method方法
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
84
|
public static String getMethodName(String invokeTarget) {
|
cee572f2
RuoYi
若依 3.0
|
85
86
87
88
89
90
|
String methodName = StringUtils.substringBefore(invokeTarget, "(");
return StringUtils.substringAfterLast(methodName, ".");
}
/**
* 获取method方法参数相关列表
|
c807de1f
YunaiV
初始化 quartz 模块~
|
91
|
*
|
cee572f2
RuoYi
若依 3.0
|
92
93
94
|
* @param invokeTarget 目标字符串
* @return method方法相关参数列表
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
95
|
public static List<Object[]> getMethodParams(String invokeTarget) {
|
cee572f2
RuoYi
若依 3.0
|
96
|
String methodStr = StringUtils.substringBetween(invokeTarget, "(", ")");
|
c807de1f
YunaiV
初始化 quartz 模块~
|
97
|
if (StringUtils.isEmpty(methodStr)) {
|
cee572f2
RuoYi
若依 3.0
|
98
99
100
101
|
return null;
}
String[] methodParams = methodStr.split(",");
List<Object[]> classs = new LinkedList<>();
|
c807de1f
YunaiV
初始化 quartz 模块~
|
102
|
for (int i = 0; i < methodParams.length; i++) {
|
cee572f2
RuoYi
若依 3.0
|
103
104
|
String str = StringUtils.trimToEmpty(methodParams[i]);
// String字符串类型,包含'
|
c807de1f
YunaiV
初始化 quartz 模块~
|
105
106
|
if (StringUtils.contains(str, "'")) {
classs.add(new Object[]{StringUtils.replace(str, "'", ""), String.class});
|
cee572f2
RuoYi
若依 3.0
|
107
108
|
}
// boolean布尔类型,等于true或者false
|
c807de1f
YunaiV
初始化 quartz 模块~
|
109
110
|
else if (StringUtils.equals(str, "true") || StringUtils.equalsIgnoreCase(str, "false")) {
classs.add(new Object[]{Boolean.valueOf(str), Boolean.class});
|
cee572f2
RuoYi
若依 3.0
|
111
112
|
}
// long长整形,包含L
|
c807de1f
YunaiV
初始化 quartz 模块~
|
113
114
|
else if (StringUtils.containsIgnoreCase(str, "L")) {
classs.add(new Object[]{Long.valueOf(StringUtils.replaceIgnoreCase(str, "L", "")), Long.class});
|
cee572f2
RuoYi
若依 3.0
|
115
116
|
}
// double浮点类型,包含D
|
c807de1f
YunaiV
初始化 quartz 模块~
|
117
118
|
else if (StringUtils.containsIgnoreCase(str, "D")) {
classs.add(new Object[]{Double.valueOf(StringUtils.replaceIgnoreCase(str, "D", "")), Double.class});
|
cee572f2
RuoYi
若依 3.0
|
119
120
|
}
// 其他类型归类为整形
|
c807de1f
YunaiV
初始化 quartz 模块~
|
121
122
|
else {
classs.add(new Object[]{Integer.valueOf(str), Integer.class});
|
cee572f2
RuoYi
若依 3.0
|
123
124
125
126
127
128
129
|
}
}
return classs;
}
/**
* 获取参数类型
|
c807de1f
YunaiV
初始化 quartz 模块~
|
130
|
*
|
cee572f2
RuoYi
若依 3.0
|
131
132
133
|
* @param methodParams 参数相关列表
* @return 参数类型列表
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
134
|
public static Class<?>[] getMethodParamsType(List<Object[]> methodParams) {
|
cee572f2
RuoYi
若依 3.0
|
135
136
|
Class<?>[] classs = new Class<?>[methodParams.size()];
int index = 0;
|
c807de1f
YunaiV
初始化 quartz 模块~
|
137
|
for (Object[] os : methodParams) {
|
cee572f2
RuoYi
若依 3.0
|
138
139
140
141
142
143
144
145
|
classs[index] = (Class<?>) os[1];
index++;
}
return classs;
}
/**
* 获取参数值
|
c807de1f
YunaiV
初始化 quartz 模块~
|
146
|
*
|
cee572f2
RuoYi
若依 3.0
|
147
148
149
|
* @param methodParams 参数相关列表
* @return 参数值列表
*/
|
c807de1f
YunaiV
初始化 quartz 模块~
|
150
|
public static Object[] getMethodParamsValue(List<Object[]> methodParams) {
|
cee572f2
RuoYi
若依 3.0
|
151
152
|
Object[] classs = new Object[methodParams.size()];
int index = 0;
|
c807de1f
YunaiV
初始化 quartz 模块~
|
153
|
for (Object[] os : methodParams) {
|
cee572f2
RuoYi
若依 3.0
|
154
155
156
157
158
159
|
classs[index] = (Object) os[0];
index++;
}
return classs;
}
}
|