任务调度quartz整理

一张图,了解quartz运行机制:

此图表示:Scheduler是容器,Trigger是多个触发器,jobDetail是多个任务,Calendar是多个日历。

jobDetail任务,需要指定类实现Job接口,并重写execute方法。并且Jobdetail初始化时需要指定任务执行的运行时类。

 

  1     package com.lec.util.job;  
  2       
  3     import java.text.ParseException;  
  4     import java.util.Date;  
  5     import java.util.HashMap;  
  6     import org.quartz.CronTrigger;  
  7     import org.quartz.Job;  
  8     import org.quartz.JobDetail;  
  9     import org.quartz.Scheduler;  
 10     import org.quartz.SchedulerException;  
 11     import org.quartz.SchedulerFactory;  
 12     import org.quartz.SimpleTrigger;  
 13     import org.quartz.Trigger;  
 14     import org.quartz.impl.StdSchedulerFactory;  
 15       
 16     import com.lec.assurance.utils.StringHandler;  
 17       
 18     /** 
 19      * CronTrigger任务调度器 
 20      * @author lcy 
 21      *  Dec 1, 2011 
 22      */  
 23     public class CronTriggerRunner {  
 24         private static SchedulerFactory sf = new StdSchedulerFactory();  
 25           
 26           
 27           
 28         /** 
 29          * 根据任务名和任务组名查询一个任务 
 30          * @param jobName 
 31          * @param jobGroupName 
 32          * @return 
 33          * @throws SchedulerException 
 34          */  
 35         public static JobDetail getJob(String jobName,String jobGroupName) throws SchedulerException{  
 36             Scheduler sched = sf.getScheduler();  
 37             JobDetail jobDetail=sched.getJobDetail(jobName, jobGroupName);  
 38             if(jobDetail != null){  
 39                 return jobDetail;  
 40             }  
 41             return null;  
 42         }  
 43              
 44         /** 
 45         * 添加一个CronTrigger定时任务 
 46         * @param jobName 任务名 
 47         * @param jobGroupName 任务组名 
 48         * @param triggerName 触发器名 
 49         * @param triggerGroupName 触发器组名 
 50         * @param job     任务 
 51         * @param time    时间设置,参考quartz说明文档 
 52         * @param params 传递参数 
 53         * @throws SchedulerException 
 54         * @throws ParseException  
 55         */  
 56         public static void addJob(String jobName,String jobGroupName,String triggerName,  
 57                 String triggerGroupName,String time,HashMap<String,Object> params,  
 58                 Job jobClass) throws SchedulerException, ParseException{  
 59            Scheduler sched = sf.getScheduler();  
 60            JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass.getClass());//任务名,任务组,任务执行类  
 61            if(params != null){  
 62                   jobDetail.getJobDataMap().put("params",params);  
 63               }  
 64            //触发器  
 65            CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);//触发器名,触发器组  
 66            trigger.setCronExpression(time);//触发器时间设定  
 67            sched.scheduleJob(jobDetail,trigger);  
 68            if(!sched.isShutdown())  
 69                sched.start();  
 70         }  
 71           
 72         /** 
 73          * 添加一个SimpleTrigger定时任务 
 74          * @param jobName 任务名 
 75          * @param jobGroupName 任务组名 
 76          * @param triggerName 触发器名 
 77          * @param triggerGroupName 触发器组名 
 78          * @param startTime 开始时间 
 79          * @param repeatInterval 间隔时间 
 80          * @param repeatCount 调用次数 
 81          * @param jobClass 任务 
 82          * @param params 传递参数 
 83          * @throws SchedulerException 
 84          */  
 85         public static void addJob(String  jobName,String  jobGroup,  
 86                 String  triggerName,String  triggerGroup,Date startTime,  
 87                 Integer repeatInterval,Integer repeatCount,Job jobClass,  
 88                 HashMap<String,Object> params) throws   
 89                 SchedulerException{  
 90             JobDetail jobDetail = new JobDetail(jobName, jobGroup, jobClass.getClass());  
 91             if(params != null){  
 92                  jobDetail.getJobDataMap().put("params",params);  
 93              }  
 94             //触发器  
 95             SimpleTrigger simpleTrigger = new SimpleTrigger(triggerName, triggerGroup);  
 96             //设置开始时间  
 97             simpleTrigger.setStartTime(startTime);  
 98             //设置间隔时间  
 99             simpleTrigger.setRepeatInterval(repeatInterval);  
100             //设置调用次数  
101             simpleTrigger.setRepeatCount(repeatCount);  
102               
103             Scheduler sched = sf.getScheduler();  
104             sched.scheduleJob(jobDetail,simpleTrigger);  
105             if(!sched.isShutdown())  
106                sched.start();  
107         }  
108              
109         /**  
110         * 根据任务名和任务组名修改任务的触发时间 
111         * @param jobName 任务名 
112         * @param jobGroupName 任务组名 
113         * @param time 
114         * @throws SchedulerException 
115         * @throws ParseException 
116         */  
117         public static void modifyJobTime(String jobName,String jobGroupName,String time)   
118                 throws SchedulerException, ParseException{  
119             Scheduler sched = sf.getScheduler();  
120             Trigger[] trigger = sched.getTriggersOfJob(jobName,jobGroupName);  
121             if(trigger != null){  
122                 for(int i=0;i<trigger.length;i++){  
123                     ((CronTrigger)trigger[i]).setCronExpression(time);  
124                     sched.resumeTrigger(trigger[i].getName(),trigger[i].getGroup());  
125                 }  
126             }  
127         }  
128              
129         /** 
130         * 根据触发器名修改一个任务的触发时间 
131         * @param triggerName触发器名 
132         * @param triggerGroupName触发器组名 
133         * @param time 
134         * @throws SchedulerException 
135         * @throws ParseException 
136         */  
137         public static void modifyTriggerTime(String triggerName,String triggerGroupName,  
138                      String time)  throws SchedulerException, ParseException{  
139             Scheduler sched = sf.getScheduler();  
140             Trigger trigger = sched.getTrigger(triggerName,triggerGroupName);  
141             if(trigger != null){  
142                 //修改时间  
143                 ((CronTrigger)trigger).setCronExpression(time);  
144                //重启触发器  
145                sched.resumeTrigger(triggerName,triggerGroupName);  
146            }  
147         }  
148           
149         /** 
150         * 移除任务 
151         * @param jobName任务名 
152         * @param jobGroupName任务组名 
153         * @param triggerGroupName触发器组名 
154         * @throws SchedulerException 
155         */  
156         public static void removeJob(String jobName,String jobGroupName,String triggerGroupName)   
157                 throws SchedulerException{  
158           Scheduler sched = sf.getScheduler();  
159           String[] triggerNames = sched.getTriggerNames(triggerGroupName);  
160           if(triggerNames!=null){  
161               for(int i=0;i<triggerNames.length;i++){  
162                   sched.pauseTrigger(triggerNames[i],triggerGroupName);//停止触发器  
163                   sched.unscheduleJob(triggerNames[i],triggerGroupName);//移除触发器   
164               }  
165           }  
166           sched.deleteJob(jobName,jobGroupName);//删除任务  
167         }  
168              
169         /** 
170         * 移除一个任务 
171         * @param jobName任务名 
172         * @param jobGroupName任务组名 
173         * @param triggerName触发器名 
174         * @param triggerGroupName触发器组名 
175         * @throws SchedulerException 
176         */  
177         public static void removeJob(String jobName,String jobGroupName,  
178                 String triggerName,String triggerGroupName)   
179                 throws SchedulerException{  
180           Scheduler sched = sf.getScheduler();  
181           sched.pauseTrigger(triggerName,triggerGroupName);//停止触发器  
182           sched.unscheduleJob(triggerName,triggerGroupName);//移除触发器  
183           sched.deleteJob(jobName,jobGroupName);//删除任务  
184         }  
185           
186         /** 
187          * 停止一个任务 需要注意的是:这里停止的job必须是实现InterruptableJob的 
188          * @param jobName 
189          * @param jobGroupName 
190          * @throws SchedulerException 
191          */  
192         public static void stopJob(String jobName,String jobGroupName) throws SchedulerException {  
193               Scheduler sched = sf.getScheduler();  
194               sched.interrupt(jobName, jobGroupName);  
195         }  
196           
197         /** 
198          * 计算时间表达式 
199          * @param second        秒 
200          * @param minute        分 
201          * @param hour          时 
202          * @param day           日期 
203          * @param month         月份 
204          * @param week          星期 
205          * @param year          年份 
206          * @param isContactYear 是否包括年份 
207          * @return 
208          */  
209         public static String getCexpress(String second,String minute,String hour,  
210                 String day,String month,String week,String year,boolean isContactYear) {  
211             String cexpress="";  
212             //秒,默认为"0" 取值范围:0-59  
213             if(StringHandler.isValidStr(second)){  
214                 cexpress+=Integer.parseInt(second)+" ";  
215             }else{  
216                 cexpress+="0 ";  
217             }  
218               
219             /** 
220              * 分 取值范围:0-59 
221              * 默认为"*"      表示每一分钟 
222              * 如果是0 12     表示整12:00触发 
223              * 如果是0/5 12   表示在每天下午12点到12:55期间的每5分钟触发 , 
224              * 如果是0-5 12   表示12:00到12:05期间的每1分钟触发 
225              * 如果是10,15 12 表示12:10和12:15触发 
226              */  
227             if(StringHandler.isValidStr(minute)){  
228                 cexpress+=minute+" ";  
229             }else{  
230                 cexpress+="* ";  
231             }  
232               
233             /** 
234              * 小时 取值范围:0-23 
235              * 默认为"*"      表示每一个小时 
236              * 如果是0        表示凌晨触发 
237              * 如果是0-5        表示凌晨到5:00期间 
238              * 如果是10,15    表示10:00和15:00触发 
239              */  
240             if(StringHandler.isValidStr(hour)){  
241                 cexpress+=hour+" ";  
242             }else{  
243                 cexpress+="* ";  
244             }  
245               
246             /** 
247              * 日期 取值范围:1-31 
248              * 默认为"*" 表示每天 
249              * 如果是15  表示每个月的15号 
250              * 如果是L   表示每个月最后一天  
251              * 注:在指定了星期的时候,把日期这里设置为"?" 
252              */  
253             if(StringHandler.isValidStr(day)){  
254                 cexpress+=day+" ";  
255             }else{  
256                 cexpress+="* ";  
257             }  
258               
259             /** 
260              * 月份 取值范围:1-12 
261              * 默认为"*"  表示每个月 
262              * 如果是 12  表示12月份 
263              */  
264             if(StringHandler.isValidStr(month)){  
265                 cexpress+=month+" ";  
266             }else{  
267                 cexpress+="* ";  
268             }  
269               
270             /** 
271              * 星期  取值范围:1-7 MON,TUE,WED,THU,FRI,SAT,SUN  其中1表示星期日,以此类推 
272              * 默认为"?" 
273              * 如果是WED      表示每个星期三 
274              * 如果是MON-FRI  表示星期一到星期五 
275              * 如果是6L       表示每月最后一个星期五 
276              * 如果是6#3      表示每月第三个星期五 
277              */  
278             if(StringHandler.isValidStr(week)){  
279                 cexpress+=week+" ";  
280             }else{  
281                 cexpress+="? ";  
282             }  
283               
284             //如果包括年份  
285             if(isContactYear){  
286                 /** 
287                  * 年份  取值范围:1970-2099 
288                  * 默认值为空 
289                  * 如果是2010 表示只有2010年触发时间 
290                  * 如果是2010,2020 表示只有2010 
291                  */  
292                 if(StringHandler.isValidStr(year)){  
293                     cexpress+=year;   
294                 }  
295             }  
296               
297             return cexpress;  
298         }  
299       
300         public static void main(String [] args){  
301             try {  
302                 /*--------------------创建一个指定时间执行的job-----------------------*/  
303                 JobDetail jobDetail = new JobDetail("fundsrecord_jobname_folkholdersId1",   
304                         "fundsrecord_jobgroupname_folkholdersId1",FundsrecordJob.class);  
305                 // ①-1:创建CronTrigger,指定组及名称  
306                 CronTrigger cronTrigger = new CronTrigger("fundsrecord_triggername_folkholdersId1",  
307                         "fundsrecord_triggergroupname_folkholdersId1");  
308                 // ①-2:定义Cron表达式  
309                 String timeexp = "";  
310                 String second = "0";  
311                 String minute = "15";  
312                 String hour = "0";  
313                 String day = "L";//当月的最后一天  
314                 String month = "";  
315                 String week = "?";  
316                 month = "6,12";  
317                 timeexp = getCexpress(second, minute, hour, day, month, week, null, false);  
318                   
319                 if(!"0 * * * * ?".equals(timeexp)){//如果没有设置时间则不执行  
320                     CronExpression cexp = new CronExpression(timeexp);  
321                     // ①-3:设置Cron表达式  
322                     cronTrigger.setCronExpression(cexp);  
323                     Scheduler scheduler = sf.getScheduler();  
324                     scheduler.scheduleJob(jobDetail, cronTrigger);  
325                     scheduler.start();  
326                 }  
327                   
328                   
329                 /*--------------------创建一个间隔一定时间执行的job-----------------------*/  
330                 String jobName = "fundsrecord_jobname_folkholdersId2";  
331                 String jobGroupName = "fundsrecord_jobgroupname_folkholdersId2";  
332                 String triggerName = "fundsrecord_triggername_folkholdersId2";  
333                 String triggerGroupName = "fundsrecord_triggergroupname_folkholdersId2";  
334                 Date beginTime = new Date(System.currentTimeMillis() + 60000L);//当前时间过60秒钟后开始  
335                 Integer repeatInterval = 5000;//以毫秒为单位  
336                 Integer repeatCount = 0;  
337                 HashMap<String, Object> map = new HashMap<String,Object>();  
338                 map.put("folkholdersId", "2");  
339                 try {  
340                     CronTriggerRunner.addJob(jobName,jobGroupName,triggerName,  
341                             triggerGroupName,beginTime,repeatInterval,  
342                             repeatCount,new FundsrecordJob(),map);  
343                 } catch (SchedulerException e) {  
344                     // TODO Auto-generated catch block  
345                     e.printStackTrace();  
346                 }  
347            } catch (Exception e) {  
348                e.printStackTrace();  
349            }  
350         }  
351           
352           
353     }  



    package com.lec.util.job;  
      
    import java.util.HashMap;  
      
    import org.quartz.InterruptableJob;  
    import org.quartz.JobDataMap;  
    import org.quartz.JobExecutionContext;  
    import org.quartz.JobExecutionException;  
    import org.quartz.SchedulerException;  
    import org.quartz.UnableToInterruptJobException;  
      
    /** 
     * 任务调度器 
     * @author lcy 
     *  May 24, 2011 
     */  
    public class FundsrecordJob implements InterruptableJob {  
      
        @Override  
        public void execute(JobExecutionContext context) throws JobExecutionException {  
            JobDataMap map = context.getMergedJobDataMap();  
            Object obj = map.get("params");  
            if(obj != null ){  
                HashMap<String,Object> params = (HashMap<String,Object>)obj;  
                dojob();//在这个任务调度器里面需要执行的方法  
                Long folkholdersId = Long.parseLong((String)params.get("folkholdersId"));  
                try {  
                    CronTriggerRunner.stopJob("fundsrecord_jobname_folkholdersId"+folkholdersId,  
                            "fundsrecord_jobgroupname_folkholdersId"+folkholdersId);//停止本次job  
                } catch (SchedulerException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                  
            }  
        }  
        public void interrupt() throws UnableToInterruptJobException {  
              
        }  
      
    }  

 

posted @ 2014-12-25 09:53  闻鸡睡觉  阅读(1296)  评论(0编辑  收藏  举报