编写quartz的jobServvice类:
package top.alterem.job.service; import Java.text.ParseException; import java.util.List; import java.util.UUID; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import top.alterem.StringUtils; import top.alterem.common.persistence.Page; import top.alterem.common.service.CrudService; import top.alterem.job.dao.JobDao; import top.alterem.job.entity.Job; /** * 定时调度任务Service * * @author honghu */ @Service @Transactional(readOnly = true) public class JobService extends CrudService<JobDao, Job> { @Autowired private JobDao jobDao; private Logger logger = LoggerFactory.getLogger(getClass()); public Job get(String id) { return super.get(id); } public List<Job> findList(Job job) { return super.findList(job); } public Page<Job> findPage(Page<Job> page, Job job) { return super.findPage(page, job); } @Transactional(readOnly = false) public void save(Job job) { super.save(job); // 启用则启动任务 if (StringUtils.equals("1", job.getIsEnable())) { startTask(job, job.getId()); } } @Transactional(readOnly = false) public void update(Job job) { //结束定时调度 endTask(job.getId()); job.preUpdate(); jobDao.update(job); // 启用则启动任务 if (StringUtils.equals("1", job.getIsEnable())) { startTask(job, job.getId()); } } @Transactional(readOnly = false) public void delete(Job job) { //结束任务 endTask(job.getId()); super.delete(job); } /** * 系统初始加载任务 */ public void loadJob() throws Exception { List<Job> jobList = this.findList(new Job()); if ( != jobList && jobList.size() > 0) { for (int i = 0; i < jobList.size(); i++) { Job job = jobList.get(i); // 任务开启状态 执行任务调度 if (StringUtils.equals("1", job.getIsEnable())) { try { JobDetail jobDetail = new JobDetail(); // 设置任务名称 if (StringUtils.isNotBlank(job.getId())) { jobDetail.setName(job.getId()); } else { UUID uuid = UUID.randomUUID(); jobDetail.setName(uuid.toString()); job.setId(uuid.toString()); } jobDetail.setGroup(Scheduler.DEFAULT_GROUP); // 设置任务执行类 jobDetail.setJobClass(getClassByTask(job.getJobClass())); // 添加任务参数 CronTrigger cronTrigger = new CronTrigger("cron_" + i, Scheduler.DEFAULT_GROUP, jobDetail.getName(), Scheduler.DEFAULT_GROUP); cronTrigger.setCronExpression(getCronExpressionFromDB(job.getId())); // 调度任务 scheduler.scheduleJob(jobDetail, cronTrigger); } catch (SchedulerException e) { logger.error("JobService SchedulerException", e); } catch (ClassNotFoundException e) { logger.error("JobService ClassNotFoundException", e); } catch (Exception e) { logger.error("JobService Exception", e); } } } } } /** * * @param taskClassName * 任务执行类名 * @return * @throws ClassNotFoundException */ @SuppressWarnings("rawtypes") private Class getClassByTask(String taskClassName) throws ClassNotFoundException { return Class.forName(taskClassName); } public String getCronExpressionFromDB(String id) throws Exception { // 设置任务规则 Job job = this.get(id); if ( != job) { if (Job.EXECYCLE_CRON == Integer.parseInt(job.getExecycle())) { return job.getCronExpression(); } else { Integer execycle = Integer.parseInt(job.getJobIntervalUnit()); String excep = ""; if (execycle.equals(Job.EXECYCLE_MONTH)) { excep = "0 " + job.getMinute() + " " + job.getHour() + " " + job.getDayOfMonth() + " * ?"; } else if (execycle.equals(Job.EXECYCLE_WEEK)) { excep = "0 " + job.getMinute() + " " + job.getHour() + " " + " ? " + " * " + job.getDayOfWeek(); } else if (execycle.equals(Job.EXECYCLE_DAY)) { excep = "0 " + job.getMinute() + " " + job.getHour() + " " + " * * ?"; } else if (execycle.equals(Job.EXECYCLE_HOUR)) { excep = "0 0 */" + job.getIntervalHour() + " * * ?"; } else if (execycle.equals(Job.EXECYCLE_MINUTE)) { excep = "0 */" + job.getIntervalMinute() + " * * * ?"; } return excep; } } return ""; } private void startTask(Job job, String id) { try { String cronExpress = getCronExpressionFromDB(id); if (StringUtils.isNotEmpty(cronExpress) && cronExpress.indexOf("null") == -1) { JobDetail jobDetail = new JobDetail(); jobDetail.setName(id); jobDetail.setGroup(Scheduler.DEFAULT_GROUP); jobDetail.setJobClass(getClassByTask(job.getJobClass())); CronTrigger cronTrigger = new CronTrigger("cron_" + id, Scheduler.DEFAULT_GROUP, jobDetail.getName(), Scheduler.DEFAULT_GROUP); cronTrigger.setCronExpression(cronExpress); scheduler.scheduleJob(jobDetail, cronTrigger); } } catch (ParseException e) { logger.error("JobService ParseException", e); } catch (Exception e) { logger.error("JobService Exception", e); } } private void endTask(String id) { try { scheduler.deleteJob(id, Scheduler.DEFAULT_GROUP); } catch (SchedulerException e) { logger.error("JobService endTask", e); } } @Autowired private Scheduler scheduler; }
编写相关job的Controller、dao、dao.xml我这边就不写了,其实就是对数据的增删改查操作
启动项目验证quartz是否成功:
项目启动个控制台: