上一节(Quartz(08) quartz spring web 项目的整合(方法二))
这一章我们将采用最简单的一种方式整合quartz spring web. 达到的效果是,我们只需要编写自己的job类,关于job,trigger 的配置信息都存放到数据库.(注:我们公司的的项目就是这么配置的,非常方便)
源码下载地址
1.编写我们自定义的job类Q1,Q2
package com.quartz.job;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class Q1 implements Serializable, Job {
private static final long serialVersionUID = 6890216263057956690L;
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println(sdf.format(new Date()));
System.out.println("------------------------");
}
}
package com.quartz.job;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
public class Q2 implements Serializable, Job {
private static final long serialVersionUID = 5004730246347558783L;
public void execute(JobExecutionContext arg0) {
System.out.println("************************");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
System.out.println(sdf.format(new Date()));
System.out.println("************************");
}
}
# 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.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
#jobStoreTX TEST
org.quartz.jobStore.misfireThreshold=60000
# TX method
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
#jdbc Delegate
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#table prefix
org.quartz.jobStore.tablePrefix=qrtz_
#
org.quartz.jobStore.dataSource=myDS
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/test
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=123456
org.quartz.dataSource.myDS.maxConnections=10
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="configLocation" value="classpath:quartz.properties" />
<property name="applicationContextSchedulerContextKey"
value="applicationContextKey" />
<property name="autoStartup" value="true" />
bean>
beans>
那么我们的关于job和trigger的信息到哪里去了? 去了数据库,配置如下:
DELETE from qrtz_cron_triggers;
DELETE from qrtz_triggers;
DELETE from qrtz_fired_triggers;
DELETE from qrtz_job_details;
COMMIT;
INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail1', 'DEFAULT', NULL, 'com.quartz.job.Q1', '1', '0', '0', '0', null);
INSERT INTO `qrtz_job_details` VALUES ('scheduler', 'myJobDetail2', 'DEFAULT', NULL, 'com.quartz.job.Q2', '1', '0', '0', '0', null);
INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', 'myJobDetail1', 'DEFAULT', NULL,
0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null);
INSERT INTO `qrtz_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', 'myJobDetail2', 'DEFAULT', NULL,
0, 0, 0, 'ACQUIRED', 'CRON', 1470241748000, 0, NULL, 0, null);
INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group1', '0/5 * * * * ?', 'Asia/Shanghai');
INSERT INTO `qrtz_cron_triggers` VALUES ('scheduler', 'my_job1', 'my_group2', '0/5 * * * * ?', 'Asia/Shanghai');
COMMIT;
这样在启动web项目的时候,Quartz就可以正常工作了.