Job管理器

原创
2015/09/22 11:05
阅读数 98

Job管理器是基于数据库管理Quartz Job任务,更方便管理Job的生命周期(Job管理器使用)

Quartz管理器主要特点:

  •  支持任务的启用、停用

  •  支持MySql、Oracle、DB2等多中数据库

  •  灵活便利的管理quartz任务

  •  支持多主机任务

  • 支持任务参数的传递

  • 一张表管理任务,简单便捷

  • 架构图如下所示:

         序列图如下所示:

   

 Quartz管理器表结构

    新增一条任务

同步任务代码

/**
	 * 扫描更新任务
	 */
	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();
			}
		}
	}

}

展开阅读全文
打赏
0
1 收藏
分享
加载中
吊!支持!学习了!
2015/09/25 10:12
回复
举报
更多评论
打赏
1 评论
1 收藏
0
分享
返回顶部
顶部