|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+package com.xxl.job.client.glue;
|
|
|
2
|
+
|
|
|
3
|
+import java.lang.reflect.Field;
|
|
|
4
|
+import java.lang.reflect.Modifier;
|
|
|
5
|
+
|
|
|
6
|
+import javax.annotation.Resource;
|
|
|
7
|
+
|
|
|
8
|
+import org.slf4j.Logger;
|
|
|
9
|
+import org.slf4j.LoggerFactory;
|
|
|
10
|
+import org.springframework.beans.BeansException;
|
|
|
11
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
12
|
+import org.springframework.context.ApplicationContext;
|
|
|
13
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
14
|
+import org.springframework.core.annotation.AnnotationUtils;
|
|
|
15
|
+
|
|
|
16
|
+import com.xxl.job.client.glue.cache.LocalCache;
|
|
|
17
|
+import com.xxl.job.client.glue.loader.GlueLoader;
|
|
|
18
|
+import com.xxl.job.client.handler.IJobHandler;
|
|
|
19
|
+import com.xxl.job.client.handler.IJobHandler.JobHandleStatus;
|
|
|
20
|
+
|
|
|
21
|
+import groovy.lang.GroovyClassLoader;
|
|
|
22
|
+
|
|
|
23
|
+/**
|
|
|
24
|
+ * glue factory, product class/object by name
|
|
|
25
|
+ * @author xuxueli 2016-1-2 20:02:27
|
|
|
26
|
+ */
|
|
|
27
|
+public class GlueFactory implements ApplicationContextAware {
|
|
|
28
|
+ private static Logger logger = LoggerFactory.getLogger(GlueFactory.class);
|
|
|
29
|
+
|
|
|
30
|
+ /**
|
|
|
31
|
+ * groovy class loader
|
|
|
32
|
+ */
|
|
|
33
|
+ private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
|
|
|
34
|
+
|
|
|
35
|
+ /**
|
|
|
36
|
+ * glue cache timeout / second
|
|
|
37
|
+ */
|
|
|
38
|
+ private long cacheTimeout = 5000;
|
|
|
39
|
+ public void setCacheTimeout(long cacheTimeout) {
|
|
|
40
|
+ this.cacheTimeout = cacheTimeout;
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ /**
|
|
|
44
|
+ * code source loader
|
|
|
45
|
+ */
|
|
|
46
|
+ private GlueLoader glueLoader;
|
|
|
47
|
+ public void setGlueLoader(GlueLoader glueLoader) {
|
|
|
48
|
+ this.glueLoader = glueLoader;
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ // ----------------------------- spring support -----------------------------
|
|
|
52
|
+ private static ApplicationContext applicationContext;
|
|
|
53
|
+ private static GlueFactory glueFactory;
|
|
|
54
|
+
|
|
|
55
|
+ @Override
|
|
|
56
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
57
|
+ GlueFactory.applicationContext = applicationContext;
|
|
|
58
|
+ GlueFactory.glueFactory = (GlueFactory) applicationContext.getBean("glueFactory");
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ /**
|
|
|
62
|
+ * inject service of spring
|
|
|
63
|
+ * @param instance
|
|
|
64
|
+ */
|
|
|
65
|
+ public void injectService(Object instance){
|
|
|
66
|
+ if (instance==null) {
|
|
|
67
|
+ return;
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+ Field[] fields = instance.getClass().getDeclaredFields();
|
|
|
71
|
+ for (Field field : fields) {
|
|
|
72
|
+ if (Modifier.isStatic(field.getModifiers())) {
|
|
|
73
|
+ continue;
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ Object fieldBean = null;
|
|
|
77
|
+ // with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired
|
|
|
78
|
+ if (AnnotationUtils.getAnnotation(field, Resource.class) != null) {
|
|
|
79
|
+ try {
|
|
|
80
|
+ fieldBean = applicationContext.getBean(field.getName());
|
|
|
81
|
+ } catch (Exception e) {
|
|
|
82
|
+ }
|
|
|
83
|
+ if (fieldBean==null ) {
|
|
|
84
|
+ fieldBean = applicationContext.getBean(field.getType());
|
|
|
85
|
+ }
|
|
|
86
|
+ } else if (AnnotationUtils.getAnnotation(field, Autowired.class) != null) {
|
|
|
87
|
+ fieldBean = applicationContext.getBean(field.getType());
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ if (fieldBean!=null) {
|
|
|
91
|
+ field.setAccessible(true);
|
|
|
92
|
+ try {
|
|
|
93
|
+ field.set(instance, fieldBean);
|
|
|
94
|
+ } catch (IllegalArgumentException e) {
|
|
|
95
|
+ e.printStackTrace();
|
|
|
96
|
+ } catch (IllegalAccessException e) {
|
|
|
97
|
+ e.printStackTrace();
|
|
|
98
|
+ }
|
|
|
99
|
+ }
|
|
|
100
|
+ }
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ // ----------------------------- load instance -----------------------------
|
|
|
104
|
+
|
|
|
105
|
+ // load class,
|
|
|
106
|
+ public static String generateClassCacheKey(String job_group, String job_name){
|
|
|
107
|
+ return job_group.concat("_").concat(job_name).concat("_class");
|
|
|
108
|
+ }
|
|
|
109
|
+ public Class<?> loadClass(String job_group, String job_name) throws Exception{
|
|
|
110
|
+ if (job_group==null || job_group.trim().length()==0 || job_name==null || job_name.trim().length()==0) {
|
|
|
111
|
+ return null;
|
|
|
112
|
+ }
|
|
|
113
|
+ String cacheClassKey = generateClassCacheKey(job_group, job_name);
|
|
|
114
|
+ Object cacheClass = LocalCache.getInstance().get(cacheClassKey);
|
|
|
115
|
+ if (cacheClass != null) {
|
|
|
116
|
+ return (Class<?>) cacheClass;
|
|
|
117
|
+ }
|
|
|
118
|
+ String codeSource = glueLoader.load(job_group, job_name);
|
|
|
119
|
+ if (codeSource!=null && codeSource.trim().length()>0) {
|
|
|
120
|
+ Class<?> clazz = groovyClassLoader.parseClass(codeSource);
|
|
|
121
|
+ if (clazz!=null) {
|
|
|
122
|
+ LocalCache.getInstance().set(cacheClassKey, clazz, cacheTimeout);
|
|
|
123
|
+ logger.info(">>>>>>>>>>>> xxl-glue, fresh class, cacheClassKey:{}", cacheClassKey);
|
|
|
124
|
+ return clazz;
|
|
|
125
|
+ }
|
|
|
126
|
+ }
|
|
|
127
|
+ return null;
|
|
|
128
|
+ }
|
|
|
129
|
+
|
|
|
130
|
+ // load new instance, prototype
|
|
|
131
|
+ public IJobHandler loadNewInstance(String job_group, String job_name) throws Exception{
|
|
|
132
|
+ if (job_group==null || job_group.trim().length()==0 || job_name==null || job_name.trim().length()==0) {
|
|
|
133
|
+ return null;
|
|
|
134
|
+ }
|
|
|
135
|
+ Class<?> clazz = loadClass(job_group, job_name);
|
|
|
136
|
+ if (clazz!=null) {
|
|
|
137
|
+ Object instance = clazz.newInstance();
|
|
|
138
|
+ if (instance!=null) {
|
|
|
139
|
+ if (!(instance instanceof IJobHandler)) {
|
|
|
140
|
+ throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, "
|
|
|
141
|
+ + "cannot convert from instance["+ instance.getClass() +"] to IJobHandler");
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ this.injectService(instance);
|
|
|
145
|
+ return (IJobHandler) instance;
|
|
|
146
|
+ }
|
|
|
147
|
+ }
|
|
|
148
|
+ throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ // // load instance, singleton
|
|
|
152
|
+ public static String generateInstanceCacheKey(String job_group, String job_name){
|
|
|
153
|
+ return job_group.concat("_").concat(job_name).concat("_instance");
|
|
|
154
|
+ }
|
|
|
155
|
+ public IJobHandler loadInstance(String job_group, String job_name) throws Exception{
|
|
|
156
|
+ if (job_group==null || job_group.trim().length()==0 || job_name==null || job_name.trim().length()==0) {
|
|
|
157
|
+ return null;
|
|
|
158
|
+ }
|
|
|
159
|
+ String cacheInstanceKey = generateInstanceCacheKey(job_group, job_name);
|
|
|
160
|
+ Object cacheInstance = LocalCache.getInstance().get(cacheInstanceKey);
|
|
|
161
|
+ if (cacheInstance!=null) {
|
|
|
162
|
+ if (!(cacheInstance instanceof IJobHandler)) {
|
|
|
163
|
+ throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadInstance error, "
|
|
|
164
|
+ + "cannot convert from cacheClass["+ cacheInstance.getClass() +"] to IJobHandler");
|
|
|
165
|
+ }
|
|
|
166
|
+ return (IJobHandler) cacheInstance;
|
|
|
167
|
+ }
|
|
|
168
|
+ Object instance = loadNewInstance(job_group, job_name);
|
|
|
169
|
+ if (instance!=null) {
|
|
|
170
|
+ if (!(instance instanceof IJobHandler)) {
|
|
|
171
|
+ throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadInstance error, "
|
|
|
172
|
+ + "cannot convert from instance["+ instance.getClass() +"] to IJobHandler");
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ LocalCache.getInstance().set(cacheInstanceKey, instance, cacheTimeout);
|
|
|
176
|
+ logger.info(">>>>>>>>>>>> xxl-glue, fresh instance, cacheInstanceKey:{}", cacheInstanceKey);
|
|
|
177
|
+ return (IJobHandler) instance;
|
|
|
178
|
+ }
|
|
|
179
|
+ throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadInstance error, instance is null");
|
|
|
180
|
+ }
|
|
|
181
|
+
|
|
|
182
|
+ // ----------------------------- util -----------------------------
|
|
|
183
|
+ public static JobHandleStatus glue(String job_group, String job_name, String... params) throws Exception{
|
|
|
184
|
+ return GlueFactory.glueFactory.loadInstance(job_group, job_name).handle(params);
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+}
|