定时任务调度
复制代码
/**
-
执行通用任务
/
public void doTaskList() {
List taskList = taskMapper.selectTaskList();
if (CollectionUtils.isEmpty(taskList)) {
return;
}
for (Task task : taskList) {
try {
Integer retryTimes = task.getRetryTimes();
if (retryTimes == 1) {
Date updateTime = task.getGmtModified();
// 第一次重试,执行时间和上次时间间隔至少5分钟
if (updateTime.getTime() + 1000 * 60 * 5 > System.currentTimeMillis()) {
continue;
}
}
if (retryTimes == 2) {
Date updateTime = task.getGmtModified();
// 第二次重试,执行时间和上次时间间隔至少30分钟
if (updateTime.getTime() + 1000 * 60 * 30 > System.currentTimeMillis()) {
continue;
}
}
service.doTaskExec(task);
} catch (Exception e) {
// 执行失败发送提醒邮件
}
}
}
复制代码
反射执行
复制代码
/* -
通用任务执行
-
@param task 待执行的任务
*/
public void doTaskExec(Task task) throws ClassNotFoundException {
String execClass = task.getExecClass();
String execMethod = task.getExecMethod();
String execParam = task.getExecParam();Class<?> clazz = Class.forName(execClass);
Object object = ApplicationContextUtil.getBean(clazz);
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (!method.getName().equals(execMethod)) {
continue;
}
Class<?>[] paramTypes = method.getParameterTypes();
Object[] objectValue = new Object[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
objectValue[i] = JSON.parseObject(execParam, paramTypes[i]);
}
Object execResult;
try {
execResult = reflection(object, clazz, execMethod, paramTypes, objectValue);
} catch (Exception e) {
log.error(“外部接口返回异常:”, e);
processFailureExecResult(task, e.getMessage());
return;
}
processExecResult(task, JSON.toJSONString(execResult));
}
}
亚马逊测评 www.yisuping.com