use of com.dimple.project.log.service.QuartzJobLogService in project DimpleBlog by martin-chips.
the class ExecutionJob method executeInternal.
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
QuartzJob quartzJob = (QuartzJob) context.getMergedJobDataMap().get(QuartzJob.JOB_KEY);
// 获取spring bean
QuartzJobService quartzJobService = SpringUtils.getBean(QuartzJobService.class);
QuartzJobLogService quartzJobLogService = SpringUtils.getBean(QuartzJobLogService.class);
QuartzJobLog quartzJobLog = new QuartzJobLog();
quartzJobLog.setJobName(quartzJob.getJobName());
quartzJobLog.setBeanName(quartzJob.getBeanName());
quartzJobLog.setMethodName(quartzJob.getMethodName());
quartzJobLog.setMethodParams(quartzJob.getMethodParams());
quartzJobLog.setCronExpression(quartzJob.getCronExpression());
long startTime = System.currentTimeMillis();
try {
// 执行任务
log.info("任务准备执行,任务名称:{}", quartzJob.getJobName());
QuartzRunnable task = new QuartzRunnable(quartzJob.getBeanName(), quartzJob.getMethodName(), quartzJob.getMethodParams());
Future<Object> future = threadPoolTaskExecutor.submit(task);
Object result = future.get();
log.info("任务返回值:{}", result);
quartzJobLog.setResult(result.toString());
long times = System.currentTimeMillis() - startTime;
quartzJobLog.setCost(times);
// 任务状态
quartzJobLog.setStatus(true);
log.info("任务执行完毕,任务名称:{} 总共耗时:{} 毫秒", quartzJob.getJobName(), times);
} catch (Exception e) {
log.error("任务执行失败,任务名称:{}" + quartzJob.getJobName(), e);
long times = System.currentTimeMillis() - startTime;
quartzJobLog.setCost(times);
quartzJobLog.setStatus(false);
quartzJobLog.setException(getStackTrace(e));
// 设置为暂停状态
quartzJob.setStatus(false);
// 更新状态
quartzJobService.updateQuartzJob(quartzJob);
} finally {
quartzJobLogService.insertQuartzJobLog(quartzJobLog);
}
}
Aggregations