使用H2(嵌入模式)持久化Quartz任务
🔝前言:
Quartz在包内提供了多种数据库的sql文件,大家可以选择方便的使用。路径如下。
1.初始化h2(不使用Server模式)
public class InitH2 {
private static String USER_NAME = "lee";
private static String PASSWORD = "";
/**
* 数据库的URL,初始化会在指定的目录下创建.db数据库文件
* (PS:该db文件不能被两个进程同时使用,请现关闭数据量工具的连接再运行程序)
* Server模式请使用jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName>
*/
private static String JDBC_URL = "jdbc:h2:./qtest.db";
/**
* 驱动类
*/
private static String DRIVER_NAME = "org.h2.Driver";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(DRIVER_NAME);
Connection connection = DriverManager.getConnection(JDBC_URL, USER_NAME, PASSWORD);
}
}
使用数据库连接工具创建需要的表
2.修改Quartz的配置文件
Quartz包中有默认的properties配置文件,可以直接复制出来进行修改。
这是原包内的配置文件:
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
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.simpl.RAMJobStore
修改后的配置文件:
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
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.simpl.RAMJobStore
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX
#设置JDBC的代理,(根据数据库类型选择)
#有PointbaseDelegate
#PostgreSQLDelegate
#MSSQLDelegate
#OracleDelegate
#等类型,如果找不到专属的化,请使用标准的JDBC代理
org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#表名的前缀--不需要修改
org.quartz.jobStore.tablePrefix:qrtz_
#数据库名称
org.quartz.jobStore.dataSource:PUBLIC
#数据源信息---注意dataSource后为数据库名称--需要更具实际情况修改
org.quartz.dataSource.PUBLIC.driver:org.h2.Driver
org.quartz.dataSource.PUBLIC.URL:jdbc:h2:./qtest.db
org.quartz.dataSource.PUBLIC.user:lee
org.quartz.dataSource.PUBLIC.password:
3.创建Trigger和Job并运行
测试执行任务后
public class InitH2 {
private static String USER_NAME = "lee";
private static String PASSWORD = "";
/**
* 数据库的URL
*/
private static String JDBC_URL = "jdbc:h2:./qtest.db";
/**
* 驱动类
*/
private static String DRIVER_NAME = "org.h2.Driver";
public static void main(String[] args) throws ClassNotFoundException, SQLException, SchedulerException, InterruptedException {
//Class.forName(DRIVER_NAME);
//Connection connection = DriverManager.getConnection(JDBC_URL, USER_NAME, PASSWORD);
StdSchedulerFactory factory = new StdSchedulerFactory();
StdScheduler scheduler = (StdScheduler)factory.getScheduler();
SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("TOne").startNow().withDescription("测试持久化")
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)).build();
JobDetail jobDetail = JobBuilder.newJob(SimpleJob.class).withIdentity("JOne").build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
Thread.sleep(6000);
}
public static class SimpleJob implements Job{
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("WDNMD");
}
}
}
运行结束后打开h2数据库,你的Scheduler绑定的Trigger和Job将会被保存到表中
4.从数据库中获取Trigger和Job直接使用
public class InitH2 {
private static String USER_NAME = "lee";
private static String PASSWORD = "";
/**
* 数据库的URL
*/
private static String JDBC_URL = "jdbc:h2:./qtest.db";
/**
* 驱动类
*/
private static String DRIVER_NAME = "org.h2.Driver";
public static void main(String[] args) throws ClassNotFoundException, SQLException, SchedulerException, InterruptedException {
StdSchedulerFactory factory = new StdSchedulerFactory();
StdScheduler scheduler = (StdScheduler)factory.getScheduler();
Trigger trigger = scheduler.getTrigger(new TriggerKey("TOne"));
JobDetail jobDetail = scheduler.getJobDetail(new JobKey("JOne"));
//注意拿Trigger重新解绑一下Job,不然下面Scheduler会重复绑定报错
scheduler.unscheduleJob(new TriggerKey("TOne"));
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
Thread.sleep(6000);
}
public static class SimpleJob implements Job{
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("WDNMD");
}
}
}
(PS:如果不使用Cluster的话,Quartz不会持久化Scheduler(除非你在properties里配置了scheduler实例),所以不要纠结于QRTZ_SCHEDULER_STATE表为什么没有数据)
目录结构:
5.转移配置到SpringBoot中
待续