|
@@ -1,9 +1,11 @@
|
1
|
1
|
package com.xxl.job.core.executor.impl;
|
2
|
2
|
|
|
3
|
+import com.xxl.job.core.biz.model.ReturnT;
|
3
|
4
|
import com.xxl.job.core.executor.XxlJobExecutor;
|
4
|
5
|
import com.xxl.job.core.glue.GlueFactory;
|
5
|
6
|
import com.xxl.job.core.handler.IJobHandler;
|
6
|
7
|
import com.xxl.job.core.handler.annotation.JobHandler;
|
|
8
|
+import com.xxl.job.core.handler.annotation.XxlJob;
|
7
|
9
|
import com.xxl.job.core.handler.impl.MethodJobHandler;
|
8
|
10
|
import org.springframework.beans.BeansException;
|
9
|
11
|
import org.springframework.beans.factory.DisposableBean;
|
|
@@ -29,11 +31,13 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
29
|
31
|
|
30
|
32
|
// init JobHandler Repository
|
31
|
33
|
initJobHandlerRepository(applicationContext);
|
|
34
|
+
|
|
35
|
+ // init JobHandler Repository (for method)
|
32
|
36
|
initJobHandlerMethodRepository(applicationContext);
|
|
37
|
+
|
33
|
38
|
// refresh GlueFactory
|
34
|
39
|
GlueFactory.refreshInstance(1);
|
35
|
40
|
|
36
|
|
-
|
37
|
41
|
// super start
|
38
|
42
|
super.start();
|
39
|
43
|
}
|
|
@@ -71,21 +75,59 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
71
|
75
|
if (applicationContext == null) {
|
72
|
76
|
return;
|
73
|
77
|
}
|
|
78
|
+
|
|
79
|
+ // init job handler from method
|
74
|
80
|
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
|
75
|
81
|
for (String beanDefinitionName : beanDefinitionNames) {
|
76
|
82
|
Object bean = applicationContext.getBean(beanDefinitionName);
|
77
|
83
|
Method[] methods = bean.getClass().getDeclaredMethods();
|
78
|
|
- for (int i = 0; i < methods.length; i++) {
|
79
|
|
- JobHandler jobHandler = AnnotationUtils.findAnnotation(methods[i], JobHandler.class);
|
80
|
|
- if (jobHandler != null) {
|
81
|
|
- String name = jobHandler.value();
|
82
|
|
- if (name.isEmpty()) {
|
83
|
|
- name = methods[i].getName();
|
|
84
|
+ for (Method method: methods) {
|
|
85
|
+ XxlJob xxlJob = AnnotationUtils.findAnnotation(method, XxlJob.class);
|
|
86
|
+ if (xxlJob != null) {
|
|
87
|
+
|
|
88
|
+ // name
|
|
89
|
+ String name = xxlJob.value();
|
|
90
|
+ if (name.trim().length() == 0) {
|
|
91
|
+ throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
|
84
|
92
|
}
|
85
|
93
|
if (loadJobHandler(name) != null) {
|
86
|
94
|
throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
|
87
|
95
|
}
|
88
|
|
- registJobHandler(name, new MethodJobHandler(bean, methods[i], jobHandler));
|
|
96
|
+
|
|
97
|
+ // execute method
|
|
98
|
+ if (!(method.getParameterTypes()!=null && method.getParameterTypes().length==1 && method.getParameterTypes()[0].isAssignableFrom(String.class))) {
|
|
99
|
+ throw new RuntimeException("xxl-job method-jobhandler param-classtype invalid, for[" + bean.getClass() + "#"+ method.getName() +"] , " +
|
|
100
|
+ "The correct method format like \" public ReturnT<String> execute(String param) \" .");
|
|
101
|
+ }
|
|
102
|
+ if (!method.getReturnType().isAssignableFrom(ReturnT.class)) {
|
|
103
|
+ throw new RuntimeException("xxl-job method-jobhandler return-classtype invalid, for[" + bean.getClass() + "#"+ method.getName() +"] , " +
|
|
104
|
+ "The correct method format like \" public ReturnT<String> execute(String param) \" .");
|
|
105
|
+ }
|
|
106
|
+ method.setAccessible(true);
|
|
107
|
+
|
|
108
|
+ // init and destory
|
|
109
|
+ Method initMethod = null;
|
|
110
|
+ Method destroyMethod = null;
|
|
111
|
+
|
|
112
|
+ if(xxlJob.init().trim().length() > 0) {
|
|
113
|
+ try {
|
|
114
|
+ initMethod = bean.getClass().getDeclaredMethod(xxlJob.init());
|
|
115
|
+ initMethod.setAccessible(true);
|
|
116
|
+ } catch (NoSuchMethodException e) {
|
|
117
|
+ throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
|
|
118
|
+ }
|
|
119
|
+ }
|
|
120
|
+ if(xxlJob.destroy().trim().length() > 0) {
|
|
121
|
+ try {
|
|
122
|
+ destroyMethod = bean.getClass().getDeclaredMethod(xxlJob.destroy());
|
|
123
|
+ destroyMethod.setAccessible(true);
|
|
124
|
+ } catch (NoSuchMethodException e) {
|
|
125
|
+ throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ // registry jobhandler
|
|
130
|
+ registJobHandler(name, new MethodJobHandler(bean, method, initMethod, destroyMethod));
|
89
|
131
|
}
|
90
|
132
|
}
|
91
|
133
|
}
|