时间:2021-07-01 10:21:17 帮助过:8人阅读
第四步:创建spring-quartz配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <description>spring任务调度,quartz任务调度,该文件是空配置,确保能启动定时任务,具体 业务配置需在页面进行配置即可,不用描述在该文件中</description> <task:annotation-driven executor="quartzTaskExecutor" /> <task:executor id="quartzTaskExecutor" keep-alive="900" pool-size="10" queue-capacity="20" /> <!-- Quartz集群Schduler --> <bean id="clusterQuartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- Triggers集成 动态配置 --> <!-- <property name="triggers"> <list> <ref bean="allocationPlanTrigger" /> </list> </property> --> <!-- quartz配置文件路径, 指向cluster配置 --> <property name="configLocation" value="classpath:quartz.properties" /> <!-- 启动时延期2秒开始任务 --> <property name="startupDelay" value="2" /> <!-- 保存Job数据到数据库所需的数据源 --> <property name="dataSource" ref="dataSource" /> <!-- Job接受applicationContext的成员变量名 --> <property name = "schedulerContextAsMap"> <map> <!-- spring管理的服务需要放到这里,才能够注入成功 --> <description> schedulerContextAsMap </description > <entry key = "allocationOrderService" value-ref = "allocationOrderService" /> </map > </property > <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <!-- 修改job时,更新到数据库 --> <property name="overwriteExistingJobs" value="true" /> </bean> <!-- 定时任务 页面动态配置--> <!-- <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myTask" /> 每5秒执行一次 <property name="cronExpression" value="*/5 * * * * ? " /> </bean> <bean id="myTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="durability" value="true" /> <property name="jobClass" value="com.yestae.tss.common.quartz.task.MyTask" /> </bean> --> </beans>
因为是基于页面进行动态配置的,所以只注册scheduler,如果你的自定义任务里需要使用spring的bean,schedulerContextAsMap 可以帮你完成注入,但在你的执行任务里要写get和set方法,不需要使用注解。
第五步:创建我们需要执行的任务类
package com.yestae.tss.common.quartz.task; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.QuartzJobBean; import com.yestae.tss.allocation.service.IAllocationOrderService; public class AllocationPlanTask extends QuartzJobBean { private IAllocationOrderService allocationOrderService; //注入service,記得生成get和set方法,才能注入成功 @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { //TODO 执行自己的任务 System.out.println(allocationOrderService+"---"+new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss").format(new Date())); } public IAllocationOrderService getAllocationOrderService() { return allocationOrderService; } public void setAllocationOrderService( IAllocationOrderService allocationOrderService) { this.allocationOrderService = allocationOrderService; } }
基本上完成就差不多了,下面来进行定时器的动态管理,直接上代码
接口
package com.yestae.tss.common.quartz.service; import java.text.ParseException; import java.util.List; import org.quartz.SchedulerException; import com.yestae.tss.common.quartz.VO.QuartzJobVo; public interface ISchedulerService { /** * @Description: 添加一个定时任务 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * @param jobClass * 任务 * @param time * 时间设置,参考quartz说明文档 * @throws ParseException * @throws SchedulerException */ Boolean addJod(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) throws ParseException, SchedulerException; /** * @Description: 移除一个任务 * * @param jobName * 任务名 * @param jobGroupName * 任务组名 * @param triggerName * 触发器名 * @param triggerGroupName * 触发器组名 * */ public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws ParseException, SchedulerException; /** * @Description: 修改一个任务的触发时间 * * @param triggerName * 触发器名称 * @param triggerGroupName * 触发器组名称 * @param time * */ public Boolean modifyJobTime(String triggerName, String triggerGroupName, String time) throws ParseException, SchedulerException; /** * @Description: 修改一个任务的触发时间 * * @param triggerName * 触发器名称 * @param triggerGroupName * 触发器组名称 * */ public List<QuartzJobVo> getTriggerS(String triggerName, String triggerGroupName) throws ParseException, SchedulerException; /** * @Description: 暂停任务,但当前任务会执行完毕,执行完毕则不会继续执行了 * * @param jobName * 任务名称 * @param groupName * 任务组名称 * */ public void pauseJob(String jobName, String groupName) throws SchedulerException; }
package com.yestae.tss.common.quartz.service; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import org.quartz.CronExpression; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.common.collect.Lists; import com.yestae.tss.common.quartz.VO.QuartzJobVo; @Service public class SchedulerServiceImpl implements ISchedulerService { @Autowired private Scheduler scheduler; @Override public Boolean addJod(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String time) throws ParseException, SchedulerException { JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任务名,任务组,任务执行类 jobDetail.setDurability(true); //任务完成后任然将任务保存在数据库 // 触发器 CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组 trigger.setCronExpression(time);// 触发器时间设定 scheduler.scheduleJob(jobDetail, trigger); return null; } @Override public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws ParseException, SchedulerException { scheduler.pauseTrigger(triggerName, triggerGroupName);// 停止触发器 scheduler.unscheduleJob(triggerName, triggerGroupName);// 移除触发器 scheduler.deleteJob(jobName, jobGroupName);// 删除任务 return; } @Override public Boolean modifyJobTime(String triggerName, String triggerGroupName, String time) throws ParseException, SchedulerException { CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerName,triggerGroupName); if (trigger == null) { return false; } String oldTime = trigger.getCronExpression(); if (!oldTime.equalsIgnoreCase(time)) { // 修改时间 trigger.setCronExpression(time); // 重启触发器 scheduler.resumeTrigger(triggerName, triggerGroupName); } return null; } @Override public List<QuartzJobVo> getTriggerS(String triggerName, String triggerGroupName) throws ParseException, SchedulerException { List<QuartzJobVo> list = Lists.newArrayList(); //获取任务名称 for (String groupName : scheduler.getJobGroupNames()) { for (String jobName : scheduler.getJobNames(groupName)) { QuartzJobVo quartzJobVo = new QuartzJobVo(); Trigger[] triggers = scheduler.getTriggersOfJob(jobName,groupName); CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggers[0].getName(), triggers[0].getGroup()); quartzJobVo.setJobName(jobName); quartzJobVo.setJobGroupName(groupName); quartzJobVo.setTriggerGroupName(triggers[0].getGroup()); quartzJobVo.setTriggerName(triggers[0].getName()); quartzJobVo.setCronExpression(trigger.getCronExpression()); list.add(quartzJobVo); } } return list; } @Override public void pauseJob(String jobName, String groupName) throws SchedulerException { scheduler.pauseJob(jobName, groupName); return; } }
前端通过参数进行添加,删除,修改相关配置就ok了,前端页面目前还没出来,等出来了,给予展示出来。
定时任务结果:
spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)
标签:except mysq 启动 upn rabl return store into int