Spring整合Quartz实现动态定时任务

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hans201507/article/details/50593834 1.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hans201507/article/details/50593834
</pre><p>1.创建动态定时任务实体</p><p><pre name="code" class="java">import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * 动态定时任务
 * 
 * @author
 * 
 */
@Entity
@Table(name = "SCHEDULE_JOB", schema = "JLW")
public class ScheduleJob implements java.io.Serializable {

	private static final long serialVersionUID = 7058240968903003001L;
	private Integer id;
	private Date createTime;
	private Date updateTime;
	private String jobName;// 任务名称
	private String jobGroup;// 任务分组
	private String jobStatus;// 任务状态 是否启动任务
	private String cronExpression;// cron表达式
	private String description;// 描述
	private String beanClass;// 任务执行时调用哪个类的方法 包名+类名
	private String isConcurrent;// 任务是否有状态(是否并发任务)
	private String springId;// spring bean
	private String methodName;// 任务调用的方法名

	@SequenceGenerator(name = "generator", sequenceName = "SEQ_SCHEDULE_JOB")
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "generator")
	@Column(name = "ID", unique = true, nullable = false, precision = 10, scale = 0)
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "CREATETIME")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "UPDATETIME")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@Column(name = "JOBNAME")
	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	@Column(name = "JOBGROUP")
	public String getJobGroup() {
		return jobGroup;
	}

	public void setJobGroup(String jobGroup) {
		this.jobGroup = jobGroup;
	}

	@Column(name = "JOBSTATUS")
	public String getJobStatus() {
		return jobStatus;
	}

	public void setJobStatus(String jobStatus) {
		this.jobStatus = jobStatus;
	}

	@Column(name = "CRONEXPRESSION")
	public String getCronExpression() {
		return cronExpression;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	@Column(name = "DESCRIPTION")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name = "BEANCLASS")
	public String getBeanClass() {
		return beanClass;
	}

	public void setBeanClass(String beanClass) {
		this.beanClass = beanClass;
	}

	@Column(name = "ISCONCURRENT")
	public String getIsConcurrent() {
		return isConcurrent;
	}

	public void setIsConcurrent(String isConcurrent) {
		this.isConcurrent = isConcurrent;
	}

	@Column(name = "SPRINGID")
	public String getSpringId() {
		return springId;
	}

	public void setSpringId(String springId) {
		this.springId = springId;
	}

	@Column(name = "METHODNAME")
	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
}
2.实现实体的dao层与service层,这里仅给出service接口的代码



public interface ScheduleJobService {

	List<ScheduleJob> findAll();

	void save(ScheduleJob scheduleJob);

	void update(ScheduleJob scheduleJob);

	ScheduleJob findById(Integer id);
}
3.实现系统动态定时任务管理service接口和实现



public interface ScheduleJobManageService {

	/**
	 * 添加任务
	 * 
	 * @param scheduleJob
	 */
	void addScheduleJob(ScheduleJob scheduleJob);

	/**
	 * 暂停任务
	 * 
	 * @param scheduleJob
	 */
	void pauseScheduleJob(ScheduleJob scheduleJob);

	/**
	 * 恢复任务
	 * 
	 * @param scheduleJob
	 */
	void resumeScheduleJob(ScheduleJob scheduleJob);

	/**
	 * 删除任务
	 * 
	 * @param scheduleJob
	 */
	void deleteScheduleJob(ScheduleJob scheduleJob);

	/**
	 * 立即执行任务
	 * 
	 * @param scheduleJob
	 */
	void runScheduleJobNow(ScheduleJob scheduleJob);

	/**
	 * 更新任务
	 * 
	 * @param scheduleJob
	 */
	void updateSchedule(ScheduleJob scheduleJob);

	/**
	 * 所有任务
	 * 
	 * @return
	 */
	List<ScheduleJob> getAllScheduleJob();

	/**
	 * 所有正在运行的任务
	 * 
	 * @return
	 */
	List<ScheduleJob> getAllScheduleJobRunning();

	/**
	 * 系统初始化添加计划任务
	 */
	void init();
}
@Service
@Transactional
public class ScheduleJobManageServiceImpl implements ScheduleJobManageService {

	public final Logger logger = Logger.getLogger(getClass());
	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;
	@Resource
	private ScheduleJobService scheduleJobService;

	@Override
	public void addScheduleJob(ScheduleJob scheduleJob) {
		if (scheduleJob == null || !Constants.SCHEDULEJOB_STATUS_RUNNING.equals(scheduleJob.getJobStatus())) {
			return;
		}
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		logger.info("开始添加任务,任务:" + scheduleJob.getJobName());
		try {
			TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
			// 不存在,创建一个
			if (trigger == null) {
				Class clazz = Constants.SCHEDULEJOB_CONCURRENT_IS.equals(scheduleJob.getIsConcurrent()) ? ScheduleJobFactory.class : ScheduleJobFactoryNotCurrent.class;
				JobDetail jobDetail = JobBuilder.newJob(clazz).withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
				jobDetail.getJobDataMap().put("scheduleJob", scheduleJob);
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
				trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(scheduleBuilder).build();
				scheduler.scheduleJob(jobDetail, trigger);
			} else {
				// Trigger已存在,那么更新相应的定时设置
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
				// 按新的cronExpression表达式重新构建trigger
				trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
				// 按新的trigger重新设置job执行
				scheduler.rescheduleJob(triggerKey, trigger);
			}
		} catch (Exception e) {
			logger.error("添加任务出错,任务:" + scheduleJob.getJobName());
		}
		logger.info("添加任务完成,任务:" + scheduleJob.getJobName());
	}

	@Override
	public void pauseScheduleJob(ScheduleJob scheduleJob) {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
			scheduler.pauseJob(jobKey);
		} catch (Exception e) {
			logger.error("暂停任务出错,任务:" + scheduleJob.getJobName());
			throw new MyException("暂停任务出错,任务:" + scheduleJob.getJobName(), e);
		}
	}

	@Override
	public void resumeScheduleJob(ScheduleJob scheduleJob) {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
			scheduler.resumeJob(jobKey);
		} catch (Exception e) {
			logger.error("恢复任务出错,任务:" + scheduleJob.getJobName());
			throw new MyException("恢复任务出错,任务:" + scheduleJob.getJobName(), e);
		}
	}

	@Override
	public void deleteScheduleJob(ScheduleJob scheduleJob) {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
			scheduler.deleteJob(jobKey);
		} catch (Exception e) {
			logger.error("删除任务出错,任务:" + scheduleJob.getJobName());
			throw new MyException("删除任务出错,任务:" + scheduleJob.getJobName(), e);
		}
	}

	@Override
	public void runScheduleJobNow(ScheduleJob scheduleJob) {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
			scheduler.triggerJob(jobKey);
		} catch (Exception e) {
			logger.error("立即启动任务出错,任务:" + scheduleJob.getJobName());
			throw new MyException("立即启动任务出错,任务:" + scheduleJob.getJobName(), e);
		}
	}

	@Override
	public void updateSchedule(ScheduleJob scheduleJob) {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			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);
		} catch (Exception e) {
			logger.error("更新任务出错,任务:" + scheduleJob.getJobName());
			throw new MyException("更新任务出错,任务:" + scheduleJob.getJobName(), e);
		}
	}

	@Override
	public List<ScheduleJob> getAllScheduleJob() {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			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;
		} catch (Exception e) {
			logger.error("获取系统所有任务出错!");
			throw new MyException("获取系统所有任务出错!", e);
		}
	}

	@Override
	public List<ScheduleJob> getAllScheduleJobRunning() {
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			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;
		} catch (Exception e) {
			logger.error("获取系统所有正在运行任务出错!");
			throw new MyException("获取系统所有正在运行任务出错!", e);
		}
	}

	@Override
	public void init() {
		// 这里获取任务信息数据
		List<ScheduleJob> jobList = scheduleJobService.findAll();
		for (ScheduleJob scheduleJob : jobList) {
			addScheduleJob(scheduleJob);
		}
	}
}
service实现需要调用的类



public class ScheduleJobFactory implements Job {

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
		ScheduleJobUtil.invokMethod(scheduleJob);
	}
}
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.tongdow.jianle.entity.ScheduleJob;

@DisallowConcurrentExecution
public class ScheduleJobFactoryNotCurrent implements Job {

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
		ScheduleJobUtil.invokMethod(scheduleJob);
	}
}


public class ScheduleJobUtil {

	public final static Logger logger = Logger.getLogger(ScheduleJobUtil.class);

	/**
	 * 通过反射调用scheduleJob中定义的方法
	 * 
	 * @param scheduleJob
	 */
	public static void invokMethod(ScheduleJob scheduleJob) {
		Object object = null;
		Class clazz = null;
		try {
			if (!Common.isNullOrEmpty(scheduleJob.getSpringId()) && SpringUtils.containsBean(scheduleJob.getSpringId())) {
				object = SpringUtils.getBean(scheduleJob.getSpringId());
			} else if (!Common.isNullOrEmpty(scheduleJob.getBeanClass())) {
				clazz = Class.forName(scheduleJob.getBeanClass());
				object = clazz.newInstance();
			}
			if (object == null) {
				logger.error("任务:" + scheduleJob.getJobName() + ",结果:启动失败!");
				return;
			}
			clazz = object.getClass();
			Method method = null;
			method = clazz.getDeclaredMethod(scheduleJob.getMethodName());
			if (method != null) {
				method.invoke(object);
			} else {
				logger.error("任务:" + scheduleJob.getJobName() + ",结果:启动失败!");
				return;
			}
			logger.info("任务:" + scheduleJob.getJobName() + ",结果:启动成功!");
		} catch (Exception e) {
			logger.error("任务:" + scheduleJob.getJobName() + ",结果:启动失败!,原因:" + e.getMessage());
			throw new MyException("计划任务启动出错", e);
		}
	}
}
4.动态定时任务的执行方法



@Component
public class JobTask {

	public Logger logger = Logger.getLogger(getClass());
	@Resource
	private GoodsOrdersService goodsOrdersService;

	public void goodsOrder() {
		logger.info("商品订单处理定时任务开始!");
		logger.info("商品订单处理定时任务结束!");
	}

	public void order() {
		logger.info("订单处理定时任务开始!");
		logger.info("订单处理定时任务结束!");
	}
}
5.Spring配置文件需加入的内容

<bean id="springUtils" class="xxx" /><pre name="code" class="html"><!-- xxx为具体的类 -->
 
 
<!-- 任务调度器生成器,Spring生成具有指定属性调度器对象 -->
    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
       <!-- 生成的调度器所管理的所有触发器 -->
       <property name="triggers">
           <list>
              <ref bean="goodsOrder" /><!-- 项目发布定时任务 -->
           </list>
       </property>
       <property name="autoStartup" value="true" />
    </bean>   

    <!-- 触发器生成器类,用被指定的调度器调度生成指定规则的触发器对象 -->
    <bean id="goodsOrder" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
       <!-- 触发后要做的操作,这个操作由spring管理,只需要指定一个MethodInvokingJobDetailFactoryBean -->
       <property name="jobDetail" ref="goodsOrderJobDetail" />
       <!-- 触发规则,这里的触发规则是每隔1分钟后触发一次 -->
       <property name="cronExpression" value="0 */1 * * * ?" /><!--  -->
    </bean>
    
       
    <!--任务生成器,由spring管理,只需要指定其对应的对象和方法即可,任务的执行即是某个对象调用一个方法 -->
    <bean id="goodsOrderJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
       <!-- 指定该任务对应的调用对象,这个对象所属的类无需实现任何接口-->
        <property name="targetObject" ref="jobTask" />
       <!-- 指定在targetObject对象中某个的方法 -->
       <property name="targetMethod" value="goodsOrder" />
       <!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
       <property name="concurrent" value="false" />
    </bean>
    
	
    <!-- 一个普通的类-->
    <bean id="jobTask" class="xxx同上"/>
配置文件说明,如果只需要动态定时任务,则只需要

<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"/>
其他的为非动态定时任务
如果配置了这个
<bean id="jobTask" class="xxx同上"/>
则实体类可以springId字段可以指定jobTask,前提是执行任务的方法是写在这个bean里的。

6.用到的常量

/**
 * 常量定义
 * 
 * @author
 * 
 */
public class Constants {

	// 计划任务 状态和是否有状态
	public static final String SCHEDULEJOB_STATUS_RUNNING = "1";// 启动任务
	public static final String SCHEDULEJOB_STATUS_NOT_RUNNING = "0";// 不启动任务
	public static final String SCHEDULEJOB_CONCURRENT_IS = "1";// 有状态
	public static final String SCHEDULEJOB_CONCURRENT_NOT = "0";// 无状态
}
7.系统初始化

<span style="white-space:pre">	</span>logger.info("系统开始初始化");
<span style="white-space:pre">	</span>// 获取容器与相关的Service对象
<span style="white-space:pre">	</span>ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
<span style="white-space:pre">	</span>ScheduleJobManageService scheduleJobManageService = ac.getBean(ScheduleJobManageService.class);
<span style="white-space:pre">	</span>// 初始化系统动态定时任务
	scheduleJobManageService.init();
	logger.info("系统初始化完成");

结束,有问题可以qq135018413








相关文章
|
4月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
130 1
|
2月前
|
Java BI 调度
Java Spring的定时任务的配置和使用
遵循上述步骤,你就可以在Spring应用中轻松地配置和使用定时任务,满足各种定时处理需求。
142 1
|
2月前
|
存储 Java API
简单两步,Spring Boot 写死的定时任务也能动态设置:技术干货分享
【10月更文挑战第4天】在Spring Boot开发中,定时任务通常通过@Scheduled注解来实现,这种方式简单直接,但存在一个显著的限制:任务的执行时间或频率在编译时就已经确定,无法在运行时动态调整。然而,在实际工作中,我们往往需要根据业务需求或外部条件的变化来动态调整定时任务的执行计划。本文将分享一个简单两步的解决方案,让你的Spring Boot应用中的定时任务也能动态设置,从而满足更灵活的业务需求。
104 4
|
5月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14985 29
|
4月前
|
Java 关系型数据库 MySQL
SpringBoot 集成 Quartz + MySQL
SpringBoot 集成 Quartz + MySQL
116 1
|
4月前
|
Java 开发者 Spring
Spring Boot实战宝典:揭秘定时任务的幕后英雄,让业务处理如流水般顺畅,轻松驾驭时间管理艺术!
【8月更文挑战第29天】在现代应用开发中,定时任务如数据备份、报告生成等至关重要。Spring Boot作为流行的Java框架,凭借其强大的集成能力和简洁的配置方式,为开发者提供了高效的定时任务解决方案。本文详细介绍了如何在Spring Boot项目中启用定时任务支持、编写定时任务方法,并通过实战案例展示了其在业务场景中的应用,同时提供了注意事项以确保任务的正确执行。
53 0
|
4月前
|
Dubbo Java 调度
揭秘!Spring Cloud Alibaba的超级力量——如何轻松驾驭分布式定时任务调度?
【8月更文挑战第20天】在现代微服务架构中,Spring Cloud Alibaba通过集成分布式定时任务调度功能解决了一致性和可靠性挑战。它利用TimerX实现任务的分布式编排与调度,并通过`@SchedulerLock`确保任务不被重复执行。示例代码展示了如何配置定时任务及其分布式锁,以实现每5秒仅由一个节点执行任务,适合构建高可用的微服务系统。
71 0
|
5月前
|
SQL Java 调度
实时计算 Flink版产品使用问题之使用Spring Boot启动Flink处理任务时,使用Spring Boot的@Scheduled注解进行定时任务调度,出现内存占用过高,该怎么办
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
5月前
|
开发框架 自然语言处理 Java
如何在Spring Boot中实现动态多语言支持
如何在Spring Boot中实现动态多语言支持
|
5月前
|
XML Java Linux
Spring Task 定时任务没有定时执行是为什么?
Spring Task 定时任务没有定时执行是为什么?
72 2