ScheduleUtils定时任务工具类
public class ScheduleUtils{
/**
* 得到quartz任务类
*
* @param sysJob 执行计划
* @return 具体执行任务类
*/
private static Class<? extends org.quartz.Job>
}
抽象quartz调用
public abstract class AbstractQuartzJob emplements org.quartz.Job{
private static final Logger log = LoggerFactory.getLogger(AbstractQuartzJob.class);
/**
线程本地变量
*/
private static ThreadLocal<Date> threadLocal = new ThreadLocal<>();
//quartz-2.3.2.jar
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
Job sysJob = new Job();
BeanUtils.copyBeanProp(sysJob, context.getMergedJobDataMap().get(ScheduleConstants.TASK_PROPERTIES));
try{
before(context,sysJob);
if(sysJob != null){
doExecute(context,sysJob);
}
after(context,sysJob,null);
}catch(Exception e){
log.error("任务执行异常 - :",e);
e.printStackTrace();
after(context,sysJob,e);
}
}
/**
* 执行前
*
* @param context 工作执行上下文对象
* @param sysJob 系统计划任务
*/
protected void before(JobExecutionContext context,Job sysJob){
threadLocal.set(new Date());
}
/**
* 执行后
*
* @param context 工作执行上下文对象
* @param sysJob 系统计划任务
*/
protected void after(JobExecutionContext context,Job sysJob,Execution e){
Date startTime = threadLocal.get();
threadLocal.remove();
final JobLog jobLog = new JobLog();
jobLog.setJobName(sysJob.getJobName());
jobLog.setJobGroup(sysJob.getJobGroup());
jobLog.setInvokeTarget(sysJob.getInvokeTarget());
jobLog.setStartTime(startTime);
jobLog.setStopTime(new Date());
long runMs = jobLog.getStopTime().getTime() - jobLog.getStartTime().getTime();
jobLog.setJobMessage(jobLog.getJobName() + "总共耗时:" + runMs + "毫秒");
if(e != null){
String errorMsg = StringUtils.substring(ExceptionUtil.getExceptionMessage(e), 0, 2000);
jobLog.setExceptionInfo(errorMsg);
}else{
jobLog.setStatus(Constants.SUCCESS);
}
//写入数据库当中
SpringUtils.getBean(IJobLogService.class).save(jobLog);
}
/**
* 执行方法,由子类重载
*
* @param context 工作执行上下文对象
* @param sysJob 系统计划任务
* @throws Exception 执行过程中的异常
*/
protected abstract void doExecute(JobExecutionContext context, Job sysJob)throws Exception;
}
处理定时任务
@DisallowConcurrentExecution
public class QuartzDieallowConcurrentExecution extends AbstractQuartzJob{
@Override
protected void doExecute(JobExecutionContext context,Job sysJob){
JobInvokeUtil.invokeMethod(sysJob);
}
}
任务执行工具
public class JobInvokeUtil{
}
Cron表达式
public class CronUtils {
/**
* 返回一个布尔值代表一个给定的Cron表达式的有效性
*
* @param cronExpression Cron表达式
* @return boolean 表达式是否有效
* quartz-2.3.2.jar
*/
public static boolean isValid(String cronExpression) {
return CronExpression.isValidExpression(cronExpression);
}
/**
* 返回一个字符串值,表示该消息无效Cron表达式给出有效性
*
* @param cronExpression Cron表达式
* @return String 无效时返回表达式错误描述,如果有效返回null
*/
public static String getInvalidMessage(String cronExpression) {
try {
new CronExpression(cronExpression);
return null;
} catch (ParseException pe) {
return pe.getMessage();
}
}
/**
* 返回下一个执行时间根据给定的Cron表达式
*
* @param cronExpression Cron表达式
* @return Date 下次Cron表达式执行时间
*/
public static Date getNextExecution(String cronExpression) {
try {
CronExpression cron = new CronExpression(cronExpression);
return cron.getNextValidTimeAfter(new Date(System.currentTimeMillis()));
} catch (ParseException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
}