- 浏览: 6537 次
- 性别:
- 来自: 北京
文章分类
最新评论
首页 开源项目 问答 动弹 博客 翻译 资讯 专题 活动 招聘
[ 登录 | 注册 ]
博客专区 > 引鸩怼孑的博客 > 博客详情
分布式调度QUARTZ+SPRING
引鸩怼孑 发表于2年前
转 分布式调度QUARTZ+SPRING
收藏
引鸩怼孑
发表于 2年前 阅读 433 收藏 22 点赞 0 评论 0
摘要: 分布式调度QUARTZ+SPRING
使用SPRING的定时任务框架,如果是在分布式的环境下,由于有多台节点,会产生相同的任务,会被多个节点执行,这时需引入分布式的QUARTZ。
触发器:存放时间排程
任务:蔟业务代码
排程器:负责调度,即在指定的时间执行对应的任务
如果是分布式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" />
<!-- This name is persisted as SCHED_NAME in db. for local testing could
change to unique name to avoid collision with dev server -->
< property name ="schedulerName" value ="quartzScheduler" />
<!-- Will update database cron triggers to what is in this jobs file on
each deploy. Replaces all previous trigger and job data that was in the database.
YMMV -->
< 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" />
<!-- fail-over 重写执行失败的任务,default=false -->
< 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;
/**
* <p>Title:SpringQuertzClusterTaskSchedulerTester</p>
* <p>Description:
* 应为要持久化等特性操作,需要继承 QuartzJobBean
* <br>由于要被持久化,所以不能存放xxxxManager类似对象,
* 只能从每次从QuartzJobBean注入的ApplicationContext 中去取出
*
* </p>
*
*
*/
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;
原文地址:http://www.blogjava.net/paulwong/archive/2014/11/14/420104.html
分类:工作日志 字数:1362
打赏
点赞
收藏
分享
引鸩怼孑
+ 关注
引鸩怼孑
项目经理 南京
粉丝 37 博文 198 码字总数 16947
相关博客
分布式任务调度平台Antares
ihaolin
128 0
python分布式调度celery无法启动或自动退出排查步骤
testwork
97 0
redis分布式锁和调度锁
收废品的-Java
67 0
评论 (0)
Ctrl+Enter 发表评论
顶部
社区
开源项目
技术问答
动弹
博客
开源资讯
技术翻译
专题
招聘
众包
项目大厅
软件与服务
接活赚钱
码云
Git代码托管
Team
PaaS
在线工具
活动
线下活动
发起活动
源创会
关注微信公众号
关注微信公众号
下载手机客户端
下载手机客户端
©开源中国(OSChina.NET)
关于我们
广告联系
@新浪微博
合作单位
开源中国社区是工信部开源软件推进联盟指定的官方社区 粤ICP备12009483号-3 深圳市奥思网络科技有限公司版权所有
[ 登录 | 注册 ]
博客专区 > 引鸩怼孑的博客 > 博客详情
分布式调度QUARTZ+SPRING
引鸩怼孑 发表于2年前
转 分布式调度QUARTZ+SPRING
收藏
引鸩怼孑
发表于 2年前 阅读 433 收藏 22 点赞 0 评论 0
摘要: 分布式调度QUARTZ+SPRING
使用SPRING的定时任务框架,如果是在分布式的环境下,由于有多台节点,会产生相同的任务,会被多个节点执行,这时需引入分布式的QUARTZ。
触发器:存放时间排程
任务:蔟业务代码
排程器:负责调度,即在指定的时间执行对应的任务
如果是分布式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" />
<!-- This name is persisted as SCHED_NAME in db. for local testing could
change to unique name to avoid collision with dev server -->
< property name ="schedulerName" value ="quartzScheduler" />
<!-- Will update database cron triggers to what is in this jobs file on
each deploy. Replaces all previous trigger and job data that was in the database.
YMMV -->
< 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" />
<!-- fail-over 重写执行失败的任务,default=false -->
< 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;
/**
* <p>Title:SpringQuertzClusterTaskSchedulerTester</p>
* <p>Description:
* 应为要持久化等特性操作,需要继承 QuartzJobBean
* <br>由于要被持久化,所以不能存放xxxxManager类似对象,
* 只能从每次从QuartzJobBean注入的ApplicationContext 中去取出
*
* </p>
*
*
*/
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;
原文地址:http://www.blogjava.net/paulwong/archive/2014/11/14/420104.html
分类:工作日志 字数:1362
打赏
点赞
收藏
分享
引鸩怼孑
+ 关注
引鸩怼孑
项目经理 南京
粉丝 37 博文 198 码字总数 16947
相关博客
分布式任务调度平台Antares
ihaolin
128 0
python分布式调度celery无法启动或自动退出排查步骤
testwork
97 0
redis分布式锁和调度锁
收废品的-Java
67 0
评论 (0)
Ctrl+Enter 发表评论
顶部
社区
开源项目
技术问答
动弹
博客
开源资讯
技术翻译
专题
招聘
众包
项目大厅
软件与服务
接活赚钱
码云
Git代码托管
Team
PaaS
在线工具
活动
线下活动
发起活动
源创会
关注微信公众号
关注微信公众号
下载手机客户端
下载手机客户端
©开源中国(OSChina.NET)
关于我们
广告联系
@新浪微博
合作单位
开源中国社区是工信部开源软件推进联盟指定的官方社区 粤ICP备12009483号-3 深圳市奥思网络科技有限公司版权所有
相关推荐
本文将深入探讨如何在分布式环境中利用Quartz和Spring构建一个高可用的集群调度系统。 一、Quartz简介 Quartz是Java平台上的作业调度库,它可以被用来创建、调度和执行计划任务。Quartz的核心是Job和Trigger。Job...
JDBC-JobStore是Quartz的一种存储策略,它利用数据库来存储作业和触发器的信息,使得在分布式环境中,多个Quartz实例能够共享相同的作业和触发器信息,实现任务的分布式调度。 以下是一些关键知识点: 1. **Quartz...
这是一个基于Spring Boot、Apache Shiro、Spring MVC、MyBatis、Quartz和Druid的数据源管理框架的示例项目,名为"renren-security"。这个DEMO提供了完整的权限管理和任务调度解决方案,下面是这些技术栈的核心知识点...
通过这个项目,开发者可以学习到如何在分布式环境中使用Quartz进行定时任务调度,以及如何利用Spring Batch进行批量数据处理。同时,了解如何将这些组件与MySQL数据库相结合,实现高可用性和可扩展性。对于希望构建...
这种方式特别适合大型分布式系统,可以实现任务的集中管理和动态调度。 在提供的压缩包文件“quartz项目资料”中,可能包含了关于如何设置Quartz与Spring集成的详细示例代码、数据库脚本、配置文件等,你可以进一步...
分布式调度框架Quartz是OpenSymphony开源组织推出的一款强大的任务调度工具,专为Java平台设计。Quartz可以方便地与其他J2EE或J2SE应用结合,也可以单独使用。它支持创建复杂的工作流,能够处理从简单到成千上万个...
Quartz是一款开源的作业调度框架,它允许...总之,Quartz与Spring Data的结合为分布式系统提供了强大的定时任务管理能力。通过深入理解这两个组件的工作原理和配置方式,开发者可以构建出稳定、可扩展的任务调度系统。
该项目计划实现通过RESTful接口,动态管理基于Http(已完成)和Thrift调用的Quartz任务(任务的 添加、查询、禁用、启用、触发)。 比如添加一个基于HTTP接口调用的定时任务,只需要向接口传递JSON数据。 - 不懂运行,...
总之,"Spring Boot + Quartz 动态管理 分布式"的主题涵盖了Spring Boot的快速开发特性,Quartz的定时任务调度能力,以及在分布式环境下的任务管理。通过学习和实践,开发者能够构建出一个灵活、健壮的定时任务系统...
10. **分布式调度**:如Quartz、CronJob,用于定时任务的执行,尤其在大数据处理、报表生成等方面。 11. **CAP原理**:在分布式系统中,一致性、可用性和分区容忍性不可兼得,需要根据业务需求进行权衡。 12. **...
Quartz是一个开源的作业调度框架,它允许应用程序在预定的时间执行任务,而Spring则是一个全面的企业级应用开发框架,提供了依赖注入、面向切面编程以及丰富的模块,如Spring MVC、Spring JDBC等。将Quartz与Spring...
分布式任务调度系统是现代软件架构中的重要组成部分,它允许在多台服务器上高效、可靠地执行预定的任务。在这个场景中,我们关注的是一个基于Maven、SSH(Spring、Struts2、Hibernate)框架、Quartz定时任务库以及...
这个"Quartz + SpringBoot 实现分布式定时任务demo"是一个示例项目,用于演示如何在分布式环境中使用Quartz和Spring Boot来管理定时任务。 首先,我们需要了解Spring Boot集成Quartz的基本步骤: 1. **依赖添加**...
在Java环境下,我们可以使用Quartz或ElasticJob等库来实现分布式调度。 三、Quartz分布式调度 Quartz是一个开源的作业调度框架,支持复杂的调度逻辑和触发器。通过实现Clustered Quartz,可以创建一个集群,使得多...
分布式调度系统选择分析,用亿图软件做的一个分布式调度系统的分析的脑图,包含,Time ,Spring 的@Scheduled,Quartz 分布式集群开源工具,基于Quartz自己开发 等几个分析
Spring整合Quartz分布式调度的示例代码 本篇文章主要介绍了Spring整合Quartz分布式调度的示例代码,主要讲解了如何使用Quartz分布式调度系统来协调每个节点执行定时任务,避免了系统资源的浪费和应用逻辑的问题。 ...
elastic job是当当网开源的基于quartz的分布式调度框架,通过zookeeper实 现分布式协调,加上支持分片、日志追踪、任务管理UI、高可性被大家熟知。 2.目前新的项目基本都是spring boot,如何通过约束、配置方式...
"Mybatis+Spring+SpringMVC+quartz多数据源切换"的架构设计就是为了满足这样的场景。这个项目结合了四个关键的技术组件,它们分别是Mybatis、Spring、SpringMVC和Quartz,下面将详细介绍这些技术以及它们在多数据源...
【标题】:基于Spring Boot和Quartz的分布式任务调度系统 在现代企业级应用中,任务调度是一项重要的功能,用于按预定的时间间隔执行特定的任务,例如数据同步、报表生成、清理过期数据等。本项目名为“schedule-...
Spring Task是Spring内置的轻量级任务调度器,而Quartz是更强大的第三方调度库,可以在Spring中集成使用。 1. **Spring Task** - **基本概念**:Spring Task是Spring框架的一部分,提供了基本的定时任务调度功能,...