/** * 任务调度类 * @author Joyce.Luo * @date 2015-3-31 下午03:32:04 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ public class QuartzJob { /** * 任务ID */ private Integer jobId; /** * 任务名称 */ private String jobName; /** * 任务分组 */ private String jobGroup; /** * 任务状态 0禁用 1启用 2删除 */ private Integer jobStatus; /** * 任务执行时间表达式 */ private String cronExpression; /** * @return the jobId */ public Integer getJobId() { return jobId; } /** * @param jobId * the jobId to set */ public void setJobId(Integer jobId) { this.jobId = jobId; } /** * @return the jobName */ public String getJobName() { return jobName; } /** * @param jobName * the jobName to set */ public void setJobName(String jobName) { this.jobName = jobName; } /** * @return the jobGroup */ public String getJobGroup() { return jobGroup; } /** * @param jobGroup * the jobGroup to set */ public void setJobGroup(String jobGroup) { this.jobGroup = jobGroup; } /** * @return the jobStatus */ public Integer getJobStatus() { return jobStatus; } /** * @param jobStatus * the jobStatus to set */ public void setJobStatus(Integer jobStatus) { this.jobStatus = jobStatus; } /** * @return the cronExpression */ public String getCronExpression() { return cronExpression; } /** * @param cronExpression * the cronExpression to set */ public void setCronExpression(String cronExpression) { this.cronExpression = cronExpression; } }
import org.apache.log4j.Logger; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import com.hupu.nac.biz.IAuthPolicyClientBiz; /** * client长连接断开任务调度工厂类 * @author Joyce.Luo * @date 2015-3-31 下午03:38:35 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ public class LinkDisconnectQuartzJobFactory implements Job { private static final Logger logger = Logger.getLogger(LinkDisconnectQuartzJobFactory.class); @Override public void execute(JobExecutionContext context) throws JobExecutionException { QuartzJob scheduleJob = (QuartzJob)context.getMergedJobDataMap().get("linkDisconnectJob"); logger.info("client长连接断开定时任务開始运行,任务名称[" + scheduleJob.getJobName() + "]"); } }
import org.apache.log4j.Logger; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; import org.quartz.impl.StdScheduler; /** * client长连接断开任务调度管理类 * @author Joyce.Luo * @date 2015-3-31 下午03:42:30 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ public class LinkDisconnectQuartzManager { private static final Logger logger = Logger.getLogger(LinkDisconnectQuartzManager.class); private StdScheduler scheduler; /** * @param scheduler the scheduler to set */ public void setScheduler(StdScheduler scheduler) { this.scheduler = scheduler; } /** * 初始化任务调度 * @author Joyce.Luo * @date 2015-3-31 下午03:48:55 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ public void initJob(){ logger.info("初始化client长连接断开任务调度"); QuartzJob job = new QuartzJob(); job.setJobName("link_disconnect_job"); job.setJobGroup("link_disconnect_group"); job.setJobStatus(1); job.setCronExpression("0 0/1 * * * ?"); try { TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup()); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey); if (null == trigger) { addQuartzJob(job, trigger); } } catch (Exception e) { logger.error("初始化client长连接断开任务调度异常!" + e.getMessage(), e); } } /** * 向任务调度中加入定时任务 * @param job 定时任务信息 * @param trigger 定时调度触发器 * @author Joyce.Luo * @date 2015-3-31 下午04:04:58 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ private void addQuartzJob(QuartzJob job, CronTrigger trigger){ logger.info("向任务调度中加入定时任务"); try { JobDetail jobDetail = JobBuilder.newJob(LinkDisconnectQuartzJobFactory.class) .withIdentity(job.getJobName(), job.getJobGroup()).build(); jobDetail.getJobDataMap().put("linkDisconnectJob", job); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()) .withSchedule(scheduleBuilder).build(); scheduler.scheduleJob(jobDetail, trigger); } catch (Exception e) { logger.error("向任务调度中加入定时任务异常!" + e.getMessage(), e); } } /** * 马上执行定时任务 * @param job 定时任务信息 * @author Joyce.Luo * @date 2015-4-20 下午02:08:41 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ void runJob(QuartzJob job){ logger.info("马上执行任务调度中的定时任务"); try { if (null == job) { logger.info("定时任务信息为空,无法马上执行"); return; } JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup()); if(null == jobKey){ logger.info("任务调度中不存在[" + job.getJobName() + "]定时任务,不予马上执行!"); return; } scheduler.triggerJob(jobKey); } catch (Exception e) { logger.error("马上执行任务调度中的定时任务异常!" + e.getMessage(), e); } } /** * 改动任务调度中的定时任务 * @param job 定时任务信息 * @param triggerKey 定时调度触发键 * @param trigger 定时调度触发器 * @author Joyce.Luo * @date 2015-3-31 下午04:16:54 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ void updateQuartzJob(QuartzJob job, TriggerKey triggerKey, CronTrigger trigger){ logger.info("改动任务调度中的定时任务"); try { if (null == job || null == triggerKey || null == trigger) { logger.info("改动调度任务參数不正常!"); return; } logger.info("原始任务表达式:" + trigger.getCronExpression() + "。如今任务表达式:" + job.getCronExpression()); if (trigger.getCronExpression().equals(job.getCronExpression())) { logger.info("任务调度表达式一致,不予进行改动!
"); return; } logger.info("任务调度表达式不一致,进行改动"); CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression()); trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build(); scheduler.rescheduleJob(triggerKey, trigger); } catch (Exception e) { logger.error("改动任务调度中的定时任务异常!
" + e.getMessage(), e); } } /** * 暂停任务调度中的定时任务 * @param job 定时任务信息 * @author Joyce.Luo * @date 2015-4-20 下午02:22:53 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ void pauseJob(QuartzJob job){ logger.info("暂停任务调度中的定时任务"); try { if (null == job) { logger.info("暂停调度任务參数不正常!
"); return; } JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup()); if(null == jobKey){ logger.info("任务调度中不存在[" + job.getJobName() + "]定时任务。不予进行暂停!"); return; } scheduler.pauseJob(jobKey); } catch (Exception e) { logger.error("暂停任务调度中的定时任务异常!" + e.getMessage(), e); } } /** * 恢复任务调度中的定时任务 * @param job 定时任务信息 * @author Joyce.Luo * @date 2015-4-20 下午02:26:08 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ void resumeJob(QuartzJob job){ logger.info("恢复任务调度中的定时任务"); try { if (null == job) { logger.info("恢复调度任务參数不正常!
"); return; } JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup()); if(null == jobKey){ logger.info("任务调度中不存在[" + job.getJobName() + "]定时任务。不予进行恢复。"); return; } scheduler.resumeJob(jobKey); } catch (Exception e) { logger.error("恢复任务调度中的定时任务异常!" + e.getMessage(), e); } } /** * 删除任务调度中的定时任务 * @param job 定时任务信息 * @author Joyce.Luo * @date 2015-3-31 下午04:30:03 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ void deleteJob(QuartzJob job){ logger.info("删除任务调度中的定时任务"); try { if (null == job) { logger.info("删除调度任务參数不正常!"); return; } JobKey jobKey = JobKey.jobKey(job.getJobName(), job.getJobGroup()); if(null == jobKey){ logger.info("任务调度中不存在[" + job.getJobName() + "]定时任务,不予进行删除!
"); return; } scheduler.deleteJob(jobKey); } catch (Exception e) { logger.error("删除任务调度中的定时任务异常。" + e.getMessage(), e); } } /** * 删除任务调度定时器 * @param triggerKey * @author Joyce.Luo * @date 2015-3-31 下午04:35:33 * @version V3.0 * @since Tomcat6.0,Jdk1.6 * @copyright Copyright (c) 2015 */ void deleteJob(TriggerKey triggerKey){ logger.info("删除任务调度定时器"); try { if(null == triggerKey){ logger.info("停止任务定时器參数不正常。不予进行停止!
"); return; } logger.info("停止任务定时器"); scheduler.pauseTrigger(triggerKey); scheduler.unscheduleJob(triggerKey); } catch (Exception e) { logger.info("删除任务调度定时器异常!
" + e.getMessage() ,e); } } }
<bean id="linkDisconnectScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" /> <bean id="linkDisconnectQuartzManager" class="com.hupu.nac.timertask.LinkDisconnectQuartzManager" init-method="initJob"> <property name="scheduler" ref="linkDisconnectScheduler"></property> </bean>