触发器:存放时间排程
任务:蔟业务代码
排程器:负责调度,即在指定的时间执行对应的任务
如果是分布式quartz,则各个节点会上报任务,存到数据库中,执行时会从数据库中取出触发器来执行,如果触发器的名称和执行时间相同,则只有一个节点去执行此任务。
如果此节点执行失败,则此任务则会被分派到另一节点执行。
quartz.properties
#============================================================================
# configure jobstore
# using spring datasource in quartzjobsconfig.xml
# spring uses localdatasourcejobstore extension of jobstorecmt
#============================================================================
org.quartz.jobstore.useproperties=true
org.quartz.jobstore.tableprefix = qrtz_
org.quartz.jobstore.isclustered = true
org.quartz.jobstore.clustercheckininterval = 5000
org.quartz.jobstore.misfirethreshold = 60000
org.quartz.jobstore.txisolationlevelreadcommitted = true
# change this to match your db vendor
org.quartz.jobstore.class = org.quartz.impl.jdbcjobstore.jobstoretx
org.quartz.jobstore.driverdelegateclass = org.quartz.impl.jdbcjobstore.stdjdbcdelegate
#============================================================================
# configure main scheduler properties
# needed to manage cluster instances
#============================================================================
org.quartz.scheduler.instanceid=auto
org.quartz.scheduler.instancename=my_clustered_job_scheduler
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
#============================================================================
# configure threadpool
#============================================================================
org.quartz.threadpool.class = org.quartz.simpl.simplethreadpool
org.quartz.threadpool.threadcount = 10
org.quartz.threadpool.threadpriority = 5
org.quartz.threadpool.threadsinheritcontextclassloaderofinitializingthread = true
web-schedule-applicationcontext.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemalocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="executor" class="org.springframework.scheduling.concurrent.threadpooltaskexecutor">
<property name="corepoolsize" value="10" />
<property name="maxpoolsize" value="100" />
<property name="queuecapacity" value="500" />
bean>
<bean id="webscheduler"
class="org.springframework.scheduling.quartz.schedulerfactorybean">
<property name="configlocation" value="classpath:/properties/config/quartz.properties" />
<property name="datasource" ref="datasourcecms" />
<property name="transactionmanager" ref="txmanager" />
<property name="schedulername" value="quartzscheduler" />
<property name="overwriteexistingjobs" value="true" />
<property name="startupdelay" value="5"/>
<property name="applicationcontextschedulercontextkey" value="applicationcontext" />
<property name="jobfactory">
<bean class="com.tcl.project7.boss.common.scheduling.autowiringspringbeanjobfactory" />
property>
<property name="triggers">
<list>
<ref bean="springquertzclustertaskschedulertestertigger" />
list>
property>
<property name="jobdetails">
<list>
<ref bean="springquertzclustertaskschedulertesterjobdetail" />
list>
property>
<property name="taskexecutor" ref="executor" />
bean>
<bean id="springquertzclustertaskschedulertestertigger" class="common.scheduling.persistablecrontriggerfactorybean">
<property name="jobdetail" ref="springquertzclustertaskschedulertesterjobdetail"/>
<property name="cronexpression" value="* * * * * ?" />
bean>
<bean id="springquertzclustertaskschedulertesterjobdetail" class="org.springframework.scheduling.quartz.jobdetailbean">
<property name="jobclass" value="common.scheduling.springquertzclustertaskschedulertester" />
<property name="requestsrecovery" value="false"/>
bean>
beans>
job文件:springquertzclustertaskschedulertester.java
package common.scheduling;
import java.util.date;
import org.quartz.jobexecutioncontext;
import org.quartz.jobexecutionexception;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.scheduling.quartz.quartzjobbean;
import com.tcl.project7.boss.common.util.urlutil;
import com.tcl.project7.boss.common.util.time.timeutils;
/**
* title:springquertzclustertaskschedulertester
* description:
* 应为要持久化等特性操作,需要继承 quartzjobbean
*
由于要被持久化,所以不能存放xxxxmanager类似对象,
* 只能从每次从quartzjobbean注入的applicationcontext 中去取出
*
*
*
*
*/public class springquertzclustertaskschedulertester
extends quartzjobbean {
private static logger logger = loggerfactory.getlogger(springquertzclustertaskschedulertester.
class);
@autowired
private urlutil urlutil;
protected void executeinternal(jobexecutioncontext arg0)
throws jobexecutionexception {
logger.info("------" timeutils.formattime(
new date()) "------" urlutil.getnginxhost());
system.out.println("------" timeutils.formattime(
new date()) "------" urlutil.getnginxhost());
}
}
如果job中有需要调用spring的bean,则需要此文件autowiringspringbeanjobfactory.java
package common.scheduling;
import org.quartz.spi.triggerfiredbundle;
import org.springframework.beans.factory.config.autowirecapablebeanfactory;
import org.springframework.context.applicationcontext;
import org.springframework.context.applicationcontextaware;
import org.springframework.scheduling.quartz.springbeanjobfactory;
/**
* autowire quartz jobs with spring context dependencies
* @see http://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring/15211030#15211030
*/
public final class autowiringspringbeanjobfactory extends springbeanjobfactory implements applicationcontextaware {
private transient autowirecapablebeanfactory beanfactory;
public void setapplicationcontext(final applicationcontext context) {
beanfactory = context.getautowirecapablebeanfactory();
}
@override
protected object createjobinstance(final triggerfiredbundle bundle) throws exception {
final object job = super.createjobinstance(bundle);
beanfactory.autowirebean(job);
return job;
}
}
由于job需要存储到数据库中,会产生property的问题,需剔除job-data,需此文件persistablecrontriggerfactorybean.java
package common.scheduling;
import org.springframework.scheduling.quartz.crontriggerfactorybean;
import org.springframework.scheduling.quartz.jobdetailawaretrigger;
/**
* needed to set quartz useproperties=true when using spring classes,
* because spring sets an object reference on jobdatamap that is not a string
*
* @see http://site.trimplement.com/using-spring-and-quartz-with-jobstore-properties/
* @see http://forum.springsource.org/showthread.php?130984-quartz-error-ioexception
*/
public class persistablecrontriggerfactorybean extends crontriggerfactorybean {
@override
public void afterpropertiesset() {
super.afterpropertiesset();
//remove the jobdetail element
getjobdatamap().remove(jobdetailawaretrigger.job_detail_key);
}
}
建表语句,mysql:quartztables.sql
#
# quartz seems to work best with the driver mm.mysql-2.0.7-bin.jar
#
# in your quartz properties file, you'll need to set
# org.quartz.jobstore.driverdelegateclass = org.quartz.impl.jdbcjobstore.stdjdbcdelegate
#
drop table if exists qrtz_job_listeners;
drop table if exists qrtz_trigger_listeners;
drop table if exists qrtz_fired_triggers;
drop table if exists qrtz_paused_trigger_grps;
drop table if exists qrtz_scheduler_state;
drop table if exists qrtz_locks;
drop table if exists qrtz_simple_triggers;
drop table if exists qrtz_cron_triggers;
drop table if exists qrtz_blob_triggers;
drop table if exists qrtz_triggers;
drop table if exists qrtz_job_details;
drop table if exists qrtz_calendars;
create table qrtz_job_details
(
job_name varchar(200) not null,
job_group varchar(200) not null,
description varchar(250) null,
job_class_name varchar(250) not null,
is_durable varchar(1) not null,
is_volatile varchar(1) not null,
is_stateful varchar(1) not null,
requests_recovery varchar(1) not null,
job_data blob null,
primary key (job_name,job_group)
);
create table qrtz_job_listeners
(
job_name varchar(200) not null,
job_group varchar(200) not null,
job_listener varchar(200) not null,
primary key (job_name,job_group,job_listener),
foreign key (job_name,job_group)
references qrtz_job_details(job_name,job_group)
);
create table qrtz_triggers
(
trigger_name varchar(200) not null,
trigger_group varchar(200) not null,
job_name varchar(200) not null,
job_group varchar(200) not null,
is_volatile varchar(1) not null,
description varchar(250) null,
next_fire_time bigint(13) null,
prev_fire_time bigint(13) null,
priority integer null,
trigger_state varchar(16) not null,
trigger_type varchar(8) not null,
start_time bigint(13) not null,
end_time bigint(13) null,
calendar_name varchar(200) null,
misfire_instr smallint(2) null,
job_data blob null,
primary key (trigger_name,trigger_group),
foreign key (job_name,job_group)
references qrtz_job_details(job_name,job_group)
);
create table qrtz_simple_triggers
(
trigger_name varchar(200) not null,
trigger_group varchar(200) not null,
repeat_count bigint(7) not null,
repeat_interval bigint(12) not null,
times_triggered bigint(10) not null,
primary key (trigger_name,trigger_group),
foreign key (trigger_name,trigger_group)
references qrtz_triggers(trigger_name,trigger_group)
);
create table qrtz_cron_triggers
(
trigger_name varchar(200) not null,
trigger_group varchar(200) not null,
cron_expression varchar(200) not null,
time_zone_id varchar(80),
primary key (trigger_name,trigger_group),
foreign key (trigger_name,trigger_group)
references qrtz_triggers(trigger_name,trigger_group)
);
create table qrtz_blob_triggers
(
trigger_name varchar(200) not null,
trigger_group varchar(200) not null,
blob_data blob null,
primary key (trigger_name,trigger_group),
foreign key (trigger_name,trigger_group)
references qrtz_triggers(trigger_name,trigger_group)
);
create table qrtz_trigger_listeners
(
trigger_name varchar(200) not null,
trigger_group varchar(200) not null,
trigger_listener varchar(200) not null,
primary key (trigger_name,trigger_group,trigger_listener),
foreign key (trigger_name,trigger_group)
references qrtz_triggers(trigger_name,trigger_group)
);
create table qrtz_calendars
(
calendar_name varchar(200) not null,
calendar blob not null,
primary key (calendar_name)
);
create table qrtz_paused_trigger_grps
(
trigger_group varchar(200) not null,
primary key (trigger_group)
);
create table qrtz_fired_triggers
(
entry_id varchar(95) not null,
trigger_name varchar(200) not null,
trigger_group varchar(200) not null,
is_volatile varchar(1) not null,
instance_name varchar(200) not null,
fired_time bigint(13) not null,
priority integer not null,
state varchar(16) not null,
job_name varchar(200) null,
job_group varchar(200) null,
is_stateful varchar(1) null,
requests_recovery varchar(1) null,
primary key (entry_id)
);
create table qrtz_scheduler_state
(
instance_name varchar(200) not null,
last_checkin_time bigint(13) not null,
checkin_interval bigint(13) not null,
primary key (instance_name)
);
create table qrtz_locks
(
lock_name varchar(40) not null,
primary key (lock_name)
);
insert into qrtz_locks values('trigger_access');
insert into qrtz_locks values('job_access');
insert into qrtz_locks values('calendar_access');
insert into qrtz_locks values('state_access');
insert into qrtz_locks values('misfire_access');
commit;
参考:
quartz集成springmvc 的方案二(持久化任务、集群和分布式)
]]>