zoukankan      html  css  js  c++  java
  • Quartz-第四篇 常规quartz的使用

    1、目录结构

       

    2、主要文件 

      1》引入的jar包,quartz-2.2.2解压后lib下所有的jar包

      2》quartz.properties

    org.quartz.threadPool.threadCount: 2
    org.quartz.jobStore.misfireThreshold: 4000

      3》SchedulerUtil.java

    package com.lfy.scheduler;
    
    import org.quartz.CronTrigger;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    
    public class SchedulerUtil {
        
        private static Scheduler scheduler;
        
        private static CronTrigger cronTrigger;
        
        private static JobDetail jobDetail;
    
        public static Scheduler getScheduler() {
            return scheduler;
        }
    
        public static void setScheduler(Scheduler scheduler) {
            SchedulerUtil.scheduler = scheduler;
        }
    
        public static CronTrigger getCronTrigger() {
            return cronTrigger;
        }
    
        public static void setCronTrigger(CronTrigger cronTrigger) {
            SchedulerUtil.cronTrigger = cronTrigger;
        }
    
        public static JobDetail getJobDetail() {
            return jobDetail;
        }
    
        public static void setJobDetail(JobDetail jobDetail) {
            SchedulerUtil.jobDetail = jobDetail;
        }
    }

       4》HelloJob.java

    package com.lfy.jobdetail;
    
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    public class HelloJob implements Job {
    
        @Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("现在的时间是:"+ sf.format(date));
            //具体的业务逻辑
            System.out.println("Hello Quartz");
        }
    }

       5》CronTriggerScheduleApp.java

    package com.lfy.controller;
    
    import org.quartz.CronScheduleBuilder;
    import org.quartz.JobBuilder;
    import org.quartz.SchedulerException;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.lfy.jobdetail.HelloJob;
    import com.lfy.scheduler.SchedulerUtil;
    
    @Controller
    @RequestMapping("/App")
    public class CronTriggerScheduleApp {
    
        @RequestMapping(value="/startQuartz")
        public String startQuartz() throws SchedulerException {
            
            SchedulerUtil.setScheduler(new StdSchedulerFactory().getScheduler());
            
            SchedulerUtil.setJobDetail(JobBuilder.newJob(HelloJob.class)
                                                 .withIdentity("cronJob","group1")
                                                 .build());
            
            CronScheduleBuilder cronScheduleBuilder=CronScheduleBuilder.cronSchedule("/6 * * * * ? *");
            cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();
            SchedulerUtil.setCronTrigger(TriggerBuilder.newTrigger()
                                                       .withIdentity("cronTrigger")
                                                       .withSchedule(cronScheduleBuilder)
                                                       .build());
            SchedulerUtil.getScheduler().start();
            SchedulerUtil.getScheduler().scheduleJob(SchedulerUtil.getJobDetail(), 
                                                     SchedulerUtil.getCronTrigger());
            return "index";
        }
    }

       6》PauseController.java

    package com.lfy.controller;
    
    import org.quartz.JobKey;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.lfy.scheduler.SchedulerUtil;
    
    @Controller
    @RequestMapping("/PauseController")
    public class PauseController {
    
        @RequestMapping(value="/pause")
        public void pauseCronTrigger() {
            
            JobKey jobkey=JobKey.jobKey("cronJob","group1");
            Scheduler scheduler=SchedulerUtil.getScheduler();
            try {
                scheduler.pauseJob(jobkey);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }

       7》ResumeController.java

    package com.lfy.controller;
    
    import org.quartz.JobKey;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.lfy.scheduler.SchedulerUtil;
    
    @Controller
    @RequestMapping("/ResumeController")
    public class ResumeController {
    
        @RequestMapping(value="/resume")
        public void resumeCronTrigger() {
            
            JobKey jobkey=JobKey.jobKey("cronJob","group1");
            Scheduler scheduler=SchedulerUtil.getScheduler();
            try {
                scheduler.resumeJob(jobkey);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }

       8》AlterTriggerTimeController.java

    package com.lfy.controller;
    
    import org.quartz.CronScheduleBuilder;
    import org.quartz.CronTrigger;
    import org.quartz.Scheduler;
    import org.quartz.TriggerKey;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.lfy.scheduler.SchedulerUtil;
    
    @Controller
    @RequestMapping("/RescheduleController")
    public class AlterTriggerTimeController {
    
        @RequestMapping(value="/reschedule")
        public void rescheduleCronTrigger() {
            
            String newCron="/3 * * * * ? *";
            TriggerKey triggerKey = TriggerKey.triggerKey("cronJob","group1");
             
            try {
                Scheduler sched = SchedulerUtil.getScheduler();
                CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);
                if (trigger == null) {
                    return;
                }
                String oldTime = trigger.getCronExpression();
                if (!oldTime.equalsIgnoreCase(newCron)) {
                    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(newCron);
                    // 按新的cronExpression表达式重新构建trigger
                    trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
                    // 按新的trigger重新设置job执行
                    sched.rescheduleJob(triggerKey, trigger);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

       9》DeleteController.java

    package com.lfy.controller;
    
    import org.quartz.JobKey;
    import org.quartz.Scheduler;
    import org.quartz.TriggerKey;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.lfy.scheduler.SchedulerUtil;
    
    @Controller
    @RequestMapping("/DeleteController")
    public class DeleteController {
    
        @RequestMapping(value="/delete")
        public void deleteCronTrigger() {
            
            TriggerKey triggerKey = TriggerKey.triggerKey("cronJob","group1");
            JobKey jobKey = JobKey.jobKey("cronJob","group1");
            try {
                Scheduler sched = SchedulerUtil.getScheduler();
                sched.pauseTrigger(triggerKey);// 停止触发器
                sched.unscheduleJob(triggerKey);// 移除触发器
                sched.deleteJob(jobKey);// 删除任务
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
  • 相关阅读:
    dedecms 织梦利用arcpagelist标签实现首页arclist分页
    notepad++使用正则表达式的查找替换
    织梦dedecms5.7二级域名文章图片不显示修改方法.相对路径改为绝对路径的方法
    织梦文章页分页导航条 分页代码副标题调用
    织梦导航 currentstyle 点击li添加class类 样式
    dede让channelartlist标签支持currentstyle属性 完美解决
    [转载]织梦DEDE多选项筛选_联动筛选功能的实现_二次开发
    Nginx Rewrite 规则入门 伪静态规则
    对拷 使用scp命令在两台linux上对拷文件或者文件夹
    Apache搭建简单的图片访问服务器
  • 原文地址:https://www.cnblogs.com/ZeroMZ/p/11495031.html
Copyright © 2011-2022 走看看