• spring整合quartz实现动态定时器


    1.

    <!-- 加载定时任务:容器启动时自动加载20140812添加 songzhanliang     -->
        <bean id="scheduleJobConfig" class="com.shz.base.init.ScheduleJobConfig">
            <constructor-arg index="0">
                <ref bean="scheduleJobService" />
            </constructor-arg>
            <!-- 定时任务是否随容器一起启动 -->
            <constructor-arg index="1" type="java.lang.Boolean">
                <value>true</value>
            </constructor-arg>
        </bean>

    2.

    public class ScheduleJobConfig implements InitializingBean {
        
        
        Logger logger = Logger.getLogger(ScheduleJobConfig.class);
        
        //@Autowired
        private IScheduleJobService scheduleJobService;
        
        private Boolean autoStart ;
        
        
        
        public IScheduleJobService getScheduleJobService() {
            return scheduleJobService;
        }

        public void setScheduleJobService(IScheduleJobService scheduleJobService) {
            this.scheduleJobService = scheduleJobService;
        }

        public Boolean getAutoStart() {
            return autoStart;
        }

        public void setAutoStart(Boolean autoStart) {
            this.autoStart = autoStart;
        }

        public ScheduleJobConfig(IScheduleJobService scheduleJobService,Boolean autoStart) {
            this.setScheduleJobService(scheduleJobService) ;
            this.setAutoStart(autoStart) ;
        }
        
        /**
         * * @Description:
         * * @param arg0
         * * @author:songzhanliang
         * * @Date :2014-8-12 下午2:40:01
         * */
        public void initialize() {
            //如果没有配置自行启动,则跳出此代码段
            if(!this.getAutoStart()){
                return ;
            }
            //如果配置自行启动,查询系统参数,启动相关定时任务
            List<TJob> jobs = null;
            try{
                jobs = this.scheduleJobService.find("from TJob model where model.jobState=1");
            }catch(Exception e){
                logger.error("==数据库查询异常:{位置:任务配置 [TJob]}:" + e.getStackTrace());
            }
            //把查询到的结果集加载到ScheduleManager的日程表中
            for (TJob scheduleJob:jobs) {
                String groupName = scheduleJob.getExeClass() ;
                String jobName = scheduleJob.getExeMethod() ;
                QuartzManager .addJob(groupName,jobName, QuartzJobBeanFactory.class, scheduleJob.getExeTime());
            }
        }

        /**
         * * @Description:
         * * @throws Exception
         * * @author:songzhanliang
         * * @Date :2014-8-12 下午2:40:01
         * */
        @Override
        public void afterPropertiesSet() throws Exception {
            // TODO Auto-generated method stub
            initialize();
        }

    }

    /**
     * Quartz调度管理器
     * 
     * @author Administrator
     * 
     */
    public class QuartzManager {
        private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
        private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
    
        /**
         * @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
         * 
         * @param sched
         *            调度器
         * 
         * @param jobName
         *            任务名
         * @param cls
         *            任务
         * @param time
         *            时间设置,参考quartz说明文档
         * 
         * @Title: QuartzManager.java
         */
        public static void addJob(Scheduler sched, String jobName, @SuppressWarnings("rawtypes") Class cls, String time) {
            try {
                JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任务名,任务组,任务执行类
                // 触发器
                CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组
                trigger.setCronExpression(time);// 触发器时间设定
                sched.scheduleJob(jobDetail, trigger);
                // 启动
                if (!sched.isShutdown()) {
                    sched.start();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description: 添加一个定时任务
         * 
         * @param sched
         *            调度器
         * 
         * @param jobName
         *            任务名
         * @param jobGroupName
         *            任务组名
         * @param triggerName
         *            触发器名
         * @param triggerGroupName
         *            触发器组名
         * @param jobClass
         *            任务
         * @param time
         *            时间设置,参考quartz说明文档
         * 
         * @Title: QuartzManager.java
         */
        public static void addJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName, @SuppressWarnings("rawtypes") Class jobClass, String time) {
            try {
                JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类
                // 触发器
                CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
                trigger.setCronExpression(time);// 触发器时间设定
                sched.scheduleJob(jobDetail, trigger);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description: 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
         * 
         * @param sched
         *            调度器
         * @param jobName
         * @param time
         * 
         * @Title: QuartzManager.java
         */
        @SuppressWarnings("rawtypes")
        public static void modifyJobTime(Scheduler sched, String jobName, String time) {
            try {
                CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
                if (trigger == null) {
                    return;
                }
                String oldTime = trigger.getCronExpression();
                if (!oldTime.equalsIgnoreCase(time)) {
                    JobDetail jobDetail = sched.getJobDetail(jobName, JOB_GROUP_NAME);
                    Class objJobClass = jobDetail.getJobClass();
                    removeJob(sched, jobName);
                    addJob(sched, jobName, objJobClass, time);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description: 修改一个任务的触发时间
         * 
         * @param sched
         *            调度器 *
         * @param sched
         *            调度器
         * @param triggerName
         * @param triggerGroupName
         * @param time
         * 
         * @Title: QuartzManager.java
         */
        public static void modifyJobTime(Scheduler sched, String triggerName, String triggerGroupName, String time) {
            try {
                CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName, triggerGroupName);
                if (trigger == null) {
                    return;
                }
                String oldTime = trigger.getCronExpression();
                if (!oldTime.equalsIgnoreCase(time)) {
                    CronTrigger ct = (CronTrigger) trigger;
                    // 修改时间
                    ct.setCronExpression(time);
                    // 重启触发器
                    sched.resumeTrigger(triggerName, triggerGroupName);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description: 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
         * 
         * @param sched
         *            调度器
         * @param jobName
         * 
         * @Title: QuartzManager.java
         */
        public static void removeJob(Scheduler sched, String jobName) {
            try {
                sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器
                sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器
                sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description: 移除一个任务
         * 
         * @param sched
         *            调度器
         * @param jobName
         * @param jobGroupName
         * @param triggerName
         * @param triggerGroupName
         * 
         * @Title: QuartzManager.java
         */
        public static void removeJob(Scheduler sched, String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
            try {
                sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
                sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
                sched.deleteJob(jobName, jobGroupName);// 删除任务
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description:启动所有定时任务
         * 
         * @param sched
         *            调度器
         * 
         * @Title: QuartzManager.java
         */
        public static void startJobs(Scheduler sched) {
            try {
                sched.start();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * @Description:关闭所有定时任务
         * 
         * 
         * @param sched
         *            调度器
         * 
         * 
         * @Title: QuartzManager.java
         */
        public static void shutdownJobs(Scheduler sched) {
            try {
                if (!sched.isShutdown()) {
                    sched.shutdown();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
  • 相关阅读:
    实现粘贴WORD图片的在线编辑器
    js实现大文件分片上传的工具
    Nginx实现大文件分片上传的方法
    百度WebUploader实现大文件分片上传的方法
    ckeditor粘贴上传图片
    umediter实现粘贴word图片
    java.net.Socket
    matlab创建对角型矩阵
    QtList
    matlab矩阵行最简形
  • 原文地址:https://www.cnblogs.com/shz365/p/6757238.html
Copyright © 2020-2023  润新知