定时任务支持并发控制
parent
8e99823bbe
commit
d373632cb6
@ -0,0 +1,41 @@
|
|||||||
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误信息处理类。
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class ExceptionUtil
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取exception的详细错误信息。
|
||||||
|
*/
|
||||||
|
public static String getExceptionMessage(Throwable e)
|
||||||
|
{
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
e.printStackTrace(new PrintWriter(sw, true));
|
||||||
|
String str = sw.toString();
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRootErrorMseeage(Exception e)
|
||||||
|
{
|
||||||
|
Throwable root = ExceptionUtils.getRootCause(e);
|
||||||
|
root = (root == null ? e : root);
|
||||||
|
if (root == null)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
String msg = root.getMessage();
|
||||||
|
if (msg == null)
|
||||||
|
{
|
||||||
|
return "null";
|
||||||
|
}
|
||||||
|
return StringUtils.defaultString(msg);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package com.ruoyi.quartz.util;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
|
import com.ruoyi.quartz.domain.SysJob;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务执行工具
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class JobInvokeUtil
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 执行方法
|
||||||
|
*
|
||||||
|
* @param sysJob 系统任务
|
||||||
|
*/
|
||||||
|
public static void invokeMethod(SysJob sysJob) throws Exception
|
||||||
|
{
|
||||||
|
Object bean = SpringUtils.getBean(sysJob.getJobName());
|
||||||
|
String methodName = sysJob.getMethodName();
|
||||||
|
String methodParams = sysJob.getMethodParams();
|
||||||
|
|
||||||
|
invokeSpringBean(bean, methodName, methodParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用任务方法
|
||||||
|
*
|
||||||
|
* @param bean 目标对象
|
||||||
|
* @param methodName 方法名称
|
||||||
|
* @param methodParams 方法参数
|
||||||
|
* @throws InvocationTargetException
|
||||||
|
* @throws SecurityException
|
||||||
|
* @throws NoSuchMethodException
|
||||||
|
* @throws IllegalArgumentException
|
||||||
|
* @throws IllegalAccessException
|
||||||
|
*/
|
||||||
|
private static void invokeSpringBean(Object bean, String methodName, String methodParams)
|
||||||
|
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
|
||||||
|
InvocationTargetException
|
||||||
|
{
|
||||||
|
if (StringUtils.isNotEmpty(methodParams))
|
||||||
|
{
|
||||||
|
Method method = bean.getClass().getDeclaredMethod(methodName, String.class);
|
||||||
|
method.invoke(bean, methodParams);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Method method = bean.getClass().getDeclaredMethod(methodName);
|
||||||
|
method.invoke(bean);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,57 +0,0 @@
|
|||||||
package com.ruoyi.quartz.util;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
import com.ruoyi.common.exception.BusinessException;
|
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
|
||||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行定时任务
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ScheduleRunnable implements Runnable
|
|
||||||
{
|
|
||||||
private Object target;
|
|
||||||
private Method method;
|
|
||||||
private String params;
|
|
||||||
|
|
||||||
public ScheduleRunnable(String beanName, String methodName, String params)
|
|
||||||
throws NoSuchMethodException, SecurityException
|
|
||||||
{
|
|
||||||
this.target = SpringUtils.getBean(beanName);
|
|
||||||
this.params = params;
|
|
||||||
|
|
||||||
if (StringUtils.isNotEmpty(params))
|
|
||||||
{
|
|
||||||
this.method = target.getClass().getDeclaredMethod(methodName, String.class);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.method = target.getClass().getDeclaredMethod(methodName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ReflectionUtils.makeAccessible(method);
|
|
||||||
if (StringUtils.isNotEmpty(params))
|
|
||||||
{
|
|
||||||
method.invoke(target, params);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
method.invoke(target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
throw new BusinessException("执行定时任务失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue