前言
有时候任务执行可能会延迟,这里我们主要检测下一下延迟超过5分钟的任务,然后发送给管理员,告诉他有任务延迟了
这个任务量很小,所有就没有必要放到数据库中存储任务了,使用ram存储
1、相关数据表
数据表在上两节有介绍
qrtz_job_details 工作详情表
qrtz_triggers 触发器任务执行表
2、quartz 配置文件
#配置线程池的容量,即表示同时最多可运行的线程数量
org.quartz.threadPool.threadCount = 20
org.quartz.scheduler.skipUpdateCheck = true
#scheduler实例名称。
org.quartz.scheduler.instanceName = HealerJeanQuartzScheduler
org.quartz.scheduler.jobFactory.class = org.quartz.simpl.SimpleJobFactory
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
#job存储方式,RAMJobStore是使用JobStore最简单的一种方式,它也是性能最高效的,顾名思义,JobStore是把它的数据都存储在RAM中,
# 这也是它的快速和简单配置的原因;JDBCJobStore也是一种相当有名的JobStore,它通过JDBC把数据都保存到数据库中,
# 所以在配置上会比RAMJobStore复杂一些,而且不像RAMJobStore那么快,但是当我们对数据库中的表的主键创建索引时,性能上的缺点就不是很关键的了。
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
3、开始配置定时器Schdule
package com.hlj;
import com.hlj.quartz.core.event.QuartzListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 类描述:
* 创建人: HealerJean
*/
@Configuration
public class ListenerConfig {
/**
* 配置定时器初始化
* @return
*/
@Bean
public QuartzListener quartzListener(){
return new QuartzListener();
}
}
3.1、初始化Schdule
package com.hlj.quartz.core.event;
import com.hlj.quartz.core.schedule.HealerJeanScheduler;
import org.quartz.SchedulerException;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;
/**
* web应用启动的时候执行,初始化quartz
* QuartzInitializerListener implements ServletContextListener,所以可以用下面的,或者我们自己也可以设置其他方式,只要让它启动的时候执行
*/
@WebListener
public class QuartzListener extends QuartzInitializerListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
super.contextInitialized(servletContextEvent);
// 可以使用下面的传入,但是我认为还是没有必要了
// schedulerFactory会自动装入ServletContext 可以使用schedulerFactory.get
// ServletContext ctx = servletContextEvent.getServletContext();
// StdSchedulerFactory schedulerFactory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
// HealerJeanScheduler.initialise(schedulerFactory);
HealerJeanScheduler.initialise();
}
}
4、定时器配置以及添加任务监听
package com.hlj.quartz.core.schedule;
import com.hlj.quartz.core.event.HealerJeanJobListener;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class HealerJeanScheduler {
private static Logger logger = LoggerFactory.getLogger(HealerJeanScheduler.class);
private final static String TRIGGER_PERFIX = "t_";
private Scheduler scheduler = null;
private static HealerJeanScheduler instance = null;
private HealerJeanScheduler(){
}
private HealerJeanScheduler(Scheduler scheduler){
this.scheduler = scheduler;
}
public static HealerJeanScheduler getInstance(){
if (instance == null){
logger.error("AdmoreScheduler not initialized");
throw new RuntimeException("AdmoreScheduler not initialized");
}
return instance;
}
/**
* 初始化一个定时器调度器
*/
// public static void initialise(StdSchedulerFactory stdSchedulerFactory){
public static void initialise(){
if