Job管理器是基於數據庫管理Quartz Job任務,更方便管理Job的生命週期(Job管理器使用) java
Quartz管理器主要特色:
-
支持任務的啓用、停用 數據庫
-
支持MySql、Oracle、DB2等多中數據庫 架構
-
靈活便利的管理quartz任務 ide
-
支持多主機任務 工具
-
支持任務參數的傳遞 ui
-
一張表管理任務,簡單便捷 this
-
架構圖以下所示: spa
序列圖以下所示: .net
Quartz管理器表結構 code
新增一條任務
同步任務代碼
/** * 掃描更新任務 */ public synchronized IDataset<IData<String, Object>> getUpdateJob( QuartzManage quartzManageFactory) throws Exception { IDataset<IData<String, Object>> list = getQueryTask(getQueryTask()); // 掃描表是否有刪除的做業 IDataset<IData<String, Object>> isJobDelete = isJobDelete(list); // 掃描表是否有更新的做業 IDataset<IData<String, Object>> isJobUpdate = isJobUpdate(list); // 掃描表是否有新增的做業 IDataset<IData<String, Object>> isJobAdd = isJobAdd(list); // 掃描表有刪除移除任務 if (isJobDelete.size() > 0) { for (IData<String, Object> data : isJobDelete) { if (!data.getString(JOB_CLASS).equals(TASK_JOB)) { quartzManageFactory.deleteJob(data.getString(JOB_ID), data.getString(JOB_GROUP)); } } } // 掃描表有修改 先刪除 在添加 if (isJobUpdate.size() > 0) { for (IData<String, Object> data : isJobUpdate) { if (!data.getString(JOB_CLASS).equals(TASK_JOB)) { quartzManageFactory.deleteJob(data.getString(JOB_ID), data.getString(JOB_GROUP)); // 做業開始時間 Date startTime = DateUtils.strToDateLong(data .getString(JOB_START)); // 做業結束時間 Date endTime = DateUtils.strToDateLong(data .getString(JOB_END)); // 加入調度 quartzManageFactory.addJob(data.getString(JOB_ID), data.getString(JOB_GROUP), getParameters(data.getString(JOB_PARAMETERS)), data.getString(JOB_TIME), data.getString(JOB_CLASS), startTime, endTime); } } } // 掃描表新增添加任務 if (isJobAdd.size() > 0) { for (IData<String, Object> data : isJobAdd) { if (!data.getString(JOB_CLASS).equals(TASK_JOB)) { // 做業開始時間 Date startTime = DateUtils.strToDateLong(data .getString(JOB_START)); // 做業結束時間 Date endTime = DateUtils.strToDateLong(data .getString(JOB_END)); // 加入調度 quartzManageFactory.addJob(data.getString(JOB_ID), data.getString(JOB_GROUP), getParameters(data.getString(JOB_PARAMETERS)), data.getString(JOB_TIME), data.getString(JOB_CLASS), startTime, endTime); } } } setTaskMemory(list); return list; }
Quartz任務管理器
package com.venustech.quartz; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; import java.util.Date; import java.util.Map; import java.util.Map.Entry; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobBuilder; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.TriggerKey; import org.quartz.impl.StdSchedulerFactory; /** * * @ClassName: QuartzManageImpl * @Description: TODO(job 工具類) * @author wangshuaiJAVA * @date 2014年12月29日 上午10:58:29 * */ public class QuartzManageImpl implements QuartzManage { private Scheduler scheduler; private String jobPath; private final static int QUARTZ_HOURS_TYPE = 3; private final static int QUARTZ_SECONDS_TYPE = 1; private final static int QUARTZ_MINUTES_TYPE = 2; public QuartzManageImpl(String jobPath) throws SchedulerException { scheduler = new StdSchedulerFactory(jobPath).getScheduler(); } public QuartzManageImpl() { super(); } public void addJob(String jobName, String group, Map<String, Object> parameters, int intervalTime, int count, int type, Date startDate, String jobClassName) throws Exception { Trigger trigger = null; if (type == QuartzManageImpl.QUARTZ_SECONDS_TYPE) { trigger = newTrigger() .startAt(startDate) .withIdentity(jobName, group) .withSchedule( simpleSchedule() .withIntervalInSeconds(intervalTime) .withRepeatCount(count)).build(); } else if (type == QuartzManageImpl.QUARTZ_MINUTES_TYPE) { trigger = newTrigger() .startAt(startDate) .withIdentity(jobName, group) .withSchedule( simpleSchedule() .withIntervalInMinutes(intervalTime) .withRepeatCount(count)).build(); } else if (type == QuartzManageImpl.QUARTZ_HOURS_TYPE) { trigger = newTrigger() .startAt(startDate) .withIdentity(jobName, group) .withSchedule( simpleSchedule().withIntervalInHours(intervalTime) .withRepeatCount(count)).build(); } JobDetail job = JobBuilder .newJob((Class<? extends Job>) Class.forName(jobClassName)) .withIdentity(jobName, group).build(); JobDataMap map = job.getJobDataMap(); for (Entry<String, Object> entry : parameters.entrySet()) { map.put(entry.getKey(), entry.getValue()); } scheduler.scheduleJob(job, trigger); } @Override public void addJob(String jobName, String group, int intervalTime, int count, int type, Date startDate) { } @Override public void addJob(String jobName, String group, int intervalTime, int count, int type) { // TODO Auto-generated method stub } @SuppressWarnings("unchecked") public void addJob(String jobName, String group, Map<String, Object> parameters, String time, String jobClassName, Date startTime, Date endTime) throws Exception { CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity(jobName, group) .withSchedule(CronScheduleBuilder.cronSchedule(time)) .startAt(startTime).endAt(endTime).build(); JobDetail job = JobBuilder .newJob((Class<? extends Job>) Class.forName(jobClassName)) .withIdentity(jobName, group).build(); JobDataMap map = job.getJobDataMap(); for (Entry<String, Object> entry : parameters.entrySet()) { map.put(entry.getKey(), entry.getValue()); } scheduler.scheduleJob(job, cronTrigger); } @Override public boolean deleteJob(String jobName, String group) throws Exception { return scheduler.deleteJob(JobKey.jobKey(jobName, group)); } @Override public boolean deleteJob(String jobName) throws Exception { return scheduler.deleteJob(JobKey.jobKey(jobName)); } public Date upadteJobTime(String jobName, String group, String time) throws Exception { TriggerKey triggerKey = TriggerKey.triggerKey(jobName, group); CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity(jobName, group) .withSchedule(CronScheduleBuilder.cronSchedule(time)).build(); return scheduler.rescheduleJob(triggerKey, cronTrigger); } @Override public void pauseJob(String jobName, String group) throws Exception { scheduler.pauseJob(JobKey.jobKey(jobName, group)); } @Override public void pauseJob(String jobName) throws Exception { scheduler.pauseJob(JobKey.jobKey(jobName)); } @Override public void resumeJob(String jobName, String group) throws Exception { scheduler.resumeJob(JobKey.jobKey(jobName, group)); } @Override public void resumeJob(String jobName) throws Exception { scheduler.resumeJob(JobKey.jobKey(jobName)); } public String getJobPath() { return jobPath; } public void setJobPath(String jobPath) { this.jobPath = jobPath; } public Scheduler getScheduler() { return scheduler; } public void setScheduler(Scheduler scheduler) { this.scheduler = scheduler; } @Override public void initJob() { try { scheduler = new StdSchedulerFactory(jobPath).getScheduler(); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void stopJob() { if (scheduler != null) { try { scheduler.shutdown(); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void startjob() { if (scheduler != null) { try { scheduler.start(); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }