|
|
@ -1,9 +1,11 @@
|
|
|
|
package com.xxl.job.core.executor.impl;
|
|
|
|
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.executor.XxlJobExecutor;
|
|
|
|
import com.xxl.job.core.glue.GlueFactory;
|
|
|
|
import com.xxl.job.core.glue.GlueFactory;
|
|
|
|
import com.xxl.job.core.handler.IJobHandler;
|
|
|
|
import com.xxl.job.core.handler.IJobHandler;
|
|
|
|
import com.xxl.job.core.handler.annotation.JobHandler;
|
|
|
|
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 com.xxl.job.core.handler.impl.MethodJobHandler;
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
@ -29,11 +31,13 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
|
|
|
|
|
|
|
|
|
|
|
// init JobHandler Repository
|
|
|
|
// init JobHandler Repository
|
|
|
|
initJobHandlerRepository(applicationContext);
|
|
|
|
initJobHandlerRepository(applicationContext);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// init JobHandler Repository (for method)
|
|
|
|
initJobHandlerMethodRepository(applicationContext);
|
|
|
|
initJobHandlerMethodRepository(applicationContext);
|
|
|
|
|
|
|
|
|
|
|
|
// refresh GlueFactory
|
|
|
|
// refresh GlueFactory
|
|
|
|
GlueFactory.refreshInstance(1);
|
|
|
|
GlueFactory.refreshInstance(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// super start
|
|
|
|
// super start
|
|
|
|
super.start();
|
|
|
|
super.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -71,21 +75,59 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
|
|
|
|
if (applicationContext == null) {
|
|
|
|
if (applicationContext == null) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// init job handler from method
|
|
|
|
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
|
|
|
|
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
|
|
|
|
for (String beanDefinitionName : beanDefinitionNames) {
|
|
|
|
for (String beanDefinitionName : beanDefinitionNames) {
|
|
|
|
Object bean = applicationContext.getBean(beanDefinitionName);
|
|
|
|
Object bean = applicationContext.getBean(beanDefinitionName);
|
|
|
|
Method[] methods = bean.getClass().getDeclaredMethods();
|
|
|
|
Method[] methods = bean.getClass().getDeclaredMethods();
|
|
|
|
for (int i = 0; i < methods.length; i++) {
|
|
|
|
for (Method method: methods) {
|
|
|
|
JobHandler jobHandler = AnnotationUtils.findAnnotation(methods[i], JobHandler.class);
|
|
|
|
XxlJob xxlJob = AnnotationUtils.findAnnotation(method, XxlJob.class);
|
|
|
|
if (jobHandler != null) {
|
|
|
|
if (xxlJob != null) {
|
|
|
|
String name = jobHandler.value();
|
|
|
|
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
// name
|
|
|
|
name = methods[i].getName();
|
|
|
|
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) {
|
|
|
|
if (loadJobHandler(name) != null) {
|
|
|
|
throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|