|
|
@ -15,20 +15,19 @@ import org.springframework.scheduling.quartz.QuartzJobBean; |
|
|
|
|
|
|
|
|
import java.lang.reflect.Method; |
|
|
import java.lang.reflect.Method; |
|
|
import java.util.Date; |
|
|
import java.util.Date; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 定时任务 |
|
|
* 定时任务 |
|
|
* |
|
|
|
|
|
* |
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public class ScheduleJob extends QuartzJobBean { |
|
|
public class ScheduleJob extends QuartzJobBean { |
|
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void executeInternal(JobExecutionContext context) throws JobExecutionException { |
|
|
protected void executeInternal(JobExecutionContext context) throws JobExecutionException { |
|
|
ScheduleJobEntity scheduleJob = (ScheduleJobEntity) context.getMergedJobDataMap() |
|
|
ScheduleJobEntity scheduleJob = (ScheduleJobEntity) context.getMergedJobDataMap() |
|
|
.get(ScheduleJobEntity.JOB_PARAM_KEY); |
|
|
|
|
|
|
|
|
.get(ScheduleJobEntity.JOB_PARAM_KEY); |
|
|
|
|
|
|
|
|
//获取spring bean |
|
|
//获取spring bean |
|
|
ScheduleJobLogService scheduleJobLogService = (ScheduleJobLogService) SpringContextUtils.getBean("scheduleJobLogService"); |
|
|
ScheduleJobLogService scheduleJobLogService = (ScheduleJobLogService) SpringContextUtils.getBean("scheduleJobLogService"); |
|
|
@ -45,31 +44,31 @@ public class ScheduleJob extends QuartzJobBean { |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
//执行任务 |
|
|
//执行任务 |
|
|
logger.debug("任务准备执行,任务ID:" + scheduleJob.getJobId()); |
|
|
|
|
|
|
|
|
|
|
|
Object target = SpringContextUtils.getBean(scheduleJob.getBeanName()); |
|
|
|
|
|
Method method = target.getClass().getDeclaredMethod("run", String.class); |
|
|
|
|
|
method.invoke(target, scheduleJob.getParams()); |
|
|
|
|
|
|
|
|
|
|
|
//任务执行总时长 |
|
|
|
|
|
long times = System.currentTimeMillis() - startTime; |
|
|
|
|
|
log.setTimes((int)times); |
|
|
|
|
|
//任务状态 0:成功 1:失败 |
|
|
|
|
|
log.setStatus(0); |
|
|
|
|
|
|
|
|
|
|
|
logger.debug("任务执行完毕,任务ID:" + scheduleJob.getJobId() + " 总共耗时:" + times + "毫秒"); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logger.error("任务执行失败,任务ID:" + scheduleJob.getJobId(), e); |
|
|
|
|
|
|
|
|
|
|
|
//任务执行总时长 |
|
|
|
|
|
long times = System.currentTimeMillis() - startTime; |
|
|
|
|
|
log.setTimes((int)times); |
|
|
|
|
|
|
|
|
|
|
|
//任务状态 0:成功 1:失败 |
|
|
|
|
|
log.setStatus(1); |
|
|
|
|
|
log.setError(StringUtils.substring(e.toString(), 0, 2000)); |
|
|
|
|
|
}finally { |
|
|
|
|
|
scheduleJobLogService.save(log); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
logger.debug("任务准备执行,任务ID:" + scheduleJob.getJobId()); |
|
|
|
|
|
|
|
|
|
|
|
Object target = SpringContextUtils.getBean(scheduleJob.getBeanName()); |
|
|
|
|
|
Method method = target.getClass().getDeclaredMethod("run", String.class); |
|
|
|
|
|
Object invoke = method.invoke(target, scheduleJob.getParams()); |
|
|
|
|
|
|
|
|
|
|
|
//任务执行总时长 |
|
|
|
|
|
long times = System.currentTimeMillis() - startTime; |
|
|
|
|
|
log.setTimes((int) times); |
|
|
|
|
|
//任务状态 0:成功 1:失败 |
|
|
|
|
|
log.setStatus(0); |
|
|
|
|
|
log.setErrorMsg(invoke.toString()); |
|
|
|
|
|
logger.debug("任务执行完毕,任务ID:" + scheduleJob.getJobId() + " 总共耗时:" + times + "毫秒"); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logger.error("任务执行失败,任务ID:" + scheduleJob.getJobId(), e); |
|
|
|
|
|
|
|
|
|
|
|
//任务执行总时长 |
|
|
|
|
|
long times = System.currentTimeMillis() - startTime; |
|
|
|
|
|
log.setTimes((int) times); |
|
|
|
|
|
//任务状态 0:成功 1:失败 |
|
|
|
|
|
log.setStatus(1); |
|
|
|
|
|
log.setErrorMsg(e.getMessage()); |
|
|
|
|
|
log.setError(StringUtils.substring(e.toString(), 0, 2000)); |
|
|
|
|
|
} finally { |
|
|
|
|
|
scheduleJobLogService.save(log); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |