Distributed scheduling QUARTZ+SPRING

Home Open Source Projects Questions and Answers Dynamic Blog Translation Information Special Activity Recruitment
[ Login | Register]
Blog Zone > Blog > Blog Details

Distributed Scheduling QUARTZ+SPRING
Published 2 years ago
  
to Distributed Scheduling QUARTZ+SPRING
Favorites
Introduced 
in 2 years ago Read 433 Favorites 22 Likes 0 Comments 0
Abstract: Distributed scheduling QUARTZ+SPRING
uses SPRING's timed task framework. If it is in a distributed environment, because there are multiple nodes, it will The same task will be executed by multiple nodes. At this time, distributed QUARTZ needs to be introduced.
Trigger: Storage time schedule
Task business code
Scheduler: Responsible for scheduling, that is, executing the corresponding task at the specified time

If it is a distributed QUARTZ, each node will report the task, store it in the database, and execute it from The trigger is taken out of the database for execution. If the trigger name and execution time are the same, there is only one node to execute this task.
If this node fails, the task will be dispatched to another node for execution.

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" >


     <!-- Add timer configuration -->
     <!- - Thread executor configuration for task registration -->
     < 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 >

     <!-- set scheduler -->
     < 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:
* It should be used for persistence and other feature operations, and QuartzJobBean needs to be inherited.
* <br>Because it needs to be persisted, it cannot store xxxxManager-like objects.
* It can only be extracted from the ApplicationContext injected from QuartzJobBean every time.
*
* </ 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;
    }
}


Since the JOB needs to be stored in the database, there will be a PROPERTY problem, which needs to be eliminated JOB-DATA, this file is required 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;
Original address: http://www.blogjava.net/paulwong/archive/2014/11/14/420104.htmlCategories
: Word count of work log: 1362 Rewards
Like Favorites Share Yingpoong Follow Yingpo project manager Nanjing Fans 37 Blog posts 198 Total code words 16947 Related blogs Distributed task scheduling platform Antares ihaolin 128 0 python distributed scheduling celery cannot be started Or automatically exit the troubleshooting steps testwork 97 0 redis distributed locks and scheduling locks Redis-Java 67 0 Comments (0) Ctrl+Enter Post Comment   Top Community Open Source Project Technical Q&A Move







 
 
 
 
 
 
 
 
 
 
 
 
 


























Blog
Open Source Information
Technical Translation
Topics
Recruitment
Crowdsourcing

Project Hall
Software and Services
Earn Money from
Code Cloud

Git Code Hosting
Team
PaaS
Online Tools
Activities

Activities
Launch Activities
Yuanchuanghui
Follow WeChat Official Account

Follow WeChat
Official Account Download Mobile Client

Download Mobile Client End
© Open Source China (OSChina.NET)
About Us
Advertising Contact
@Sina Weibo
Partners
Open Source China Community is the official community designated by the Open Source Software Promotion Alliance of the Ministry of Industry and Information Technology 粤ICP备12009483号-3 Copyright Shenzhen Osi Network Technology Co., Ltd.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326340974&siteId=291194637