Quartz任务执行
public class JobScheduler { private Scheduler scheduler; private static final String GROUP = "_default-group_"; public JobScheduler() throws Exception{ scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); } public boolean schedule(String key,String cronExpression,JobExecutor executor){ try { CronScheduleBuilder sb = CronScheduleBuilder.cronSchedule(cronExpression); Trigger trigger = TriggerBuilder.newTrigger().withIdentity(key, GROUP).withSchedule(sb).build(); //如果任务已经在执行,则返回 if(scheduler.checkExists(trigger.getKey())){ return true; } Class<JobProxy> jobClass = JobProxy.class; JobDetail job = JobBuilder.newJob(jobClass).withIdentity(key, GROUP).build(); job.getJobDataMap().put(Utils.WORKER_ID, key); //for get job.getJobDataMap().put(Utils.WORKER_CALLER, executor); //for get scheduler.scheduleJob(job, trigger); // 任务调度列表 return true; } catch (Exception e) { e.printStackTrace(); } return false; } public boolean unschedule(String key){ try { TriggerKey tk = new TriggerKey(key, GROUP); if (scheduler.checkExists(tk)) { scheduler.unscheduleJob(tk); } return true; } catch (Exception e) { e.printStackTrace(); } return false; }}
? ? 并没有特别神秘的地方,只是借助了dataMap来保存JobDeTetail的实例.?
?