• Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)


    Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)

     

    转载:http://www.cnblogs.com/jiafuwei/p/6145280.html

    单个Quartz实例能给予你很好的Job调度能力,但它不能满足典型的企业需求,如可伸缩性、高可靠性满足。假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一部分了。使用 Quartz 的集群能力可以更好的支持你的业务需求,并且即使是其中一台机器在最糟的时间崩溃了也能确保所有的 Job 得到执行。

    Quartz 中集群如何工作

    一个 Quartz 集群中的每个节点是一个独立的 Quartz 应用,它又管理着其他的节点。意思是你必须对每个节点分别启动或停止。不像许多应用服务器的集群,独立的 Quartz 节点并不与另一其的节点或是管理节点通信。Quartz 应用是通过数据库表来感知到另一应用的。

    图:表示了每个节点直接与数据库通信,若离开数据库将对其他节点一无所知

    创建Quartz数据库表

    因为Quartz 集群依赖于数据库,所以必须首先创建Quartz数据库表。Quartz 包括了所有被支持的数据库平台的 SQL 脚本。解压 Quartz 分发包后的目录,在 /docs/dbTables 目录下找到那些 SQL 脚本。
    总共11张表,不同版本,表个数可能不同。数据库为mysql,用tables_mysql_innodb.sql创建数据库表。

     

    复制代码
    #
    # In your Quartz properties file, you'll need to set 
    # org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
    #
    #
    # By: Ron Cordell - roncordell
    #  I didn't see this anywhere, so I thought I'd post it here. This is the script from Quartz to create the tables in a MySQL database, modified to use INNODB instead of MYISAM.
    
    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_SIMPROP_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(
    SCHED_NAME VARCHAR(120) NOT NULL,
    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_NONCONCURRENT VARCHAR(1) NOT NULL,
    IS_UPDATE_DATA VARCHAR(1) NOT NULL,
    REQUESTS_RECOVERY VARCHAR(1) NOT NULL,
    JOB_DATA BLOB NULL,
    PRIMARY KEY (SCHED_NAME,JOB_NAME,JOB_GROUP))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_TRIGGERS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    JOB_NAME VARCHAR(200) NOT NULL,
    JOB_GROUP VARCHAR(200) 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 (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,JOB_NAME,JOB_GROUP)
    REFERENCES QRTZ_JOB_DETAILS(SCHED_NAME,JOB_NAME,JOB_GROUP))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_SIMPLE_TRIGGERS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    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 (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
    REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_CRON_TRIGGERS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    CRON_EXPRESSION VARCHAR(120) NOT NULL,
    TIME_ZONE_ID VARCHAR(80),
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
    REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_SIMPROP_TRIGGERS
      (          
        SCHED_NAME VARCHAR(120) NOT NULL,
        TRIGGER_NAME VARCHAR(200) NOT NULL,
        TRIGGER_GROUP VARCHAR(200) NOT NULL,
        STR_PROP_1 VARCHAR(512) NULL,
        STR_PROP_2 VARCHAR(512) NULL,
        STR_PROP_3 VARCHAR(512) NULL,
        INT_PROP_1 INT NULL,
        INT_PROP_2 INT NULL,
        LONG_PROP_1 BIGINT NULL,
        LONG_PROP_2 BIGINT NULL,
        DEC_PROP_1 NUMERIC(13,4) NULL,
        DEC_PROP_2 NUMERIC(13,4) NULL,
        BOOL_PROP_1 VARCHAR(1) NULL,
        BOOL_PROP_2 VARCHAR(1) NULL,
        PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
        FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) 
        REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_BLOB_TRIGGERS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    BLOB_DATA BLOB NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP),
    INDEX (SCHED_NAME,TRIGGER_NAME, TRIGGER_GROUP),
    FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP)
    REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_CALENDARS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    CALENDAR_NAME VARCHAR(200) NOT NULL,
    CALENDAR BLOB NOT NULL,
    PRIMARY KEY (SCHED_NAME,CALENDAR_NAME))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_FIRED_TRIGGERS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    ENTRY_ID VARCHAR(95) NOT NULL,
    TRIGGER_NAME VARCHAR(200) NOT NULL,
    TRIGGER_GROUP VARCHAR(200) NOT NULL,
    INSTANCE_NAME VARCHAR(200) NOT NULL,
    FIRED_TIME BIGINT(13) NOT NULL,
    SCHED_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_NONCONCURRENT VARCHAR(1) NULL,
    REQUESTS_RECOVERY VARCHAR(1) NULL,
    PRIMARY KEY (SCHED_NAME,ENTRY_ID))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_SCHEDULER_STATE (
    SCHED_NAME VARCHAR(120) NOT NULL,
    INSTANCE_NAME VARCHAR(200) NOT NULL,
    LAST_CHECKIN_TIME BIGINT(13) NOT NULL,
    CHECKIN_INTERVAL BIGINT(13) NOT NULL,
    PRIMARY KEY (SCHED_NAME,INSTANCE_NAME))
    ENGINE=InnoDB;
    
    CREATE TABLE QRTZ_LOCKS (
    SCHED_NAME VARCHAR(120) NOT NULL,
    LOCK_NAME VARCHAR(40) NOT NULL,
    PRIMARY KEY (SCHED_NAME,LOCK_NAME))
    ENGINE=InnoDB;
    
    CREATE INDEX IDX_QRTZ_J_REQ_RECOVERY ON QRTZ_JOB_DETAILS(SCHED_NAME,REQUESTS_RECOVERY);
    CREATE INDEX IDX_QRTZ_J_GRP ON QRTZ_JOB_DETAILS(SCHED_NAME,JOB_GROUP);
    
    CREATE INDEX IDX_QRTZ_T_J ON QRTZ_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP);
    CREATE INDEX IDX_QRTZ_T_JG ON QRTZ_TRIGGERS(SCHED_NAME,JOB_GROUP);
    CREATE INDEX IDX_QRTZ_T_C ON QRTZ_TRIGGERS(SCHED_NAME,CALENDAR_NAME);
    CREATE INDEX IDX_QRTZ_T_G ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP);
    CREATE INDEX IDX_QRTZ_T_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE);
    CREATE INDEX IDX_QRTZ_T_N_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP,TRIGGER_STATE);
    CREATE INDEX IDX_QRTZ_T_N_G_STATE ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_GROUP,TRIGGER_STATE);
    CREATE INDEX IDX_QRTZ_T_NEXT_FIRE_TIME ON QRTZ_TRIGGERS(SCHED_NAME,NEXT_FIRE_TIME);
    CREATE INDEX IDX_QRTZ_T_NFT_ST ON QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_STATE,NEXT_FIRE_TIME);
    CREATE INDEX IDX_QRTZ_T_NFT_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME);
    CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_STATE);
    CREATE INDEX IDX_QRTZ_T_NFT_ST_MISFIRE_GRP ON QRTZ_TRIGGERS(SCHED_NAME,MISFIRE_INSTR,NEXT_FIRE_TIME,TRIGGER_GROUP,TRIGGER_STATE);
    
    CREATE INDEX IDX_QRTZ_FT_TRIG_INST_NAME ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME);
    CREATE INDEX IDX_QRTZ_FT_INST_JOB_REQ_RCVRY ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,INSTANCE_NAME,REQUESTS_RECOVERY);
    CREATE INDEX IDX_QRTZ_FT_J_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_NAME,JOB_GROUP);
    CREATE INDEX IDX_QRTZ_FT_JG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,JOB_GROUP);
    CREATE INDEX IDX_QRTZ_FT_T_G ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);
    CREATE INDEX IDX_QRTZ_FT_TG ON QRTZ_FIRED_TRIGGERS(SCHED_NAME,TRIGGER_GROUP);
    
    commit;
    复制代码

    配置 Quartz 使用集群

    1.配置节点的 quartz.properties 文件

    复制代码
    #==============================================================
    #Configure Main Scheduler Properties
    #==============================================================
    org.quartz.scheduler.instanceName = defaultScheduler
    org.quartz.scheduler.instanceId = AUTO
    
    #==============================================================
    #Configure JobStore
    #==============================================================
    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.isClustered = true
    org.quartz.jobStore.clusterCheckinInterval = 20000
    org.quartz.jobStore.dataSource = myDS
    org.quartz.jobStore.maxMisfiresToHandleAtATime = 1
    org.quartz.jobStore.misfireThreshold = 120000
    org.quartz.jobStore.txIsolationLevelSerializable = true
    
    #==============================================================
    #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
    
    #==============================================================
    #Skip Check Update
    #update:true
    #not update:false
    #==============================================================
    org.quartz.scheduler.skipUpdateCheck = true
    
    #============================================================================
    # Configure Plugins
    #============================================================================
    org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
    org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
    org.quartz.plugin.shutdownhook.cleanShutdown = true
    复制代码

    org.quartz.scheduler.instanceName    属性可为任何值,用在 JDBC JobStore 中来唯一标识实例,但是所有集群节点中必须相同。
    org.quartz.scheduler.instanceId       属性为 AUTO即可,基于主机名和时间戳来产生实例 ID。
    org.quartz.jobStore.class          属性为 JobStoreTX,将任务持久化到数据中。因为集群中节点依赖于数据库来传播 Scheduler 实例的状态,你只能在使用 JDBC JobStore 时应用 Quartz 集群。这意味着你必须使用 JobStoreTX 或是 JobStoreCMT 作为 Job 存储;你不能在集群中使用 RAMJobStore。
    org.quartz.jobStore.isClustered          属性为 true,你就告诉了 Scheduler 实例要它参与到一个集群当中。这一属性会贯穿于调度框架的始终,用于修改集群环境中操作的默认行为。
    org.quartz.jobStore.clusterCheckinInterval   属性定义了Scheduler 实例检入到数据库中的频率(单位:毫秒)。Scheduler 检查是否其他的实例到了它们应当检入的时候未检入;这能指出一个失败的 Scheduler 实例,且当前 Scheduler 会以此来接管任何执行失败并可恢复的 Job。通过检入操作,Scheduler 也会更新自身的状态记录。clusterChedkinInterval 越小,Scheduler 节点检查失败的 Scheduler 实例就越频繁。默认值是 15000 (即15 秒)。

    2.配置applicationContext-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">
        <bean id="myJobFactory" class="com.xiyinli.Timed.MyJobFactory"></bean>
        <bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="jobFactory" ref="myJobFactory"/>
            <property name="dataSource" ref="dataSource"/>
            <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
            <property name="configLocation" value="classpath:quartz.properties" />
            <property name="triggers">
                <list>
                    <ref bean="orderTimedTrigger" />
                    <ref bean="bonusTimedTrigger" />
                    <ref bean="cltWeightedCommissionTimedTrigger" />
                    <ref bean="emptyVisitOfTodayTimedTrigger" />
                    <ref bean="userCentCommissionTimedTrigger" />
                </list>
            </property>
        </bean>
    
        <!-- quartz1 -->
        <bean id="orderTimedClass" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass">
                <value>com.xiyinli.Timed.OrderTimed</value>
            </property>
            <property name="durability" value="true" />
            <property name="requestsRecovery" value="true" />
        </bean>
        <bean id="orderTimedTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="orderTimedClass" />
            <property name="cronExpression" value="0 0/5 * * * ?" /> <!-- //每5分钟执行一次 -->
        </bean>
    
        <!-- quartz2 -->
        <bean id="bonusTimedClass" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass">
                <value>com.xiyinli.Timed.BonusTimed</value>
            </property>
            <property name="durability" value="true" />
            <property name="requestsRecovery" value="true" />
        </bean>
        <bean id="bonusTimedTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="bonusTimedClass" />
            <property name="cronExpression" value="0 05 00 1 * ?" /> <!-- //每月一号00:05分执行一次 -->
        </bean>
    
        <!-- quartz3 -->
        <bean id="cltWeightedCommissionTimedClass" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass">
                <value>com.xiyinli.Timed.CltWeightedCommissionTimed</value>
            </property>
            <property name="durability" value="true" />
            <property name="requestsRecovery" value="true" />
        </bean>
        <bean id="cltWeightedCommissionTimedTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="cltWeightedCommissionTimedClass" />
            <property name="cronExpression" value="0 0 1 * * ?" /> <!-- //"0 0 1 * * ?" 每天1点,统计上一天的营业额 -->
        </bean>
    
        <!-- quartz4 -->
        <bean id="emptyVisitOfTodayTimedClass" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass">
                <value>com.xiyinli.Timed.EmptyVisitOfTodayTimed</value>
            </property>
            <property name="durability" value="true" />
            <property name="requestsRecovery" value="true" />
        </bean>
        <bean id="emptyVisitOfTodayTimedTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="emptyVisitOfTodayTimedClass" />
            <property name="cronExpression" value="0 10 0 * * ?" /> <!-- ///每天00:10点,统计上一天的营业额 -->
        </bean>
    
        <!-- quartz5 -->
        <bean id="userCentCommissionTimedClass" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
            <property name="jobClass">
                <value>com.xiyinli.Timed.UserCentCommissionTimed</value>
            </property>
            <property name="durability" value="true" />
            <property name="requestsRecovery" value="true" />
        </bean>
        <bean id="userCentCommissionTimedTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <property name="jobDetail" ref="userCentCommissionTimedClass" />
            <property name="cronExpression" value="0 0 3 * * ?" /> <!-- //0 0 3 * * ? 每天凌晨3点 分佣解冻 -->
        </bean>
    
    
    </beans>
    复制代码

    dataSource:项目中用到的数据源,里面包含了quartz用到的11张数据库表;

    applicationContextSchedulerContextKey: 是org.springframework.scheduling.quartz.SchedulerFactoryBean这个类中把spring上下 文以key/value的方式存放在了SchedulerContext中了,可以用applicationContextSchedulerContextKey所 定义的key得到对应spring 的ApplicationContext; 

    configLocation:用于指明quartz的配置文件的位置

    requestsRecovery
    requestsRecovery属性必须设置为 true,当Quartz服务被中止后,再次启动或集群中其他机器接手任务时会尝试恢复执行之前未完成的所有任务。

    <bean id="myJobFactory" class="com.xiyinli.Timed.MyJobFactory"></bean>为重写的bean管理工厂类方便定时类service注入bean:

    复制代码
    package com.xiyinli.Timed;
    
    import org.quartz.spi.TriggerFiredBundle;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
    import org.springframework.scheduling.quartz.AdaptableJobFactory;
    
    /**
     * Created by Administrator on 2017/8/16 0016.
     */
    public class MyJobFactory extends AdaptableJobFactory {
        //这个对象Spring会帮我们自动注入进来,也属于Spring技术范畴.
        @Autowired
        private AutowireCapableBeanFactory capableBeanFactory;
    
        protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
            //调用父类的方法
            Object jobInstance = super.createJobInstance(bundle);
            //进行注入,这属于Spring的技术,不清楚的可以查看Spring的API.
            capableBeanFactory.autowireBean(jobInstance);
            return jobInstance;
        }
    }
    复制代码

    3.把applicationContext-quartz.xml引入spring, applicationContext-quartz.xml中的dataSource为上面spring-mybatis.xml的bean数据库配置

    4. 上面的配置是多任务多定时器,这里以其中的 com.xiyinli.Timed.EmptyVisitOfTodayTimed 为例,其他4个一样编写。

    复制代码
    package com.xiyinli.Timed;
    
    import com.xiyinli.mapper.StoreMapper;
    import com.xiyinli.service.StoreService;
    import org.apache.log4j.Logger;
    import org.quartz.DisallowConcurrentExecution;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.quartz.PersistJobDataAfterExecution;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import javax.annotation.Resource;
    
    /**
     * Created by Administrator on 2017/8/2.
     */
    @Service
    @Transactional
    @PersistJobDataAfterExecution
    @DisallowConcurrentExecution// 不允许并发执行
    public class EmptyVisitOfTodayTimed extends QuartzJobBean {
    
        private Logger log = Logger.getLogger(this.getClass());
        private static boolean isRun = false;
        @Autowired
        private StoreMapper storeMapper;
    
        @Override
        protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            if (isRun) {
                log.debug("EmptyVisitOfTodayTimed-前一次未执行完,跳过本次任务!");
                return;
            }
            isRun = true;
    
            commissionTask();
    
            isRun = false;
            log.debug("执行逻辑-EmptyVisitOfTodayTimed-isRunFinish");
        }
        //------------------------------------------------------------------------------------------------------------------
    
        public void commissionTask() {
            log.debug("凌晨00:10清空每日访问量");
            Boolean b = storeMapper.updateTodayVisit();
            if (b){
                log.debug("正常清空");
            }else{
                log.debug("清空失败");
            }
            log.debug("清空每日访问量执行结束");
        }
    
    
    
    }
    复制代码

    5.在最后测试时发现,windows环境运行正常,不是到linux环境下启动时会出现如下错误:

    ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed  

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzScheduler' defined ...

    Caused by: Java.lang.IllegalStateException: Cannot run without an instance id.

    可以尝试使用下面的办法进行解决(一般情况是不能识别主机名):

    先查找主机名复制下来:

    [root@iZbp12b0psb88pmxixiwcsZ /]# hostname
    iZbp12b0psb88pmxixiwcsZ

    然后:

    [root@iZbp12b0psb88pmxixiwcsZ /]# vi /etc/hosts

    编辑如下即可:

     转载:http://www.cnblogs.com/007sx/p/7374095.html

    http://www.cnblogs.com/jiafuwei/p/6145280.html

    Spring—Quartz定时调度CronTrigger时间配置格式说明与实例   http://blog.csdn.net/foamflower/article/details/4260645

  • 相关阅读:
    Cognitive Radio Cognitive Network Simulator (NS3 based)
    RCU(Read-Copy Update)synchronize原理分析
    英雄黑客:一个试图提高物联网安全性的“义务警员”
    Android NDK放弃GCC,全面转向Clang
    Why is FreeBSD deprecating GCC in favor of Clang/LLVM?
    (Low Level Virtual Machine) LLVM 与 Clang介绍
    RMS谈GCC、LLVM和Copyleft
    LLVM和Clang背后的故事
    FEP: 测试 lineage, 获得 CPU, MEMORY 统计信息
    以wifi-example-sim.cc为例说明NS3统计数据模型
  • 原文地址:https://www.cnblogs.com/J-Cooper/p/7779352.html
Copyright © 2020-2023  润新知