任务执行实体
/** * 任务执行情况详情 */ public class JobExecuteDetail implements Serializable{ /** * */ private static final long serialVersionUID = 3145929362286951322L; /** * 名称 */ private String triggerName; /** * 组名 */ private String triggerGroup; /** * 成功数 */ private int successTimes = 0; /** * 执行时间 */ private String date; public String getTriggerName() { return triggerName; } public void setTriggerName(String triggerName) { this.triggerName = triggerName; } public String getTriggerGroup() { return triggerGroup; } public void setTriggerGroup(String triggerGroup) { this.triggerGroup = triggerGroup; } public int getSuccessTimes() { return successTimes; } public void setSuccessTimes(int successTimes) { this.successTimes = successTimes; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } @Override public String toString() { return "JobExecuteDetail [triggerName=" + triggerName + ", triggerGroup=" + triggerGroup + ", successTimes=" + successTimes + ", date=" + date + "]"; } }
执行的任务记录到队列,
JobExecuteDetailQueue是自己声明的一个队列
public class SerializableQuartzJobBean extends QuartzJobBean { static final Logger logger = LoggerFactory.getLogger(SerializableQuartzJobBean.class); @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { JobExecuteDetail detail= new JobExecuteDetail(); try { Object otargetObject = ctx.getBean(targetObject); TriggerKey key = context.getTrigger().getKey(); detail.setTriggerGroup(key.getGroup()); detail.setTriggerName(key.getName()); detail.setDate(DateHelper.format(new Date(), "yyyy-MM-dd")); Method m = null; try { m = otargetObject.getClass().getMethod(targetMethod, new Class[] {}); m.invoke(otargetObject, new Object[] {}); detail.setSuccessTimes(1); } catch (SecurityException e) { e.printStackTrace(); logger.error("任务调度运行出错:", e); } catch (NoSuchMethodException e) { e.printStackTrace(); logger.error("任务调度运行出错:", e); } } catch (Exception e) { throw new JobExecutionException(e); } finally { JobExecuteDetailQueue jobExecuteDetailQueue = ctx.getBean(JobExecuteDetailQueue.class); jobExecuteDetailQueue.offer(detail); } } private ApplicationContext ctx; public void setApplicationContext(ApplicationContext applicationContext) { this.ctx = applicationContext; } private String targetObject; public void setTargetObject(String targetObject) { this.targetObject = targetObject; } private String targetMethod; public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; } }
从队列中取出任务执行的内容存入数据库即可