2018-10-07 14:16:47 +08:00
|
|
|
|
package com.ruoyi.quartz.util;
|
2018-07-09 08:44:52 +08:00
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
2018-10-07 14:16:47 +08:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-07-09 08:44:52 +08:00
|
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
2019-01-24 21:56:45 +08:00
|
|
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
2018-07-09 08:44:52 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 执行定时任务
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class ScheduleRunnable implements Runnable
|
|
|
|
|
{
|
2018-10-07 14:16:47 +08:00
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ScheduleRunnable.class);
|
|
|
|
|
|
2018-07-09 08:44:52 +08:00
|
|
|
|
private Object target;
|
|
|
|
|
private Method method;
|
|
|
|
|
private String params;
|
|
|
|
|
|
|
|
|
|
public ScheduleRunnable(String beanName, String methodName, String params)
|
|
|
|
|
throws NoSuchMethodException, SecurityException
|
|
|
|
|
{
|
2019-01-24 21:56:45 +08:00
|
|
|
|
this.target = SpringUtils.getBean(beanName);
|
2018-07-09 08:44:52 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
2018-10-07 14:16:47 +08:00
|
|
|
|
log.error("执行定时任务 - :", e);
|
2018-07-09 08:44:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|