1、导入quartz、C3p0、MySQL的jar包(懒得找jar包,直接创建springboot工程导入坐标)
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2、先quartz.jar看一下目录下的内容
该图中的单节点任务主要是单机版使用到的,下面那个是可以创建集群定时任务调度管理控制环境
3、查看调度工厂相关的类
该接口有三个方法
// 获取调度器
Scheduler getScheduler() throws SchedulerException;
// 通过名字获取调度器
Scheduler getScheduler(String var1) throws SchedulerException;
// 获取所有的调度器
Collection<Scheduler> getAllSchedulers() throws SchedulerException;
该接口有两个实现类,第一个类(推荐大佬,喜欢折腾的去搞),第二个是常见的实现
4、创建工厂类的实现,并获取调度器
在该类中可以看到该类有三个构造方法
分别是通过:
默认的方式创建创建
通过读取指定文件名的内容来创建
通过去读取PropertiesParser对象传的数据创建
获取调度器
当不是通过传入配置文件内容的情况创建的工厂时,会进行工厂初始化,再通过仓库同步创建一个实例,再通过默认的调度器实例名称去仓库查找改名字的的调度器,如果该调度器不为空,再判断改调度器是否关闭,没有关闭的话则返回改调度器,关闭的话,则从仓库移除改调度器;如果调度器为空,则进行实例化
SchedulerRepository的源码如下:
public class SchedulerRepository {
private HashMap<String, Scheduler> schedulers = new HashMap();
private static SchedulerRepository inst;
private SchedulerRepository() {
}
public static synchronized SchedulerRepository getInstance() {
if (inst == null) {
inst = new SchedulerRepository();
}
return inst;
}
public synchronized void bind(Scheduler sched) throws SchedulerException {
if ((Scheduler)this.schedulers.get(sched.getSchedulerName()) != null) {
throw new SchedulerException("Scheduler with name '" + sched.getSchedulerName() + "' already exists.");
} else {
this.schedulers.put(sched.getSchedulerName(), sched);
}
}
public synchronized boolean remove(String schedName) {
return this.schedulers.remove(schedName) != null;
}
public synchronized Scheduler lookup(String schedName) {
return (Scheduler)this.schedulers.get(schedName);
}
public synchronized Collection<Scheduler> lookupAll() {
return Collections.unmodifiableCollection(this.schedulers.values());
}
}
5、使用基于数据库的定时任务管理
通过加载自定义配置文件来完成实现
自定义配置文件存放于classpath下
org.quartz.scheduler.instanceName=MyQuartzScheduler
org.quartz.scheduler.dSName=MyDemo
org.quartz.scheduler.rmi.export=false
org.quartz.scheduler.rmi.proxy=false
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=10
org.quartz.threadPool.threadPriority=5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.myDS.provider=C3p0PoolingConnectionProvider
org.quartz.dataSource.myDS.driver=com.mysql.cj.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/test
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = root
org.quartz.dataSource.myDS.maxConnections = 5
org.quartz.dataSource.myDS.validationQuery = select 1
创建工厂获取调度器代码如下
StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory("myQuartz.properties");
Scheduler scheduler = stdSchedulerFactory.getScheduler();
通过加载自定义配置文件初始化部分源码如下
for(int i = 0; i < dsNames.length; ++i) {
PropertiesParser pp = new PropertiesParser(this.cfg.getPropertyGroup("org.quartz.dataSource." + dsNames[i], true));
String cpClass = pp.getStringProperty("connectionProvider.class", (String)null);
if (cpClass != null) {
// 代码省略
} else {
String dsJndi = pp.getStringProperty("jndiURL", (String)null);
String dsJndiProvider;
if (dsJndi != null) {
// 代码省略
} else {
plugInClass = pp.getStringProperty("provider");
dsJndiInitial = pp.getStringProperty("driver");
dsJndiProvider = pp.getStringProperty("URL");
if (dsJndiInitial == null) {
this.initException = new SchedulerException("Driver not specified for DataSource: " + dsNames[i]);
throw this.initException;
}
if (dsJndiProvider == null) {
this.initException = new SchedulerException("DB URL not specified for DataSource: " + dsNames[i]);
throw this.initException;
}
if (plugInClass != null && plugInClass.equals("hikaricp")) {
cpClass = "org.quartz.utils.HikariCpPoolingConnectionProvider";
} else {
cpClass = "org.quartz.utils.C3p0PoolingConnectionProvider";
}
this.log.info("Using ConnectionProvider class '" + cpClass + "' for data source '" + dsNames[i] + "'");
try {
listenerClass = null;
ConnectionProvider cp;
try {
Constructor constructor = loadHelper.loadClass(cpClass).getConstructor(Properties.class);
cp = (ConnectionProvider)constructor.newInstance(pp.getUnderlyingProperties());
} catch (Exception var75) {
this.initException = new SchedulerException("ConnectionProvider class '" + cpClass + "' could not be instantiated.", var75);
throw this.initException;
}
dbMgr = DBConnectionManager.getInstance();
dbMgr.addConnectionProvider(dsNames[i], cp);
this.populateProviderWithExtraProps((PoolingConnectionProvider)cp, pp.getUnderlyingProperties());
} catch (Exception var76) {
this.initException = new SchedulerException("Could not initialize DataSource: " + dsNames[i], var76);
throw this.initException;
}
}
}
}
String[] pluginNames = this.cfg.getPropertyGroups("org.quartz.plugin");
SchedulerPlugin[] plugins = new SchedulerPlugin[pluginNames.length];
// 代码省略
Class<?>[] strArg = new Class[]{
String.class};
String[] jobListenerNames = this.cfg.getPropertyGroups("org.quartz.jobListener");
JobListener[] jobListeners = new JobListener[jobListenerNames.length];
// 代码省略
String[] triggerListenerNames = this.cfg.getPropertyGroups("org.quartz.triggerListener");
TriggerListener[] triggerListeners = new TriggerListener[triggerListenerNames.length];
String listenerClass;
// 代码省略
boolean tpInited = false;
boolean qsInited = false;
listenerClass = this.cfg.getStringProperty("org.quartz.threadExecutor.class");
Object threadExecutor;
if (listenerClass != null) {
tProps = this.cfg.getPropertyGroup("org.quartz.threadExecutor", true);
try {
threadExecutor = (ThreadExecutor)loadHelper.loadClass(listenerClass).newInstance();
this.log.info("Using custom implementation for ThreadExecutor: " + listenerClass);
this.setBeanProps(threadExecutor, tProps);
} catch (Exception var68) {