Chinaunix首页 | 论坛 | 博客
  • 博客访问: 615619
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-17 10:51:17


点击(此处)折叠或打开


  1. package org.fh.util;

  2. import java.util.Map;

  3. import org.quartz.CronScheduleBuilder;
  4. import org.quartz.CronTrigger;
  5. import org.quartz.Job;
  6. import org.quartz.JobBuilder;
  7. import org.quartz.JobDetail;
  8. import org.quartz.JobKey;
  9. import org.quartz.Scheduler;
  10. import org.quartz.SchedulerFactory;
  11. import org.quartz.TriggerBuilder;
  12. import org.quartz.TriggerKey;
  13. import org.quartz.impl.StdSchedulerFactory;
  14.   
  15. /**
  16.  * 说明:定时任务管理类
  17.  * 作者:FH Admin
  18.  * from:fhadmin.cn
  19.  */
  20. public class QuartzManager {
  21.     private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); //创建一个SchedulerFactory工厂实例
  22.     private static String JOB_GROUP_NAME = "FH_JOBGROUP_NAME";                     //任务组
  23.     private static String TRIGGER_GROUP_NAME = "FH_TRIGGERGROUP_NAME";             //触发器组
  24.   
  25.     /**添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
  26.      * @param jobName 任务名
  27.      * @param cls 任务
  28.      * @param time 时间设置,参考quartz说明文档
  29.      */
  30.     public static void addJob(String jobName, Class<? extends Job> cls, String time) {
  31.         try {
  32.             Scheduler sched = gSchedulerFactory.getScheduler();                                         //通过SchedulerFactory构建Scheduler对象
  33.             JobDetail jobDetail= JobBuilder.newJob(cls).withIdentity(jobName,JOB_GROUP_NAME).build();    //用于描叙Job实现类及其他的一些静态信息,构建一个作业实例
  34.             CronTrigger trigger = (CronTrigger) TriggerBuilder
  35.                     .newTrigger()                                                                         //创建一个新的TriggerBuilder来规范一个触发器
  36.                     .withIdentity(jobName, TRIGGER_GROUP_NAME)                                            //给触发器起一个名字和组名
  37.                     .withSchedule(CronScheduleBuilder.cronSchedule(time))
  38.                     .build();
  39.             sched.scheduleJob(jobDetail, trigger);
  40.             if (!sched.isShutdown()) {
  41.                 sched.start();      // 启动
  42.             }
  43.         } catch (Exception e) {
  44.             throw new RuntimeException(e);
  45.         }
  46.     }
  47.     
  48.     /**添加一个定时任务,使用默认的任务组名,触发器名,触发器组名 (带参数)
  49.      * @param jobName 任务名
  50.      * @param cls 任务
  51.      * @param time 时间设置,参考quartz说明文档
  52.      */
  53.     public static void addJob(String jobName, Class<? extends Job> cls, String time, Map<String,Object> parameter) {
  54.         try {
  55.             Scheduler sched = gSchedulerFactory.getScheduler();                                         //通过SchedulerFactory构建Scheduler对象
  56.             JobDetail jobDetail= JobBuilder.newJob(cls).withIdentity(jobName,JOB_GROUP_NAME).build();    //用于描叙Job实现类及其他的一些静态信息,构建一个作业实例
  57.             jobDetail.getJobDataMap().put("parameterList", parameter);                                    //传参数
  58.             CronTrigger trigger = (CronTrigger) TriggerBuilder
  59.                     .newTrigger()                                                                         //创建一个新的TriggerBuilder来规范一个触发器
  60.                     .withIdentity(jobName, TRIGGER_GROUP_NAME)                                            //给触发器起一个名字和组名
  61.                     .withSchedule(CronScheduleBuilder.cronSchedule(time))
  62.                     .build();
  63.             sched.scheduleJob(jobDetail, trigger);
  64.             if (!sched.isShutdown()) {
  65.                 sched.start();      // 启动
  66.             }
  67.         } catch (Exception e) {
  68.             throw new RuntimeException(e);
  69.         }
  70.     }
  71.   
  72.     /**添加一个定时任务
  73.      * @param jobName    任务名
  74.      * @param jobGroupName    任务组名
  75.      * @param triggerName    触发器名
  76.      * @param triggerGroupName    触发器组名
  77.      * @param jobClass    任务
  78.      * @param time    时间设置,参考quartz说明文档
  79.      */
  80.     public static void addJob(String jobName, String jobGroupName,
  81.             String triggerName, String triggerGroupName, Class<? extends Job> jobClass,
  82.             String time) {
  83.         try {
  84.             Scheduler sched = gSchedulerFactory.getScheduler();
  85.             JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName,jobGroupName).build();// 任务名,任务组,任务执行类
  86.             CronTrigger trigger = (CronTrigger) TriggerBuilder     // 触发器
  87.                     .newTrigger()
  88.                     .withIdentity(triggerName, triggerGroupName)
  89.                     .withSchedule(CronScheduleBuilder.cronSchedule(time))
  90.                     .build();
  91.             sched.scheduleJob(jobDetail, trigger);
  92.             if (!sched.isShutdown()) {
  93.                 sched.start();      // 启动
  94.             }
  95.         } catch (Exception e) {
  96.             throw new RuntimeException(e);
  97.         }
  98.     }
  99.     
  100.     /**添加一个定时任务 (带参数)
  101.      * @param jobName    任务名
  102.      * @param jobGroupName    任务组名
  103.      * @param triggerName    触发器名
  104.      * @param triggerGroupName    触发器组名
  105.      * @param jobClass    任务
  106.      * @param time    时间设置,参考quartz说明文档
  107.      */
  108.     public static void addJob(String jobName, String jobGroupName,
  109.             String triggerName, String triggerGroupName, Class<? extends Job> jobClass,
  110.             String time, Map<String,Object> parameter) {
  111.         try {
  112.             Scheduler sched = gSchedulerFactory.getScheduler();
  113.             JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName,jobGroupName).build();// 任务名,任务组,任务执行类
  114.             jobDetail.getJobDataMap().put("parameterList", parameter);                                //传参数
  115.             CronTrigger trigger = (CronTrigger) TriggerBuilder     // 触发器
  116.                     .newTrigger()
  117.                     .withIdentity(triggerName, triggerGroupName)
  118.                     .withSchedule(CronScheduleBuilder.cronSchedule(time))
  119.                     .build();
  120.             sched.scheduleJob(jobDetail, trigger);
  121.             if (!sched.isShutdown()) {
  122.                 sched.start();      // 启动
  123.             }
  124.         } catch (Exception e) {
  125.             throw new RuntimeException(e);
  126.         }
  127.     }
  128.   
  129.     /** 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
  130.      * @param jobName    任务名
  131.      * @param time    新的时间设置
  132.      */
  133.     public static void modifyJobTime(String jobName, String time) {
  134.         try {
  135.             Scheduler sched = gSchedulerFactory.getScheduler();                             //通过SchedulerFactory构建Scheduler对象
  136.             TriggerKey triggerKey = TriggerKey.triggerKey(jobName,TRIGGER_GROUP_NAME);         //通过触发器名和组名获取TriggerKey
  137.             CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerKey);                //通过TriggerKey获取CronTrigger
  138.             if (trigger == null) {
  139.                 return;
  140.             }
  141.             String oldTime = trigger.getCronExpression();
  142.             if (!oldTime.equalsIgnoreCase(time)) {
  143.                 JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);                        //通过任务名和组名获取JobKey
  144.                 JobDetail jobDetail = sched.getJobDetail(jobKey);
  145.                 Class<? extends Job> objJobClass = jobDetail.getJobClass();
  146.                 removeJob(jobName);
  147.                 addJob(jobName, objJobClass, time);
  148.             }
  149.         } catch (Exception e) {
  150.             throw new RuntimeException(e);
  151.         }
  152.     }
  153.     
  154.     /**修改一个任务的触发时间
  155.      * @param triggerName    任务名称
  156.      * @param triggerGroupName    传过来的任务名称
  157.      * @param time    更新后的时间规则
  158.      */
  159.     public static void modifyJobTime(String triggerName, String triggerGroupName, String time) {
  160.         try {
  161.             Scheduler sched = gSchedulerFactory.getScheduler();                             //通过SchedulerFactory构建Scheduler对象
  162.             TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName);     //通过触发器名和组名获取TriggerKey
  163.             CronTrigger trigger = (CronTrigger)sched.getTrigger(triggerKey);                //通过TriggerKey获取CronTrigger
  164.             if (trigger == null) return;
  165.             CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(trigger.getCronExpression());
  166.             String oldTime = trigger.getCronExpression();
  167.             if (!oldTime.equalsIgnoreCase(time)) {
  168.                 trigger = (CronTrigger)trigger.getTriggerBuilder()        //重新构建trigger
  169.                         .withIdentity(triggerKey)
  170.                         .withSchedule(scheduleBuilder)
  171.                         .withSchedule(CronScheduleBuilder.cronSchedule(time))
  172.                         .build();
  173.                 sched.rescheduleJob(triggerKey, trigger);                //按新的trigger重新设置job执行
  174.             }
  175.         } catch (Exception e) {
  176.             throw new RuntimeException(e);
  177.         }
  178.     }
  179.     
  180.     /**移除一个任务(使用默认的任务组名,触发器名,触发器组名)
  181.      * @param jobName    任务名称
  182.      */
  183.     public static void removeJob(String jobName) {
  184.         try {
  185.             Scheduler sched = gSchedulerFactory.getScheduler();
  186.             TriggerKey triggerKey = TriggerKey.triggerKey(jobName,TRIGGER_GROUP_NAME);     //通过触发器名和组名获取TriggerKey
  187.             JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);                        //通过任务名和组名获取JobKey
  188.             sched.pauseTrigger(triggerKey);    // 停止触发器
  189.             sched.unscheduleJob(triggerKey);// 移除触发器
  190.             sched.deleteJob(jobKey);        // 删除任务
  191.         } catch (Exception e) {
  192.             throw new RuntimeException(e);
  193.         }
  194.     }
  195.     
  196.     /**移除一个任务
  197.      * @param jobName    任务名
  198.      * @param jobGroupName    任务组名
  199.      * @param triggerName    触发器名
  200.      * @param triggerGroupName    触发器组名
  201.      */
  202.     public static void removeJob(String jobName, String jobGroupName,String triggerName, String triggerGroupName) {
  203.         try {
  204.             Scheduler sched = gSchedulerFactory.getScheduler();
  205.             TriggerKey triggerKey = TriggerKey.triggerKey(triggerName,triggerGroupName);     //通过触发器名和组名获取TriggerKey
  206.             JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);                            //通过任务名和组名获取JobKey
  207.             sched.pauseTrigger(triggerKey);    // 停止触发器
  208.             sched.unscheduleJob(triggerKey);// 移除触发器
  209.             sched.deleteJob(jobKey);        // 删除任务
  210.         } catch (Exception e) {
  211.             throw new RuntimeException(e);
  212.         }
  213.     }
  214.     
  215.     /**
  216.      * 启动所有定时任务
  217.      */
  218.     public static void startJobs() {
  219.         try {
  220.             Scheduler sched = gSchedulerFactory.getScheduler();
  221.             sched.start();
  222.         } catch (Exception e) {
  223.             throw new RuntimeException(e);
  224.         }
  225.     }
  226.   
  227.     /**
  228.      * 关闭所有定时任务
  229.      */
  230.     public static void shutdownJobs() {
  231.         try {
  232.             Scheduler sched = gSchedulerFactory.getScheduler();
  233.             if (!sched.isShutdown()) {
  234.                 sched.shutdown();
  235.             }
  236.         } catch (Exception e) {
  237.             throw new RuntimeException(e);
  238.         }
  239.     }
  240. }


阅读(795) | 评论(0) | 转发(0) |
0

上一篇:java MD5 加密

下一篇:java 常用工具类

给主人留下些什么吧!~~