Quartz定时任务工具类

一、前言

为了方便定时任务的使用,本文提供一个定时任务的工具类,从而使用方便,随用随取

二、使用步骤

1、引入相关pom依赖

    
    
        org.quartz-scheduler
        quartz
        2.3.2
    

本工具类使用的其他pom

    
    
        commons-collections
        commons-collections
        3.2.2
    

2、引入定时任务工具类

import org.apache.commons.collections.MapUtils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Map;


public class QuartzManager {

	/**
	 * 任务调度管理单例对象
	 */
	private static QuartzManager manager = new QuartzManager();

	/**
	 * 任务调度
	 */
	private Scheduler scheduler;

	private QuartzManager() {
		try {
			scheduler = new StdSchedulerFactory().getScheduler();
		} catch (SchedulerException e) {
			throw new RuntimeException("构造调度任务异常...", e);
		}
	}

	/**
	 * 获取实例方法
	 * 
	 * @return QuartzManager;
	 */
	public static QuartzManager getInstance() {
		return manager;
	}

	/**
	 * 添加任务
	 * 
	 * @param name 任务名
	 * @param group 任务组
	 * @param clazz 任务class
	 * @param cronExpression 克隆表达式
	 * @param jobDatalMap 任务附加参数
	 * @throws SchedulerException 添加任务失败抛出该异常
	 */
	public void addJob(String name, String group, Class clazz, String cronExpression,
					   Map jobDatalMap) throws SchedulerException {
		JobDetail job = JobBuilder.newJob(clazz).withIdentity(name, group).storeDurably().build();
		if (MapUtils.isNotEmpty(jobDatalMap))
			job.getJobDataMap().putAll(jobDatalMap);

		Trigger trg = TriggerBuilder.newTrigger().withIdentity(name, group)
				.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();

		scheduler.scheduleJob(job, trg);
	}

	/**
	 *
	 *  更新jobDataMap
	 * 
	 * @throws SchedulerException
	 */
	public void updateJobDataMap(String name, String group, Map jobDatalMap) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(name, group);
		JobDetail jobDetail = scheduler.getJobDetail(jobKey);
		jobDetail.getJobDataMap().putAll(jobDatalMap);
		scheduler.addJob(jobDetail, true);
	}

	/**
	 * 删除任务
	 * 
	 * @param name
	 *            任务名
	 * @param group
	 *            任务组
	 * @throws SchedulerException
	 *             添加任务失败抛出该异常
	 */
	public void removeJob(String name, String group) throws SchedulerException {
		TriggerKey tk = TriggerKey.triggerKey(name, group);
		scheduler.pauseTrigger(tk);
		scheduler.unscheduleJob(tk);
		JobKey jobKey = JobKey.jobKey(name, group);
		scheduler.deleteJob(jobKey);
	}

	/**
	 * 暂停任务
	 * 
	 * @param name
	 *            任务名
	 * @param group
	 *            任务组
	 * @throws SchedulerException
	 *             添加任务失败抛出该异常
	 */
	public void pauseJob(String name, String group) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(name, group);
		scheduler.pauseJob(jobKey);
	}

	/**
	 * 恢复任务
	 * 
	 * @param name
	 *            任务名
	 * @param group
	 *            任务组
	 * @throws SchedulerException
	 *             添加任务失败抛出该异常
	 */
	public void resumeJob(String name, String group) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(name, group);
		scheduler.resumeJob(jobKey);
	}

	/**
	 * 修改任务触发时间
	 * 
	 * @param name
	 *            任务名
	 * @param group
	 *            任务组
	 * @throws SchedulerException
	 *             添加任务失败抛出该异常
	 */
	public void modifyTime(String name, String group, String cronExpression) throws SchedulerException {
		TriggerKey tk = TriggerKey.triggerKey(name, group);
		Trigger trg = TriggerBuilder.newTrigger().withIdentity(name, group)
				.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).build();
		scheduler.rescheduleJob(tk, trg);
	}

	/**
	 * 启动任务
	 */
	public void start() throws SchedulerException {
		if (!scheduler.isStarted())
			scheduler.start();
	}

	/**
	 * 停止任务
	 */
	public void shutdown() throws SchedulerException {
		if (!scheduler.isShutdown())
			scheduler.shutdown();
	}

	/**
	 * 获取任务上下文
	 * 
	 * @return SchedulerContext
	 * @throws SchedulerException
	 *             获取上下文失败抛出异常
	 */
	public SchedulerContext getContext() throws SchedulerException {
		return scheduler.getContext();
	}
}

3、定义任务执行体(实现Job接口),例如定时打印时间:

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Date;

public class JobContext implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.err.println("当前时间:"+new Date());
    }
}

4、触发定时任务,每秒钟执行一次

import org.quartz.SchedulerException;

public class BuildQuartz {
    public static void main(String[] args) throws SchedulerException {
        QuartzManager manager = QuartzManager.getInstance();
        manager.addJob("jobName","jobGroup", JobContext.class,"* * * * * ? *",null);
        manager.start();
    }
}

愿对你有帮助!

你可能感兴趣的:(java)