使用quartz2.2.1,quartz有两种执行模式,一种是RAMJobStore,一种是JDBCJobStore。
1、RAMJobStore
RAMJobStore是默认的数据存储方式,其把数据存在本地内存中,官方宣称这是最有效率的方式(在本地内存当然快啦)。但是在宕机或者重启的时候数据就会丢失。
//配置方式
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
2、JDBCJobStore
JDBCJobStore终于把数据给持久化起来了,这个也是使用最广泛的数据存储方式。在于Spring集成后都不需要自己再配置一遍数据库连接了。建表脚本在官方包里面可以找到(http://www.quartz-scheduler.org/downloads/)
//配置方式
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
之前采用的是第一种方式,就像上面说的,这种方式对内存依赖较多,并且服务器一旦怠机或者重启,所有的定时任务的数据就会丢失,定时任务无法再跑起来,所以现在尝试使用第二种方法使用quartz。
1、执行quartz数据库脚本
首先下载quartz的官方压缩包,里面包括源码、examples、javadoc一应俱全,http://www.quartz-scheduler.org/downloads/。
下载之后找到quartz-2.2.1/docs/dbTables/tables_mysql.sql文件,去自己数据库中执行下,这是quartz任务和触发器的相关表。
2、添加quartz的properities
在resources目录下新建文件quartz.properities文件,该文件用来保存quartz执行的相关配置信息,具体内容如下:
# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#
org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.instanceId: AUTO
<!--线程池配置-->
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.jobStore.misfireThreshold: 60000
<!--jobstore配置-->
org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.useProperities: true
<!--数据库配置-->
org.quartz.jobStore.tablePrefix: qrtz_
org.quartz.jobStore.dataSource: qzDS
org.quartz.dataSource.qzDS.connectionProvider.class: org.quartz.examples.example17.MyPoolingconnectionProvider
org.quartz.dataSource.qzDS.driver: com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.url: jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.qzDS.user: root
org.quartz.dataSource.qzDS.password: 123456
org.quartz.dataSource.qzDS.maxConnections: 30
3、配置spring-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:property-placeholder location="classpath:quartz.properities" ignore-unresolvable="true"/>
<!-- 配置调度工厂 -->
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<properity name="applicationContextSchedulerContextKey" value="applicationContext"/>
<properity name="configLocation" value="classpath:quartz.properties"/>
</bean>
</beans>
4、编写Job类
import org.quartz.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JobTest implements Job {
@Override
public void excute(JobExecutionContext context) throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey();
JobDataMap data = context.getJobDetail.getJobDataMap();
String color = data.getString("color");
System.out.println("job <" + jobKey + "> start at:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " and color=" + color);
}
}
5、编写quartz的测试类
import org.quartz.*;
import org.springframework.context.ApplicationContext;
import org.springframework.support.ClassPathXmlApplicationContext;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
public class QuartzTest {
private static Scheduler scheduler;
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/spring-quartz.xml");
scheduler = (Scheduler) applicationContext.getBean("quartzScheduler");
startSchedule();
}
public static void startSchedule() {
JobDetail job = new Job(JobTest.class).withIdentity("job1", "group1").build();
CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
cron.withMisfireHandlingInstructionFireAndProceed();
CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronScheduleBuilder).build();
job.getJobDataMap.put("color", "Red");
try {
scheduler.deleteJob(job.getKey());
scheduler.scheduleJob(job, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}