|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
package com.xxl.job.core.executor.impl;
|
|
|
|
|
|
|
|
|
|
import com.xxl.job.core.biz.model.ReturnT;
|
|
|
|
|
import com.xxl.job.core.executor.XxlJobExecutor;
|
|
|
|
|
import com.xxl.job.core.glue.GlueFactory;
|
|
|
|
|
import com.xxl.job.core.handler.IJobHandler;
|
|
|
|
|
import com.xxl.job.core.handler.annotation.JobHandler;
|
|
|
|
|
import com.xxl.job.core.handler.annotation.XxlJob;
|
|
|
|
|
import com.xxl.job.core.handler.impl.MethodJobHandler;
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
@ -29,11 +31,13 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
|
|
|
|
|
|
|
|
|
// init JobHandler Repository
|
|
|
|
|
initJobHandlerRepository(applicationContext);
|
|
|
|
|
|
|
|
|
|
// init JobHandler Repository (for method)
|
|
|
|
|
initJobHandlerMethodRepository(applicationContext);
|
|
|
|
|
|
|
|
|
|
// refresh GlueFactory
|
|
|
|
|
GlueFactory.refreshInstance(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// super start
|
|
|
|
|
super.start();
|
|
|
|
|
}
|
|
|
|
@ -71,21 +75,59 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
|
|
|
|
if (applicationContext == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// init job handler from method
|
|
|
|
|
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
|
|
|
|
|
for (String beanDefinitionName : beanDefinitionNames) {
|
|
|
|
|
Object bean = applicationContext.getBean(beanDefinitionName);
|
|
|
|
|
Method[] methods = bean.getClass().getDeclaredMethods();
|
|
|
|
|
for (int i = 0; i < methods.length; i++) {
|
|
|
|
|
JobHandler jobHandler = AnnotationUtils.findAnnotation(methods[i], JobHandler.class);
|
|
|
|
|
if (jobHandler != null) {
|
|
|
|
|
String name = jobHandler.value();
|
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
|
name = methods[i].getName();
|
|
|
|
|
for (Method method: methods) {
|
|
|
|
|
XxlJob xxlJob = AnnotationUtils.findAnnotation(method, XxlJob.class);
|
|
|
|
|
if (xxlJob != null) {
|
|
|
|
|
|
|
|
|
|
// name
|
|
|
|
|
String name = xxlJob.value();
|
|
|
|
|
if (name.trim().length() == 0) {
|
|
|
|
|
throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
|
|
|
|
|
}
|
|
|
|
|
if (loadJobHandler(name) != null) {
|
|
|
|
|
throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
|
|
|
|
|
}
|
|
|
|
|
registJobHandler(name, new MethodJobHandler(bean, methods[i], jobHandler));
|
|
|
|
|
|
|
|
|
|
// execute method
|
|
|
|
|
if (!(method.getParameterTypes()!=null && method.getParameterTypes().length==1 && method.getParameterTypes()[0].isAssignableFrom(String.class))) {
|
|
|
|
|
throw new RuntimeException("xxl-job method-jobhandler param-classtype invalid, for[" + bean.getClass() + "#"+ method.getName() +"] , " +
|
|
|
|
|
"The correct method format like \" public ReturnT<String> execute(String param) \" .");
|
|
|
|
|
}
|
|
|
|
|
if (!method.getReturnType().isAssignableFrom(ReturnT.class)) {
|
|
|
|
|
throw new RuntimeException("xxl-job method-jobhandler return-classtype invalid, for[" + bean.getClass() + "#"+ method.getName() +"] , " +
|
|
|
|
|
"The correct method format like \" public ReturnT<String> execute(String param) \" .");
|
|
|
|
|
}
|
|
|
|
|
method.setAccessible(true);
|
|
|
|
|
|
|
|
|
|
// init and destory
|
|
|
|
|
Method initMethod = null;
|
|
|
|
|
Method destroyMethod = null;
|
|
|
|
|
|
|
|
|
|
if(xxlJob.init().trim().length() > 0) {
|
|
|
|
|
try {
|
|
|
|
|
initMethod = bean.getClass().getDeclaredMethod(xxlJob.init());
|
|
|
|
|
initMethod.setAccessible(true);
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(xxlJob.destroy().trim().length() > 0) {
|
|
|
|
|
try {
|
|
|
|
|
destroyMethod = bean.getClass().getDeclaredMethod(xxlJob.destroy());
|
|
|
|
|
destroyMethod.setAccessible(true);
|
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
|
throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + bean.getClass() + "#"+ method.getName() +"] .");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// registry jobhandler
|
|
|
|
|
registJobHandler(name, new MethodJobHandler(bean, method, initMethod, destroyMethod));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|