本文主要是介绍Quartz 仿SqlServer 作业管理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、效果图
列表为自定义作业类,cron表达式定义任务执行计划,操作分别为开启执行计划,立即执行一次。
2、关键代码
/*** 立即执行一次任务* @param job*/public void runJobImmediately(ScheduleJob job) throws SchedulerException{JobKey jobKey = new JobKey(job.getJobKey(), job.getJobGroup());JobDetail jobDetail = scheduler.getJobDetail(jobKey);if(jobDetail !=null){scheduler.triggerJob(jobKey);}else{jobDetail = JobBuilder.newJob(QuartzJobAgent.class).storeDurably(true).withIdentity(job.getJobKey(), job.getJobGroup()).build();jobDetail.getJobDataMap().put("scheduleJob", job);scheduler.addJob(jobDetail, true);scheduler.triggerJob(jobDetail.getKey());}}
/*** 添加一个任务* @param job*/public void addJob(ScheduleJob job){try {TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobKey(), job.getJobGroup());CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);// 不存在,创建一个if (null == trigger) {/*JobDetail jobDetail = JobBuilder.newJob(QuartzJobAgent.class).withIdentity(job.getJobKey(), job.getJobGroup()).build();jobDetail.getJobDataMap().put("scheduleJob", job);*/JobKey jobKey = new JobKey(job.getJobKey(), job.getJobGroup());JobDetail jobDetail = scheduler.getJobDetail(jobKey);if(jobDetail == null){jobDetail = JobBuilder.newJob(QuartzJobAgent.class).storeDurably(true).withIdentity(job.getJobKey(), job.getJobGroup()).build();jobDetail.getJobDataMap().put("scheduleJob", job);}// 表达式调度构建器CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());// 按新的cronExpression表达式构建一个新的trigger 此处withIdentity 不是JOB的key,只是方便管理用同样的名称和组,// forJob: 当JOB已经保存,此时再进行scheduleJob(jobDetail, trigger)会报JOB已经存在异常,要想只保存trigger,先关联JOBDETAIL调用forJob方法//trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobKey(), job.getJobGroup()).withSchedule(scheduleBuilder).build();trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobKey(), job.getJobGroup()).forJob(jobDetail).withSchedule(scheduleBuilder).build();//scheduler.scheduleJob(jobDetail, trigger);scheduler.scheduleJob(trigger);} else {// Trigger已存在,那么更新相应的定时设置// 表达式调度构建器CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());// 按新的cronExpression表达式重新构建triggertrigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();// 按新的trigger重新设置job执行scheduler.rescheduleJob(triggerKey, trigger);}if(job.getJobStatus().equals("1")){pauseJob(job);}} catch (SchedulerException e){e.printStackTrace();}}
/*** 删除一个任务* @param scheduleJob*/public void removeJob(ScheduleJob job){logger.info(job.getJobName(),"删除定时任务");TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobKey(), job.getJobGroup());JobKey jobKey = JobKey.jobKey(job.getJobKey(),job.getJobGroup());//CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);try {scheduler.pauseTrigger(triggerKey);scheduler.unscheduleJob(triggerKey);scheduler.deleteJob(jobKey);} catch(SchedulerException e){e.printStackTrace();}}
这篇关于Quartz 仿SqlServer 作业管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!