zoukankan      html  css  js  c++  java
  • Quartz实现执行任务记录数据库,方便计算任务的执行次数以及成功次数

    任务执行实体

    /** 
     * 任务执行情况详情
     */
    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;
        }
    }

    从队列中取出任务执行的内容存入数据库即可

  • 相关阅读:
    NOIP2016 愤怒的小鸟
    LCIS code force 10D
    UVA 1398
    uva1382 Distant Galaxy
    洛谷-3930(我在洛谷上也写了题解)
    HDU-1505 City Game
    导弹拦截n logn的算法(单调性)洛谷1020
    POJ 1182 食物链
    POJ
    1202. 交换字符串中的元素
  • 原文地址:https://www.cnblogs.com/Cassie-wang/p/10973232.html
Copyright © 2011-2022 走看看