Spring基于Scheduler的定时任务模块

  • 第一部分

基于批量的定时任务,数据库表结构展示:

CREATE TABLE `sys_task` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cron_expression` varchar(255) DEFAULT NULL COMMENT 'cron表达式',
  `method_name` varchar(255) DEFAULT NULL COMMENT '任务调用的方法名',
  `is_concurrent` varchar(255) DEFAULT NULL COMMENT '任务是否有状态',
  `description` varchar(255) DEFAULT NULL COMMENT '任务描述',
  `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
  `bean_class` varchar(255) DEFAULT NULL COMMENT '任务执行时调用哪个类的方法 包名+类名',
  `create_date` datetime DEFAULT NULL COMMENT '创建时间',
  `job_status` varchar(255) DEFAULT NULL COMMENT '任务状态',
  `job_group` varchar(255) DEFAULT NULL COMMENT '任务分组',
  `update_date` datetime DEFAULT NULL COMMENT '更新时间',
  `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
  `spring_bean` varchar(255) DEFAULT NULL COMMENT 'Spring bean',
  `job_name` varchar(255) DEFAULT NULL COMMENT '任务名',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

实体类 

public class TaskDO implements Serializable {
    private static final long serialVersionUID = 1L;

    //
    private Long id;
    // cron表达式
    private String cronExpression;
    // 任务调用的方法名
    private String methodName;
    // 任务是否有状态
    private String isConcurrent;
    // 任务描述
    private String description;
    // 更新者
    private String updateBy;
    // 任务执行时调用哪个类的方法 包名+类名
    private String beanClass;
    // 创建时间
    private Date createDate;
    // 任务状态
    private String jobStatus;
    // 任务分组
    private String jobGroup;
    // 更新时间
    private Date updateDate;
    // 创建者
    private String createBy;
    // Spring bean
    private String springBean;
    // 任务名
    private String jobName;

    set.get
    ......
}

 mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.****.****.dao.TaskDao">

	<select id="get" resultType="com.****.****.domain.TaskDO">
		select `id`,`cron_expression`,`method_name`,`is_concurrent`,`description`,`update_by`,`bean_class`,`create_date`,`job_status`,`job_group`,`update_date`,`create_by`,`spring_bean`,`job_name` from sys_task where id = #{value}
	</select>

	<select id="list" resultType="com.****.****.domain.TaskDO">
		select `id`,`cron_expression`,`method_name`,`is_concurrent`,`description`,`update_by`,`bean_class`,`create_date`,`job_status`,`job_group`,`update_date`,`create_by`,`spring_bean`,`job_name` from sys_task
        <where>  
		  		  <if test="id != null and id != ''"> and id = #{id} </if>
		  		  <if test="cronExpression != null and cronExpression != ''"> and cron_expression = #{cronExpression} </if>
		  		  <if test="methodName != null and methodName != ''"> and method_name = #{methodName} </if>
		  		  <if test="isConcurrent != null and isConcurrent != ''"> and is_concurrent = #{isConcurrent} </if>
		  		  <if test="description != null and description != ''"> and description = #{description} </if>
		  		  <if test="updateBy != null and updateBy != ''"> and update_by = #{updateBy} </if>
		  		  <if test="beanClass != null and beanClass != ''"> and bean_class = #{beanClass} </if>
		  		  <if test="createDate != null and createDate != ''"> and create_date = #{createDate} </if>
		  		  <if test="jobStatus != null and jobStatus != ''"> and job_status = #{jobStatus} </if>
		  		  <if test="jobGroup != null and jobGroup != ''"> and job_group = #{jobGroup} </if>
		  		  <if test="updateDate != null and updateDate != ''"> and update_date = #{updateDate} </if>
		  		  <if test="createBy != null and createBy != ''"> and create_by = #{createBy} </if>
		  		  <if test="springBean != null and springBean != ''"> and spring_bean = #{springBean} </if>
		  		  <if test="jobName != null and jobName != ''"> and job_name = #{jobName} </if>
		  		</where>
        <choose>
            <when test="sort != null and sort.trim() != ''">
                order by ${sort} ${order}
            </when>
			<otherwise>
                order by id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="count" resultType="int">
		select count(*) from sys_task
		 <where>  
		  		  <if test="id != null and id != ''"> and id = #{id} </if>
		  		  <if test="cronExpression != null and cronExpression != ''"> and cron_expression = #{cronExpression} </if>
		  		  <if test="methodName != null and methodName != ''"> and method_name = #{methodName} </if>
		  		  <if test="isConcurrent != null and isConcurrent != ''"> and is_concurrent = #{isConcurrent} </if>
		  		  <if test="description != null and description != ''"> and description = #{description} </if>
		  		  <if test="updateBy != null and updateBy != ''"> and update_by = #{updateBy} </if>
		  		  <if test="beanClass != null and beanClass != ''"> and bean_class = #{beanClass} </if>
		  		  <if test="createDate != null and createDate != ''"> and create_date = #{createDate} </if>
		  		  <if test="jobStatus != null and jobStatus != ''"> and job_status = #{jobStatus} </if>
		  		  <if test="jobGroup != null and jobGroup != ''"> and job_group = #{jobGroup} </if>
		  		  <if test="updateDate != null and updateDate != ''"> and update_date = #{updateDate} </if>
		  		  <if test="createBy != null and createBy != ''"> and create_by = #{createBy} </if>
		  		  <if test="springBean != null and springBean != ''"> and spring_bean = #{springBean} </if>
		  		  <if test="jobName != null and jobName != ''"> and job_name = #{jobName} </if>
		  		</where>
	</select>
	 
	<insert id="save" parameterType="com.****.****.domain.TaskDO" useGeneratedKeys="true" keyProperty="id">
		insert into sys_task
		(
			`cron_expression`, 
			`method_name`, 
			`is_concurrent`, 
			`description`, 
			`update_by`, 
			`bean_class`, 
			`create_date`, 
			`job_status`, 
			`job_group`, 
			`update_date`, 
			`create_by`, 
			`spring_bean`, 
			`job_name`
		)
		values
		(
			#{cronExpression}, 
			#{methodName}, 
			#{isConcurrent}, 
			#{description}, 
			#{updateBy}, 
			#{beanClass}, 
			#{createDate}, 
			#{jobStatus}, 
			#{jobGroup}, 
			#{updateDate}, 
			#{createBy}, 
			#{springBean}, 
			#{jobName}
		)
	</insert>
	 
	<update id="update" parameterType="com.****.****.domain.TaskDO">
		update sys_task 
		<set>
			<if test="cronExpression != null">`cron_expression` = #{cronExpression}, </if>
			<if test="methodName != null">`method_name` = #{methodName}, </if>
			<if test="isConcurrent != null">`is_concurrent` = #{isConcurrent}, </if>
			<if test="description != null">`description` = #{description}, </if>
			<if test="updateBy != null">`update_by` = #{updateBy}, </if>
			<if test="beanClass != null">`bean_class` = #{beanClass}, </if>
			<if test="createDate != null">`create_date` = #{createDate}, </if>
			<if test="jobStatus != null">`job_status` = #{jobStatus}, </if>
			<if test="jobGroup != null">`job_group` = #{jobGroup}, </if>
			<if test="updateDate != null">`update_date` = #{updateDate}, </if>
			<if test="createBy != null">`create_by` = #{createBy}, </if>
			<if test="springBean != null">`spring_bean` = #{springBean}, </if>
			<if test="jobName != null">`job_name` = #{jobName}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="remove">
		delete from sys_task where id = #{value}
	</delete>
	
	<delete id="batchRemove">
		delete from sys_task where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>
  • 第二部分

批量的基于定时器的任务,则需在项目启动的时候就要初始化任务池,接下来用SpringBoot为例讲解:

POM

<!-- quartz -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

监听

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.bootdo.common.quartz.utils.QuartzManager;
import com.bootdo.common.service.JobService;

@Component
@Order(value = 1)
public class ScheduleJobInitListener implements CommandLineRunner {

	@Autowired
	JobService scheduleJobService;

	@Autowired
	QuartzManager quartzManager;

	@Override
	public void run(String... arg0) throws Exception {
		try {
			scheduleJobService.initSchedule();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

注:这个SpringBoot是实现implements CommandLineRunner的run方法来进行初始化(如果用的是SpringMVC则需要配置文件中配置<mvc:interceptors>的bean。然后implements HandlerInterceptor 多余的不在赘述)

接下来贴Service,关于数据库操作的增删查改,尤其注意service的实现类,注入QuartzManager quartzManager,在增删查过数据库的同时将quartzManager的数据状态更新,增删查改的同步。

public interface JobService {
	
	TaskDO get(Long id);
	
	List<TaskDO> list(Map<String, Object> map);
	
	int count(Map<String, Object> map);
	
	int save(TaskDO taskScheduleJob);
	
	int update(TaskDO taskScheduleJob);
	
	int remove(Long id);
	
	int batchRemove(Long[] ids);

	void initSchedule() throws SchedulerException;

	void changeStatus(Long jobId, String cmd) throws SchedulerException;

	void updateCron(Long jobId) throws SchedulerException;
}
@Service
public class JobServiceImpl implements JobService {

	@Autowired
	private TaskDao taskScheduleJobMapper;

	@Autowired
	QuartzManager quartzManager;

	@Override
	public TaskDO get(Long id) {
		return taskScheduleJobMapper.get(id);
	}

	@Override
	public List<TaskDO> list(Map<String, Object> map) {
		return taskScheduleJobMapper.list(map);
	}

	@Override
	public int count(Map<String, Object> map) {
		return taskScheduleJobMapper.count(map);
	}

	@Override
	public int save(TaskDO taskScheduleJob) {
		return taskScheduleJobMapper.save(taskScheduleJob);
	}

	@Override
	public int update(TaskDO taskScheduleJob) {
		return taskScheduleJobMapper.update(taskScheduleJob);
	}

	@Override
	public int remove(Long id) {
		try {
			TaskDO scheduleJob = get(id);
			quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
			return taskScheduleJobMapper.remove(id);
		} catch (SchedulerException e) {
			e.printStackTrace();
			return 0;
		}

	}

	@Override
	public int batchRemove(Long[] ids) {
		for (Long id : ids) {
			try {
				TaskDO scheduleJob = get(id);
				quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
			} catch (SchedulerException e) {
				e.printStackTrace();
				return 0;
			}
		}
		return taskScheduleJobMapper.batchRemove(ids);
	}

	@Override
	public void initSchedule() throws SchedulerException {
		// 这里获取任务信息数据
		List<TaskDO> jobList = taskScheduleJobMapper.list(new HashMap<String, Object>(16));
		for (TaskDO scheduleJob : jobList) {
			if ("1".equals(scheduleJob.getJobStatus())) {
				ScheduleJob job = ScheduleJobUtils.entityToData(scheduleJob);
				quartzManager.addJob(job);
			}

		}
	}

	@Override
	public void changeStatus(Long jobId, String cmd) throws SchedulerException {
		TaskDO scheduleJob = get(jobId);
		if (scheduleJob == null) {
			return;
		}
		if (Constant.STATUS_RUNNING_STOP.equals(cmd)) {
			quartzManager.deleteJob(ScheduleJobUtils.entityToData(scheduleJob));
			scheduleJob.setJobStatus(ScheduleJob.STATUS_NOT_RUNNING);
		} else {
			if (!Constant.STATUS_RUNNING_START.equals(cmd)) {
			} else {
                scheduleJob.setJobStatus(ScheduleJob.STATUS_RUNNING);
                quartzManager.addJob(ScheduleJobUtils.entityToData(scheduleJob));
            }
		}
		update(scheduleJob);
	}

	@Override
	public void updateCron(Long jobId) throws SchedulerException {
		TaskDO scheduleJob = get(jobId);
		if (scheduleJob == null) {
			return;
		}
		if (ScheduleJob.STATUS_RUNNING.equals(scheduleJob.getJobStatus())) {
			quartzManager.updateJobCron(ScheduleJobUtils.entityToData(scheduleJob));
		}
		update(scheduleJob);
	}

}

在quartzManager中注入Scheduler,将jobDetail和trigger规整到scheduler中

Trigger trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())// 触发器key
					.startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
					.withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression())).startNow().build();
			// 把作业和触发器注册到任务调度中
			scheduler.scheduleJob(jobDetail, trigger);

贴一下 quartzManager

/**
 * 
 * 
 * @title: QuartzManager.java
 * @description: 计划任务管理
 *
 */
@Service
public class QuartzManager {
	public final Logger log = Logger.getLogger(this.getClass());
	// private SchedulerFactoryBean schedulerFactoryBean
	// =SpringContextHolder.getBean(SchedulerFactoryBean.class);
	// @Autowired
	// @Qualifier("schedulerFactoryBean")
	// private SchedulerFactoryBean schedulerFactoryBean;
	@Autowired
	private Scheduler scheduler;

	/**
	 * 添加任务
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	
	public void addJob(ScheduleJob job) {
		try {
			// 创建jobDetail实例,绑定Job实现类
			// 指明job的名称,所在组的名称,以及绑定job类

			Class<? extends Job> jobClass = (Class<? extends Job>) (Class.forName(job.getBeanClass()).newInstance()
					.getClass());
			JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(job.getJobName(), job.getJobGroup())// 任务名称和组构成任务key
					.build();
			// 定义调度触发规则
			// 使用cornTrigger规则
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup())// 触发器key
					.startAt(DateBuilder.futureDate(1, IntervalUnit.SECOND))
					.withSchedule(CronScheduleBuilder.cronSchedule(job.getCronExpression())).startNow().build();
			// 把作业和触发器注册到任务调度中
			scheduler.scheduleJob(jobDetail, trigger);
			// 启动
			if (!scheduler.isShutdown()) {
				scheduler.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取所有计划中的任务列表
	 * 
	 * @return
	 * @throws SchedulerException
	 */
	public List<ScheduleJob> getAllJob() throws SchedulerException {
		GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
		Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
		List<ScheduleJob> jobList = new ArrayList<ScheduleJob>();
		for (JobKey jobKey : jobKeys) {
			List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
			for (Trigger trigger : triggers) {
				ScheduleJob job = new ScheduleJob();
				job.setJobName(jobKey.getName());
				job.setJobGroup(jobKey.getGroup());
				job.setDescription("触发器:" + trigger.getKey());
				Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
				job.setJobStatus(triggerState.name());
				if (trigger instanceof CronTrigger) {
					CronTrigger cronTrigger = (CronTrigger) trigger;
					String cronExpression = cronTrigger.getCronExpression();
					job.setCronExpression(cronExpression);
				}
				jobList.add(job);
			}
		}
		return jobList;
	}

	/**
	 * 所有正在运行的job
	 * 
	 * @return
	 * @throws SchedulerException
	 */
	public List<ScheduleJob> getRunningJob() throws SchedulerException {
		List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
		List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(executingJobs.size());
		for (JobExecutionContext executingJob : executingJobs) {
			ScheduleJob job = new ScheduleJob();
			JobDetail jobDetail = executingJob.getJobDetail();
			JobKey jobKey = jobDetail.getKey();
			Trigger trigger = executingJob.getTrigger();
			job.setJobName(jobKey.getName());
			job.setJobGroup(jobKey.getGroup());
			job.setDescription("触发器:" + trigger.getKey());
			Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
			job.setJobStatus(triggerState.name());
			if (trigger instanceof CronTrigger) {
				CronTrigger cronTrigger = (CronTrigger) trigger;
				String cronExpression = cronTrigger.getCronExpression();
				job.setCronExpression(cronExpression);
			}
			jobList.add(job);
		}
		return jobList;
	}

	/**
	 * 暂停一个job
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public void pauseJob(ScheduleJob scheduleJob) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		scheduler.pauseJob(jobKey);
	}

	/**
	 * 恢复一个job
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public void resumeJob(ScheduleJob scheduleJob) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		scheduler.resumeJob(jobKey);
	}

	/**
	 * 删除一个job
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public void deleteJob(ScheduleJob scheduleJob) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		scheduler.deleteJob(jobKey);

	}

	/**
	 * 立即执行job
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public void runAJobNow(ScheduleJob scheduleJob) throws SchedulerException {
		JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
		scheduler.triggerJob(jobKey);
	}

	/**
	 * 更新job时间表达式
	 * 
	 * @param scheduleJob
	 * @throws SchedulerException
	 */
	public void updateJobCron(ScheduleJob scheduleJob) throws SchedulerException {

		TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());

		CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());

		trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

		scheduler.rescheduleJob(triggerKey, trigger);
	}
}
  • 第三部分

关于前台显示界面的数据展示及维护,不做赘述 

猜你喜欢

转载自my.oschina.net/ytliyang/blog/1635037