首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Quartz任务施行

2013-10-10 
Quartz任务执行public class JobScheduler {private Scheduler schedulerprivate static final String GR

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的实例.?

?

热点排行