|
@@ -4,12 +4,15 @@ import com.xxl.job.core.executor.XxlJobExecutor;
|
4
|
4
|
import com.xxl.job.core.glue.GlueFactory;
|
5
|
5
|
import com.xxl.job.core.handler.IJobHandler;
|
6
|
6
|
import com.xxl.job.core.handler.annotation.JobHandler;
|
|
7
|
+import com.xxl.job.core.handler.impl.MethodJobHandler;
|
7
|
8
|
import org.springframework.beans.BeansException;
|
8
|
9
|
import org.springframework.beans.factory.DisposableBean;
|
9
|
10
|
import org.springframework.beans.factory.InitializingBean;
|
10
|
11
|
import org.springframework.context.ApplicationContext;
|
11
|
12
|
import org.springframework.context.ApplicationContextAware;
|
|
13
|
+import org.springframework.core.annotation.AnnotationUtils;
|
12
|
14
|
|
|
15
|
+import java.lang.reflect.Method;
|
13
|
16
|
import java.util.Map;
|
14
|
17
|
|
15
|
18
|
/**
|
|
@@ -26,7 +29,7 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
26
|
29
|
|
27
|
30
|
// init JobHandler Repository
|
28
|
31
|
initJobHandlerRepository(applicationContext);
|
29
|
|
-
|
|
32
|
+ initJobHandlerMethodRepository(applicationContext);
|
30
|
33
|
// refresh GlueFactory
|
31
|
34
|
GlueFactory.refreshInstance(1);
|
32
|
35
|
|
|
@@ -42,7 +45,7 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
42
|
45
|
}
|
43
|
46
|
|
44
|
47
|
|
45
|
|
- private void initJobHandlerRepository(ApplicationContext applicationContext){
|
|
48
|
+ private void initJobHandlerRepository(ApplicationContext applicationContext) {
|
46
|
49
|
if (applicationContext == null) {
|
47
|
50
|
return;
|
48
|
51
|
}
|
|
@@ -50,13 +53,13 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
50
|
53
|
// init job handler action
|
51
|
54
|
Map<String, Object> serviceBeanMap = applicationContext.getBeansWithAnnotation(JobHandler.class);
|
52
|
55
|
|
53
|
|
- if (serviceBeanMap!=null && serviceBeanMap.size()>0) {
|
|
56
|
+ if (serviceBeanMap != null && serviceBeanMap.size() > 0) {
|
54
|
57
|
for (Object serviceBean : serviceBeanMap.values()) {
|
55
|
|
- if (serviceBean instanceof IJobHandler){
|
|
58
|
+ if (serviceBean instanceof IJobHandler) {
|
56
|
59
|
String name = serviceBean.getClass().getAnnotation(JobHandler.class).value();
|
57
|
60
|
IJobHandler handler = (IJobHandler) serviceBean;
|
58
|
61
|
if (loadJobHandler(name) != null) {
|
59
|
|
- throw new RuntimeException("xxl-job jobhandler["+ name +"] naming conflicts.");
|
|
62
|
+ throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
|
60
|
63
|
}
|
61
|
64
|
registJobHandler(name, handler);
|
62
|
65
|
}
|
|
@@ -64,12 +67,38 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
64
|
67
|
}
|
65
|
68
|
}
|
66
|
69
|
|
|
70
|
+ private void initJobHandlerMethodRepository(ApplicationContext applicationContext) {
|
|
71
|
+ if (applicationContext == null) {
|
|
72
|
+ return;
|
|
73
|
+ }
|
|
74
|
+ String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
|
|
75
|
+ for (String beanDefinitionName : beanDefinitionNames) {
|
|
76
|
+ Object bean = applicationContext.getBean(beanDefinitionName);
|
|
77
|
+ 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
|
+ }
|
|
85
|
+ if (loadJobHandler(name) != null) {
|
|
86
|
+ throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
|
|
87
|
+ }
|
|
88
|
+ registJobHandler(name, new MethodJobHandler(bean, methods[i], jobHandler));
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+
|
67
|
94
|
// ---------------------- applicationContext ----------------------
|
68
|
95
|
private static ApplicationContext applicationContext;
|
|
96
|
+
|
69
|
97
|
@Override
|
70
|
98
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
71
|
99
|
this.applicationContext = applicationContext;
|
72
|
100
|
}
|
|
101
|
+
|
73
|
102
|
public static ApplicationContext getApplicationContext() {
|
74
|
103
|
return applicationContext;
|
75
|
104
|
}
|