java定时任务Qeartz

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

[BAT][JAVA]定时任务之-Quartz使用篇分类:Java专区2011-01-2216:384595人阅读评论(9)收藏举报定时任务之-Quartz使用篇Quartz是OpenSymphony开源组织在Jobscheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或EJBs。官方网站:相关Jar:quartz-all-1.6.0.jarjta.jarcommons-logging-1.1.jarcommons-collections3.2.jar经过封装的管理类:[java]viewplaincopy/***版权所有:华信软件*项目名称:公用模块*创建者:Wangdf*创建日期:2011-1-22*文件说明:定时任务管理类*最近修改者:Wangdf*最近修改日期:2011-1-22*/packagecom.extjweb.quartz;importjava.text.ParseException;importorg.quartz.CronTrigger;importorg.quartz.JobDetail;importorg.quartz.Scheduler;importorg.quartz.SchedulerException;importorg.quartz.SchedulerFactory;importorg.quartz.impl.StdSchedulerFactory;/***定时任务管理类**@author王德封*/publicclassQuartzManager{privatestaticSchedulerFactorygSchedulerFactory=newStdSchedulerFactory();privatestaticStringJOB_GROUP_NAME=EXTJWEB_JOBGROUP_NAME;privatestaticStringTRIGGER_GROUP_NAME=EXTJWEB_TRIGGERGROUP_NAME;/***添加一个定时任务,使用默认的任务组名,触发器名,触发器组名**@paramjobName*任务名*@paramjobClass*任务*@paramtime*时间设置,参考quartz说明文档*@throwsSchedulerException*@throwsParseException*/publicstaticvoidaddJob(StringjobName,StringjobClass,Stringtime){try{Schedulersched=gSchedulerFactory.getScheduler();JobDetailjobDetail=newJobDetail(jobName,JOB_GROUP_NAME,Class.forName(jobClass));//任务名,任务组,任务执行类//触发器CronTriggertrigger=newCronTrigger(jobName,TRIGGER_GROUP_NAME);//触发器名,触发器组trigger.setCronExpression(time);//触发器时间设定sched.scheduleJob(jobDetail,trigger);//启动if(!sched.isShutdown()){sched.start();}}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}/***添加一个定时任务**@paramjobName*任务名*@paramjobGroupName*任务组名*@paramtriggerName*触发器名*@paramtriggerGroupName*触发器组名*@paramjobClass*任务*@paramtime*时间设置,参考quartz说明文档*@throwsSchedulerException*@throwsParseException*/publicstaticvoidaddJob(StringjobName,StringjobGroupName,StringtriggerName,StringtriggerGroupName,StringjobClass,Stringtime){try{Schedulersched=gSchedulerFactory.getScheduler();JobDetailjobDetail=newJobDetail(jobName,jobGroupName,Class.forName(jobClass));//任务名,任务组,任务执行类//触发器CronTriggertrigger=newCronTrigger(triggerName,triggerGroupName);//触发器名,触发器组trigger.setCronExpression(time);//触发器时间设定sched.scheduleJob(jobDetail,trigger);}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}/***修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)**@paramjobName*@paramtime*/publicstaticvoidmodifyJobTime(StringjobName,Stringtime){try{Schedulersched=gSchedulerFactory.getScheduler();CronTriggertrigger=(CronTrigger)sched.getTrigger(jobName,TRIGGER_GROUP_NAME);if(trigger==null){return;}StringoldTime=trigger.getCronExpression();if(!oldTime.equalsIgnoreCase(time)){JobDetailjobDetail=sched.getJobDetail(jobName,JOB_GROUP_NAME);ClassobjJobClass=jobDetail.getJobClass();StringjobClass=objJobClass.getName();removeJob(jobName);addJob(jobName,jobClass,time);}}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}/***修改一个任务的触发时间**@paramtriggerName*@paramtriggerGroupName*@paramtime*/publicstaticvoidmodifyJobTime(StringtriggerName,StringtriggerGroupName,Stringtime){try{Schedulersched=gSchedulerFactory.getScheduler();CronTriggertrigger=(CronTrigger)sched.getTrigger(triggerName,triggerGroupName);if(trigger==null){return;}StringoldTime=trigger.getCronExpression();if(!oldTime.equalsIgnoreCase(time)){CronTriggerct=(CronTrigger)trigger;//修改时间ct.setCronExpression(time);//重启触发器sched.resumeTrigger(triggerName,triggerGroupName);}}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}/***移除一个任务(使用默认的任务组名,触发器名,触发器组名)**@paramjobName*/publicstaticvoidremoveJob(StringjobName){try{Schedulersched=gSchedulerFactory.getScheduler();sched.pauseTrigger(jobName,TRIGGER_GROUP_NAME);//停止触发器sched.unscheduleJob(jobName,TRIGGER_GROUP_NAME);//移除触发器sched.deleteJob(jobName,JOB_GROUP_NAME);//删除任务}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}/***移除一个任务**@paramjobName*@paramjobGroupName*@paramtriggerName*@paramtriggerGroupName*/publicstaticvoidremoveJob(StringjobName,StringjobGroupName,StringtriggerName,StringtriggerGroupName){try{Schedulersched=gSchedulerFactory.getScheduler();sched.pauseTrigger(triggerName,triggerGroupName);//停止触发器sched.unscheduleJob(triggerName,triggerGroupName);//移除触发器sched.deleteJob(jobName,jobGroupName);//删除任务}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}/***启动所有定时任务*/publicstaticvoidstartJobs(){try{Schedulersched=gSchedulerFactory.getScheduler();sched.start();}catch(Exceptione){e.printStackTrace();thrownewRuntimeException(e);}}/***关

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功