mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2025-09-24 07:19:46 +08:00
update 日常字符串校验 统一重构到 StringUtils 便于维护扩展
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
package com.ruoyi.quartz.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.annotation.Log;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
@ -77,11 +77,11 @@ public class SysJobController extends BaseController
|
||||
{
|
||||
return error("新增任务'" + job.getJobName() + "'失败,Cron表达式不正确");
|
||||
}
|
||||
else if (StrUtil.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
|
||||
else if (StringUtils.containsIgnoreCase(job.getInvokeTarget(), Constants.LOOKUP_RMI))
|
||||
{
|
||||
return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi://'调用");
|
||||
}
|
||||
else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
|
||||
else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
|
||||
{
|
||||
return error("新增任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用");
|
||||
}
|
||||
@ -105,7 +105,7 @@ public class SysJobController extends BaseController
|
||||
{
|
||||
return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'rmi://'调用");
|
||||
}
|
||||
else if (StrUtil.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
|
||||
else if (StringUtils.containsAnyIgnoreCase(job.getInvokeTarget(), new String[] { Constants.HTTP, Constants.HTTPS }))
|
||||
{
|
||||
return error("修改任务'" + job.getJobName() + "'失败,目标字符串不允许'http(s)//'调用");
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.ruoyi.quartz.domain;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.ruoyi.common.annotation.Excel;
|
||||
@ -125,7 +125,7 @@ public class SysJob implements Serializable {
|
||||
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
public Date getNextValidTime() {
|
||||
if (StrUtil.isNotEmpty(cronExpression)) {
|
||||
if (StringUtils.isNotEmpty(cronExpression)) {
|
||||
return CronUtils.getNextExecution(cronExpression);
|
||||
}
|
||||
return null;
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.ruoyi.quartz.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
@ -27,10 +27,10 @@ public class SysJobLogServiceImpl extends ServicePlusImpl<SysJobLogMapper, SysJo
|
||||
public TableDataInfo<SysJobLog> selectPageJobLogList(SysJobLog jobLog) {
|
||||
Map<String, Object> params = jobLog.getParams();
|
||||
LambdaQueryWrapper<SysJobLog> lqw = new LambdaQueryWrapper<SysJobLog>()
|
||||
.like(StrUtil.isNotBlank(jobLog.getJobName()), SysJobLog::getJobName, jobLog.getJobName())
|
||||
.eq(StrUtil.isNotBlank(jobLog.getJobGroup()), SysJobLog::getJobGroup, jobLog.getJobGroup())
|
||||
.eq(StrUtil.isNotBlank(jobLog.getStatus()), SysJobLog::getStatus, jobLog.getStatus())
|
||||
.like(StrUtil.isNotBlank(jobLog.getInvokeTarget()), SysJobLog::getInvokeTarget, jobLog.getInvokeTarget())
|
||||
.like(StringUtils.isNotBlank(jobLog.getJobName()), SysJobLog::getJobName, jobLog.getJobName())
|
||||
.eq(StringUtils.isNotBlank(jobLog.getJobGroup()), SysJobLog::getJobGroup, jobLog.getJobGroup())
|
||||
.eq(StringUtils.isNotBlank(jobLog.getStatus()), SysJobLog::getStatus, jobLog.getStatus())
|
||||
.like(StringUtils.isNotBlank(jobLog.getInvokeTarget()), SysJobLog::getInvokeTarget, jobLog.getInvokeTarget())
|
||||
.apply(Validator.isNotEmpty(params.get("beginTime")),
|
||||
"date_format(create_time,'%y%m%d') >= date_format({0},'%y%m%d')",
|
||||
params.get("beginTime"))
|
||||
@ -50,10 +50,10 @@ public class SysJobLogServiceImpl extends ServicePlusImpl<SysJobLogMapper, SysJo
|
||||
public List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
|
||||
Map<String, Object> params = jobLog.getParams();
|
||||
return list(new LambdaQueryWrapper<SysJobLog>()
|
||||
.like(StrUtil.isNotBlank(jobLog.getJobName()), SysJobLog::getJobName, jobLog.getJobName())
|
||||
.eq(StrUtil.isNotBlank(jobLog.getJobGroup()), SysJobLog::getJobGroup, jobLog.getJobGroup())
|
||||
.eq(StrUtil.isNotBlank(jobLog.getStatus()), SysJobLog::getStatus, jobLog.getStatus())
|
||||
.like(StrUtil.isNotBlank(jobLog.getInvokeTarget()), SysJobLog::getInvokeTarget, jobLog.getInvokeTarget())
|
||||
.like(StringUtils.isNotBlank(jobLog.getJobName()), SysJobLog::getJobName, jobLog.getJobName())
|
||||
.eq(StringUtils.isNotBlank(jobLog.getJobGroup()), SysJobLog::getJobGroup, jobLog.getJobGroup())
|
||||
.eq(StringUtils.isNotBlank(jobLog.getStatus()), SysJobLog::getStatus, jobLog.getStatus())
|
||||
.like(StringUtils.isNotBlank(jobLog.getInvokeTarget()), SysJobLog::getInvokeTarget, jobLog.getInvokeTarget())
|
||||
.apply(Validator.isNotEmpty(params.get("beginTime")),
|
||||
"date_format(create_time,'%y%m%d') >= date_format({0},'%y%m%d')",
|
||||
params.get("beginTime"))
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.ruoyi.quartz.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ruoyi.common.constant.ScheduleConstants;
|
||||
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
|
||||
@ -48,10 +48,10 @@ public class SysJobServiceImpl extends ServicePlusImpl<SysJobMapper, SysJob, Sys
|
||||
@Override
|
||||
public TableDataInfo<SysJob> selectPageJobList(SysJob job) {
|
||||
LambdaQueryWrapper<SysJob> lqw = new LambdaQueryWrapper<SysJob>()
|
||||
.like(StrUtil.isNotBlank(job.getJobName()), SysJob::getJobName, job.getJobName())
|
||||
.eq(StrUtil.isNotBlank(job.getJobGroup()), SysJob::getJobGroup, job.getJobGroup())
|
||||
.eq(StrUtil.isNotBlank(job.getStatus()), SysJob::getStatus, job.getStatus())
|
||||
.like(StrUtil.isNotBlank(job.getInvokeTarget()), SysJob::getInvokeTarget, job.getInvokeTarget());
|
||||
.like(StringUtils.isNotBlank(job.getJobName()), SysJob::getJobName, job.getJobName())
|
||||
.eq(StringUtils.isNotBlank(job.getJobGroup()), SysJob::getJobGroup, job.getJobGroup())
|
||||
.eq(StringUtils.isNotBlank(job.getStatus()), SysJob::getStatus, job.getStatus())
|
||||
.like(StringUtils.isNotBlank(job.getInvokeTarget()), SysJob::getInvokeTarget, job.getInvokeTarget());
|
||||
return PageUtils.buildDataInfo(page(PageUtils.buildPage(), lqw));
|
||||
}
|
||||
|
||||
@ -64,10 +64,10 @@ public class SysJobServiceImpl extends ServicePlusImpl<SysJobMapper, SysJob, Sys
|
||||
@Override
|
||||
public List<SysJob> selectJobList(SysJob job) {
|
||||
return list(new LambdaQueryWrapper<SysJob>()
|
||||
.like(StrUtil.isNotBlank(job.getJobName()), SysJob::getJobName, job.getJobName())
|
||||
.eq(StrUtil.isNotBlank(job.getJobGroup()), SysJob::getJobGroup, job.getJobGroup())
|
||||
.eq(StrUtil.isNotBlank(job.getStatus()), SysJob::getStatus, job.getStatus())
|
||||
.like(StrUtil.isNotBlank(job.getInvokeTarget()), SysJob::getInvokeTarget, job.getInvokeTarget()));
|
||||
.like(StringUtils.isNotBlank(job.getJobName()), SysJob::getJobName, job.getJobName())
|
||||
.eq(StringUtils.isNotBlank(job.getJobGroup()), SysJob::getJobGroup, job.getJobGroup())
|
||||
.eq(StringUtils.isNotBlank(job.getStatus()), SysJob::getStatus, job.getStatus())
|
||||
.like(StringUtils.isNotBlank(job.getInvokeTarget()), SysJob::getInvokeTarget, job.getInvokeTarget()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,29 +1,29 @@
|
||||
package com.ruoyi.quartz.task;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 定时任务调度测试
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component("ryTask")
|
||||
public class RyTask
|
||||
{
|
||||
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
|
||||
{
|
||||
Console.log(StrUtil.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
|
||||
}
|
||||
|
||||
public void ryParams(String params)
|
||||
{
|
||||
Console.log("执行有参方法:" + params);
|
||||
}
|
||||
|
||||
public void ryNoParams()
|
||||
{
|
||||
Console.log("执行无参方法");
|
||||
}
|
||||
}
|
||||
package com.ruoyi.quartz.task;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 定时任务调度测试
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component("ryTask")
|
||||
public class RyTask
|
||||
{
|
||||
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
|
||||
{
|
||||
Console.log(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
|
||||
}
|
||||
|
||||
public void ryParams(String params)
|
||||
{
|
||||
Console.log("执行有参方法:" + params);
|
||||
}
|
||||
|
||||
public void ryNoParams()
|
||||
{
|
||||
Console.log("执行无参方法");
|
||||
}
|
||||
}
|
||||
|
@ -1,109 +1,109 @@
|
||||
package com.ruoyi.quartz.util;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.constant.ScheduleConstants;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.quartz.domain.SysJob;
|
||||
import com.ruoyi.quartz.domain.SysJobLog;
|
||||
import com.ruoyi.quartz.service.ISysJobLogService;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 抽象quartz调用
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public abstract class AbstractQuartzJob implements Job
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class);
|
||||
|
||||
/**
|
||||
* 线程本地变量
|
||||
*/
|
||||
private static ThreadLocal<Date> threadLocal = new ThreadLocal<>();
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException
|
||||
{
|
||||
SysJob sysJob = new SysJob();
|
||||
BeanUtil.copyProperties(context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES),sysJob);
|
||||
try
|
||||
{
|
||||
before(context, sysJob);
|
||||
if (Validator.isNotNull(sysJob))
|
||||
{
|
||||
doExecute(context, sysJob);
|
||||
}
|
||||
after(context, sysJob, null);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("任务执行异常 - :", e);
|
||||
after(context, sysJob, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行前
|
||||
*
|
||||
* @param context 工作执行上下文对象
|
||||
* @param sysJob 系统计划任务
|
||||
*/
|
||||
protected void before(JobExecutionContext context, SysJob sysJob)
|
||||
{
|
||||
threadLocal.set(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行后
|
||||
*
|
||||
* @param context 工作执行上下文对象
|
||||
* @param sysJob 系统计划任务
|
||||
*/
|
||||
protected void after(JobExecutionContext context, SysJob sysJob, Exception e)
|
||||
{
|
||||
Date startTime = threadLocal.get();
|
||||
threadLocal.remove();
|
||||
|
||||
final SysJobLog sysJobLog = new SysJobLog();
|
||||
sysJobLog.setJobName(sysJob.getJobName());
|
||||
sysJobLog.setJobGroup(sysJob.getJobGroup());
|
||||
sysJobLog.setInvokeTarget(sysJob.getInvokeTarget());
|
||||
sysJobLog.setStartTime(startTime);
|
||||
sysJobLog.setStopTime(new Date());
|
||||
long runMs = sysJobLog.getStopTime().getTime() - sysJobLog.getStartTime().getTime();
|
||||
sysJobLog.setJobMessage(sysJobLog.getJobName() + " 总共耗时:" + runMs + "毫秒");
|
||||
if (e != null)
|
||||
{
|
||||
sysJobLog.setStatus(Constants.FAIL);
|
||||
String errorMsg = StrUtil.sub(ExceptionUtil.stacktraceToString(e), 0, 2000);
|
||||
sysJobLog.setExceptionInfo(errorMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
sysJobLog.setStatus(Constants.SUCCESS);
|
||||
}
|
||||
|
||||
// 写入数据库当中
|
||||
SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行方法,由子类重载
|
||||
*
|
||||
* @param context 工作执行上下文对象
|
||||
* @param sysJob 系统计划任务
|
||||
* @throws Exception 执行过程中的异常
|
||||
*/
|
||||
protected abstract void doExecute(JobExecutionContext context, SysJob sysJob) throws Exception;
|
||||
}
|
||||
package com.ruoyi.quartz.util;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.constant.ScheduleConstants;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.quartz.domain.SysJob;
|
||||
import com.ruoyi.quartz.domain.SysJobLog;
|
||||
import com.ruoyi.quartz.service.ISysJobLogService;
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 抽象quartz调用
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public abstract class AbstractQuartzJob implements Job
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class);
|
||||
|
||||
/**
|
||||
* 线程本地变量
|
||||
*/
|
||||
private static ThreadLocal<Date> threadLocal = new ThreadLocal<>();
|
||||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException
|
||||
{
|
||||
SysJob sysJob = new SysJob();
|
||||
BeanUtil.copyProperties(context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES),sysJob);
|
||||
try
|
||||
{
|
||||
before(context, sysJob);
|
||||
if (Validator.isNotNull(sysJob))
|
||||
{
|
||||
doExecute(context, sysJob);
|
||||
}
|
||||
after(context, sysJob, null);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("任务执行异常 - :", e);
|
||||
after(context, sysJob, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行前
|
||||
*
|
||||
* @param context 工作执行上下文对象
|
||||
* @param sysJob 系统计划任务
|
||||
*/
|
||||
protected void before(JobExecutionContext context, SysJob sysJob)
|
||||
{
|
||||
threadLocal.set(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行后
|
||||
*
|
||||
* @param context 工作执行上下文对象
|
||||
* @param sysJob 系统计划任务
|
||||
*/
|
||||
protected void after(JobExecutionContext context, SysJob sysJob, Exception e)
|
||||
{
|
||||
Date startTime = threadLocal.get();
|
||||
threadLocal.remove();
|
||||
|
||||
final SysJobLog sysJobLog = new SysJobLog();
|
||||
sysJobLog.setJobName(sysJob.getJobName());
|
||||
sysJobLog.setJobGroup(sysJob.getJobGroup());
|
||||
sysJobLog.setInvokeTarget(sysJob.getInvokeTarget());
|
||||
sysJobLog.setStartTime(startTime);
|
||||
sysJobLog.setStopTime(new Date());
|
||||
long runMs = sysJobLog.getStopTime().getTime() - sysJobLog.getStartTime().getTime();
|
||||
sysJobLog.setJobMessage(sysJobLog.getJobName() + " 总共耗时:" + runMs + "毫秒");
|
||||
if (e != null)
|
||||
{
|
||||
sysJobLog.setStatus(Constants.FAIL);
|
||||
String errorMsg = StringUtils.sub(ExceptionUtil.stacktraceToString(e), 0, 2000);
|
||||
sysJobLog.setExceptionInfo(errorMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
sysJobLog.setStatus(Constants.SUCCESS);
|
||||
}
|
||||
|
||||
// 写入数据库当中
|
||||
SpringUtils.getBean(ISysJobLogService.class).addJobLog(sysJobLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行方法,由子类重载
|
||||
*
|
||||
* @param context 工作执行上下文对象
|
||||
* @param sysJob 系统计划任务
|
||||
* @throws Exception 执行过程中的异常
|
||||
*/
|
||||
protected abstract void doExecute(JobExecutionContext context, SysJob sysJob) throws Exception;
|
||||
}
|
||||
|
@ -1,184 +1,184 @@
|
||||
package com.ruoyi.quartz.util;
|
||||
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.quartz.domain.SysJob;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 任务执行工具
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class JobInvokeUtil
|
||||
{
|
||||
/**
|
||||
* 执行方法
|
||||
*
|
||||
* @param sysJob 系统任务
|
||||
*/
|
||||
public static void invokeMethod(SysJob sysJob) throws Exception
|
||||
{
|
||||
String invokeTarget = sysJob.getInvokeTarget();
|
||||
String beanName = getBeanName(invokeTarget);
|
||||
String methodName = getMethodName(invokeTarget);
|
||||
List<Object[]> methodParams = getMethodParams(invokeTarget);
|
||||
|
||||
if (!isValidClassName(beanName))
|
||||
{
|
||||
Object bean = SpringUtils.getBean(beanName);
|
||||
invokeMethod(bean, methodName, methodParams);
|
||||
}
|
||||
else
|
||||
{
|
||||
Object bean = Class.forName(beanName).newInstance();
|
||||
invokeMethod(bean, methodName, methodParams);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用任务方法
|
||||
*
|
||||
* @param bean 目标对象
|
||||
* @param methodName 方法名称
|
||||
* @param methodParams 方法参数
|
||||
*/
|
||||
private static void invokeMethod(Object bean, String methodName, List<Object[]> methodParams)
|
||||
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (Validator.isNotNull(methodParams) && methodParams.size() > 0)
|
||||
{
|
||||
Method method = bean.getClass().getDeclaredMethod(methodName, getMethodParamsType(methodParams));
|
||||
method.invoke(bean, getMethodParamsValue(methodParams));
|
||||
}
|
||||
else
|
||||
{
|
||||
Method method = bean.getClass().getDeclaredMethod(methodName);
|
||||
method.invoke(bean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验是否为为class包名
|
||||
*
|
||||
* @param str 名称
|
||||
* @return true是 false否
|
||||
*/
|
||||
public static boolean isValidClassName(String invokeTarget)
|
||||
{
|
||||
return StrUtil.count(invokeTarget, ".") > 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取bean名称
|
||||
*
|
||||
* @param invokeTarget 目标字符串
|
||||
* @return bean名称
|
||||
*/
|
||||
public static String getBeanName(String invokeTarget)
|
||||
{
|
||||
String beanName = StrUtil.subBefore(invokeTarget, "(",false);
|
||||
return StrUtil.subBefore(beanName, ".",true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取bean方法
|
||||
*
|
||||
* @param invokeTarget 目标字符串
|
||||
* @return method方法
|
||||
*/
|
||||
public static String getMethodName(String invokeTarget)
|
||||
{
|
||||
String methodName = StrUtil.subBefore(invokeTarget, "(",false);
|
||||
return StrUtil.subAfter(methodName, ".",true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取method方法参数相关列表
|
||||
*
|
||||
* @param invokeTarget 目标字符串
|
||||
* @return method方法相关参数列表
|
||||
*/
|
||||
public static List<Object[]> getMethodParams(String invokeTarget)
|
||||
{
|
||||
String methodStr = StrUtil.subBetween(invokeTarget, "(", ")");
|
||||
if (StrUtil.isEmpty(methodStr))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String[] methodParams = methodStr.split(",");
|
||||
List<Object[]> classs = new LinkedList<>();
|
||||
for (int i = 0; i < methodParams.length; i++)
|
||||
{
|
||||
String str = StrUtil.trimToEmpty(methodParams[i]);
|
||||
// String字符串类型,包含'
|
||||
if (StrUtil.contains(str, "'"))
|
||||
{
|
||||
classs.add(new Object[] { StrUtil.replace(str, "'", ""), String.class });
|
||||
}
|
||||
// boolean布尔类型,等于true或者false
|
||||
else if (StrUtil.equals(str, "true") || StrUtil.equalsIgnoreCase(str, "false"))
|
||||
{
|
||||
classs.add(new Object[] { Boolean.valueOf(str), Boolean.class });
|
||||
}
|
||||
// long长整形,包含L
|
||||
else if (StrUtil.containsIgnoreCase(str, "L"))
|
||||
{
|
||||
classs.add(new Object[] { Long.valueOf(StrUtil.replaceIgnoreCase(str, "L", "")), Long.class });
|
||||
}
|
||||
// double浮点类型,包含D
|
||||
else if (StrUtil.containsIgnoreCase(str, "D"))
|
||||
{
|
||||
classs.add(new Object[] { Double.valueOf(StrUtil.replaceIgnoreCase(str, "D", "")), Double.class });
|
||||
}
|
||||
// 其他类型归类为整形
|
||||
else
|
||||
{
|
||||
classs.add(new Object[] { Integer.valueOf(str), Integer.class });
|
||||
}
|
||||
}
|
||||
return classs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参数类型
|
||||
*
|
||||
* @param methodParams 参数相关列表
|
||||
* @return 参数类型列表
|
||||
*/
|
||||
public static Class<?>[] getMethodParamsType(List<Object[]> methodParams)
|
||||
{
|
||||
Class<?>[] classs = new Class<?>[methodParams.size()];
|
||||
int index = 0;
|
||||
for (Object[] os : methodParams)
|
||||
{
|
||||
classs[index] = (Class<?>) os[1];
|
||||
index++;
|
||||
}
|
||||
return classs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参数值
|
||||
*
|
||||
* @param methodParams 参数相关列表
|
||||
* @return 参数值列表
|
||||
*/
|
||||
public static Object[] getMethodParamsValue(List<Object[]> methodParams)
|
||||
{
|
||||
Object[] classs = new Object[methodParams.size()];
|
||||
int index = 0;
|
||||
for (Object[] os : methodParams)
|
||||
{
|
||||
classs[index] = (Object) os[0];
|
||||
index++;
|
||||
}
|
||||
return classs;
|
||||
}
|
||||
}
|
||||
package com.ruoyi.quartz.util;
|
||||
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.quartz.domain.SysJob;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 任务执行工具
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class JobInvokeUtil
|
||||
{
|
||||
/**
|
||||
* 执行方法
|
||||
*
|
||||
* @param sysJob 系统任务
|
||||
*/
|
||||
public static void invokeMethod(SysJob sysJob) throws Exception
|
||||
{
|
||||
String invokeTarget = sysJob.getInvokeTarget();
|
||||
String beanName = getBeanName(invokeTarget);
|
||||
String methodName = getMethodName(invokeTarget);
|
||||
List<Object[]> methodParams = getMethodParams(invokeTarget);
|
||||
|
||||
if (!isValidClassName(beanName))
|
||||
{
|
||||
Object bean = SpringUtils.getBean(beanName);
|
||||
invokeMethod(bean, methodName, methodParams);
|
||||
}
|
||||
else
|
||||
{
|
||||
Object bean = Class.forName(beanName).newInstance();
|
||||
invokeMethod(bean, methodName, methodParams);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用任务方法
|
||||
*
|
||||
* @param bean 目标对象
|
||||
* @param methodName 方法名称
|
||||
* @param methodParams 方法参数
|
||||
*/
|
||||
private static void invokeMethod(Object bean, String methodName, List<Object[]> methodParams)
|
||||
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
|
||||
InvocationTargetException
|
||||
{
|
||||
if (Validator.isNotNull(methodParams) && methodParams.size() > 0)
|
||||
{
|
||||
Method method = bean.getClass().getDeclaredMethod(methodName, getMethodParamsType(methodParams));
|
||||
method.invoke(bean, getMethodParamsValue(methodParams));
|
||||
}
|
||||
else
|
||||
{
|
||||
Method method = bean.getClass().getDeclaredMethod(methodName);
|
||||
method.invoke(bean);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验是否为为class包名
|
||||
*
|
||||
* @param str 名称
|
||||
* @return true是 false否
|
||||
*/
|
||||
public static boolean isValidClassName(String invokeTarget)
|
||||
{
|
||||
return StringUtils.count(invokeTarget, ".") > 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取bean名称
|
||||
*
|
||||
* @param invokeTarget 目标字符串
|
||||
* @return bean名称
|
||||
*/
|
||||
public static String getBeanName(String invokeTarget)
|
||||
{
|
||||
String beanName = StringUtils.subBefore(invokeTarget, "(",false);
|
||||
return StringUtils.subBefore(beanName, ".",true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取bean方法
|
||||
*
|
||||
* @param invokeTarget 目标字符串
|
||||
* @return method方法
|
||||
*/
|
||||
public static String getMethodName(String invokeTarget)
|
||||
{
|
||||
String methodName = StringUtils.subBefore(invokeTarget, "(",false);
|
||||
return StringUtils.subAfter(methodName, ".",true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取method方法参数相关列表
|
||||
*
|
||||
* @param invokeTarget 目标字符串
|
||||
* @return method方法相关参数列表
|
||||
*/
|
||||
public static List<Object[]> getMethodParams(String invokeTarget)
|
||||
{
|
||||
String methodStr = StringUtils.subBetween(invokeTarget, "(", ")");
|
||||
if (StringUtils.isEmpty(methodStr))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String[] methodParams = methodStr.split(",");
|
||||
List<Object[]> classs = new LinkedList<>();
|
||||
for (int i = 0; i < methodParams.length; i++)
|
||||
{
|
||||
String str = StringUtils.trimToEmpty(methodParams[i]);
|
||||
// String字符串类型,包含'
|
||||
if (StringUtils.contains(str, "'"))
|
||||
{
|
||||
classs.add(new Object[] { StringUtils.replace(str, "'", ""), String.class });
|
||||
}
|
||||
// boolean布尔类型,等于true或者false
|
||||
else if (StringUtils.equals(str, "true") || StringUtils.equalsIgnoreCase(str, "false"))
|
||||
{
|
||||
classs.add(new Object[] { Boolean.valueOf(str), Boolean.class });
|
||||
}
|
||||
// long长整形,包含L
|
||||
else if (StringUtils.containsIgnoreCase(str, "L"))
|
||||
{
|
||||
classs.add(new Object[] { Long.valueOf(StringUtils.replaceIgnoreCase(str, "L", "")), Long.class });
|
||||
}
|
||||
// double浮点类型,包含D
|
||||
else if (StringUtils.containsIgnoreCase(str, "D"))
|
||||
{
|
||||
classs.add(new Object[] { Double.valueOf(StringUtils.replaceIgnoreCase(str, "D", "")), Double.class });
|
||||
}
|
||||
// 其他类型归类为整形
|
||||
else
|
||||
{
|
||||
classs.add(new Object[] { Integer.valueOf(str), Integer.class });
|
||||
}
|
||||
}
|
||||
return classs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参数类型
|
||||
*
|
||||
* @param methodParams 参数相关列表
|
||||
* @return 参数类型列表
|
||||
*/
|
||||
public static Class<?>[] getMethodParamsType(List<Object[]> methodParams)
|
||||
{
|
||||
Class<?>[] classs = new Class<?>[methodParams.size()];
|
||||
int index = 0;
|
||||
for (Object[] os : methodParams)
|
||||
{
|
||||
classs[index] = (Class<?>) os[1];
|
||||
index++;
|
||||
}
|
||||
return classs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取参数值
|
||||
*
|
||||
* @param methodParams 参数相关列表
|
||||
* @return 参数值列表
|
||||
*/
|
||||
public static Object[] getMethodParamsValue(List<Object[]> methodParams)
|
||||
{
|
||||
Object[] classs = new Object[methodParams.size()];
|
||||
int index = 0;
|
||||
for (Object[] os : methodParams)
|
||||
{
|
||||
classs[index] = (Object) os[0];
|
||||
index++;
|
||||
}
|
||||
return classs;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user