支持在 Bean 方法(必须保证入参和返回值符合 ReturnT<String> 方法名任意(String param) throws Exception)上使用 @JobHandler 注解,在方法上使用注解时如果没有指定 value,就会使用方法名作为 JobHandler 的名字,注解额外增加 init 和 destroy 可以指定当前 bean 中的其他方法作为 JobHandler 中的 init 和 destroy 方法。
parent
82bb3dfcc8
commit
039346b730
@ -0,0 +1,97 @@
|
||||
package com.xxl.job.core.handler.impl;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.handler.annotation.JobHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* @author liuzh 2019-12-07
|
||||
*/
|
||||
public class MethodJobHandler extends IJobHandler {
|
||||
private static Logger logger = LoggerFactory.getLogger(MethodJobHandler.class);
|
||||
|
||||
private final Object target;
|
||||
private final Method method;
|
||||
private final JobHandler jobHandler;
|
||||
private Method initMethod;
|
||||
private Method destroyMethod;
|
||||
|
||||
public MethodJobHandler(Object target, Method method, JobHandler jobHandler) {
|
||||
this.target = target;
|
||||
this.method = method;
|
||||
this.jobHandler = jobHandler;
|
||||
this.method.setAccessible(true);
|
||||
this.prepareMethod();
|
||||
}
|
||||
|
||||
protected void prepareMethod() {
|
||||
String init = jobHandler.init();
|
||||
if(!init.isEmpty()) {
|
||||
try {
|
||||
initMethod = target.getClass().getDeclaredMethod(init);
|
||||
initMethod.setAccessible(true);
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
String destroy = jobHandler.destroy();
|
||||
if(!destroy.isEmpty()) {
|
||||
try {
|
||||
destroyMethod = target.getClass().getDeclaredMethod(destroy);
|
||||
destroyMethod.setAccessible(true);
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnT<String> execute(String param) throws Exception {
|
||||
return (ReturnT<String>) method.invoke(target, new Object[]{param});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
super.init();
|
||||
if(initMethod != null) {
|
||||
try {
|
||||
initMethod.invoke(target);
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
super.destroy();
|
||||
if(destroyMethod != null) {
|
||||
try {
|
||||
destroyMethod.invoke(target);
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Object getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public JobHandler getJobHandler() {
|
||||
return jobHandler;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue