quartz定时任务搭建

项目使用的框架是springboot,数据库使用PostgreSQL,搭建步骤如下:
1.maven依赖

<dependency>
       <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-quartz</artifactId>
      <version>2.3.3.RELEASE</version>
</dependency>

2.脚本
SQL脚本:

-- Quartz
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 character varying(120) NOT NULL,
                                  JOB_NAME character varying(200) NOT NULL,
                                  JOB_GROUP character varying(200) NOT NULL,
                                  DESCRIPTION character varying(250) NULL,
                                  JOB_CLASS_NAME character varying(250) NOT NULL,
                                  IS_DURABLE boolean NOT NULL,
                                  IS_NONCONCURRENT boolean NOT NULL,
                                  IS_UPDATE_DATA boolean NOT NULL,
                                  REQUESTS_RECOVERY boolean NOT NULL,
                                  JOB_DATA bytea NULL
);
alter table QRTZ_JOB_DETAILS add primary key(SCHED_NAME,JOB_NAME,JOB_GROUP);

CREATE TABLE QRTZ_TRIGGERS (
                               SCHED_NAME character varying(120) NOT NULL,
                               TRIGGER_NAME character varying(200) NOT NULL,
                               TRIGGER_GROUP character varying(200) NOT NULL,
                               JOB_NAME character varying(200) NOT NULL,
                               JOB_GROUP character varying(200) NOT NULL,
                               DESCRIPTION character varying(250) NULL,
                               NEXT_FIRE_TIME BIGINT NULL,
                               PREV_FIRE_TIME BIGINT NULL,
                               PRIORITY INTEGER NULL,
                               TRIGGER_STATE character varying(16) NOT NULL,
                               TRIGGER_TYPE character varying(8) NOT NULL,
                               START_TIME BIGINT NOT NULL,
                               END_TIME BIGINT NULL,
                               CALENDAR_NAME character varying(200) NULL,
                               MISFIRE_INSTR SMALLINT NULL,
                               JOB_DATA bytea NULL
);
alter table QRTZ_TRIGGERS add primary key(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);

CREATE TABLE QRTZ_SIMPLE_TRIGGERS (
                                      SCHED_NAME character varying(120) NOT NULL,
                                      TRIGGER_NAME character varying(200) NOT NULL,
                                      TRIGGER_GROUP character varying(200) NOT NULL,
                                      REPEAT_COUNT BIGINT NOT NULL,
                                      REPEAT_INTERVAL BIGINT NOT NULL,
                                      TIMES_TRIGGERED BIGINT NOT NULL
);
alter table QRTZ_SIMPLE_TRIGGERS add primary key(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);

CREATE TABLE QRTZ_CRON_TRIGGERS (
                                    SCHED_NAME character varying(120) NOT NULL,
                                    TRIGGER_NAME character varying(200) NOT NULL,
                                    TRIGGER_GROUP character varying(200) NOT NULL,
                                    CRON_EXPRESSION character varying(120) NOT NULL,
                                    TIME_ZONE_ID character varying(80)
);
alter table QRTZ_CRON_TRIGGERS add primary key(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);

CREATE TABLE QRTZ_SIMPROP_TRIGGERS (
                                       SCHED_NAME character varying(120) NOT NULL,
                                       TRIGGER_NAME character varying(200) NOT NULL,
                                       TRIGGER_GROUP character varying(200) NOT NULL,
                                       STR_PROP_1 character varying(512) NULL,
                                       STR_PROP_2 character varying(512) NULL,
                                       STR_PROP_3 character varying(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 boolean NULL,
                                       BOOL_PROP_2 boolean NULL
);
alter table QRTZ_SIMPROP_TRIGGERS add primary key(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);

CREATE TABLE QRTZ_BLOB_TRIGGERS (
                                    SCHED_NAME character varying(120) NOT NULL,
                                    TRIGGER_NAME character varying(200) NOT NULL,
                                    TRIGGER_GROUP character varying(200) NOT NULL,
                                    BLOB_DATA bytea NULL
);
alter table QRTZ_BLOB_TRIGGERS add primary key(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP);

CREATE TABLE QRTZ_CALENDARS (
                                SCHED_NAME character varying(120) NOT NULL,
                                CALENDAR_NAME character varying(200) NOT NULL,
                                CALENDAR bytea NOT NULL
);
alter table QRTZ_CALENDARS add primary key(SCHED_NAME,CALENDAR_NAME);

CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS (
                                          SCHED_NAME character varying(120) NOT NULL,
                                          TRIGGER_GROUP character varying(200) NOT NULL
);
alter table QRTZ_PAUSED_TRIGGER_GRPS add primary key(SCHED_NAME,TRIGGER_GROUP);

CREATE TABLE QRTZ_FIRED_TRIGGERS (
                                     SCHED_NAME character varying(120) NOT NULL,
                                     ENTRY_ID character varying(95) NOT NULL,
                                     TRIGGER_NAME character varying(200) NOT NULL,
                                     TRIGGER_GROUP character varying(200) NOT NULL,
                                     INSTANCE_NAME character varying(200) NOT NULL,
                                     FIRED_TIME BIGINT NOT NULL,
                                     SCHED_TIME BIGINT NOT NULL,
                                     PRIORITY INTEGER NOT NULL,
                                     STATE character varying(16) NOT NULL,
                                     JOB_NAME character varying(200) NULL,
                                     JOB_GROUP character varying(200) NULL,
                                     IS_NONCONCURRENT boolean NULL,
                                     REQUESTS_RECOVERY boolean NULL
);
alter table QRTZ_FIRED_TRIGGERS add primary key(SCHED_NAME,ENTRY_ID);

CREATE TABLE QRTZ_SCHEDULER_STATE (
                                      SCHED_NAME character varying(120) NOT NULL,
                                      INSTANCE_NAME character varying(200) NOT NULL,
                                      LAST_CHECKIN_TIME BIGINT NOT NULL,
                                      CHECKIN_INTERVAL BIGINT NOT NULL
);
alter table QRTZ_SCHEDULER_STATE add primary key(SCHED_NAME,INSTANCE_NAME);

CREATE TABLE QRTZ_LOCKS (
                            SCHED_NAME character varying(120) NOT NULL,
                            LOCK_NAME character varying(40) NOT NULL
);
alter table QRTZ_LOCKS add primary key(SCHED_NAME,LOCK_NAME);

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);

3.job实现
实现job接口,当任务触发时会调用

package com.alibaba.atlas.quartz;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 滚动计划任务
 */
public class RollingPlanUpdateJob implements Job {

    private static final Logger logger = LoggerFactory.getLogger(RollingPlanUpdateJob.class);

    @Override
    public void execute(JobExecutionContext context) {
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();

        //取出业务需要的数据
        String planId = dataMap.getString("planId");

        //进行业务处理
        .....
    }
}

4.job增删改

package com.alibaba.atlas.quartz;

import com.guandata.atlas.exception.AtlasException;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QuartzService {

    private static final Logger logger = LoggerFactory.getLogger(QuartzService.class);

    @Autowired
    private Scheduler scheduler;

    //增加或者修改
    public void updateJob(JobKey jobKey, TriggerKey triggerKey, JobDataMap dataMap, String cronExpression) {
        try {
            //job
            JobDetail jobDetail = JobBuilder.newJob(RollingPlanUpdateJob.class)
                    .withIdentity(jobKey)
                    .usingJobData(dataMap)
                    .build();
            //trigger
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(triggerKey)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
                    .build();
            //create or update
            if (scheduler.checkExists(jobKey)) {
                scheduler.rescheduleJob(triggerKey, trigger);
                logger.info("reschedule job: {}, cronExpression: {}", jobKey, cronExpression);
            } else {
                scheduler.scheduleJob(jobDetail, trigger);
                logger.info("schedule job: {}, cronExpression: {}", jobKey, cronExpression);
            }
        } catch (Exception ex) {
            logger.error("add job failed", ex);
            throw new AtlasException("add job failed:" + ex.getMessage());
        }
    }

    //暂停
    public void pauseJobIfExists(JobKey jobKey) {
        try {
            if (!scheduler.checkExists(jobKey)) {
                logger.info("job not exists,jobName={}", jobKey.getName());
                return;
            }
            scheduler.pauseJob(jobKey);
            logger.info("job exists,pause job success,jobName={}", jobKey.getName());
        } catch (Exception ex) {
            logger.error("pause job failed", ex);
            throw new AtlasException("pause job failed:" + ex.getMessage());
        }
    }

    //删除
    public void deleteJobIfExists(JobKey jobKey) {
        try {
            if (!scheduler.checkExists(jobKey)) {
                logger.info("job not exists, jobName={}", jobKey.getName());
                return;
            }
            scheduler.deleteJob(jobKey);
            logger.info("job exists,delete job success,jobName={}", jobKey.getName());
        } catch (Exception ex) {
            logger.error("delete job failed", ex);
            throw new AtlasException("delete job failed:" + ex.getMessage());
        }
    }
}

使用:

//业务唯一标识,这个根据具体业务需要具体的数据,可能是一个配置等
 String planId = UUID.randomUUID().toString();
 JobDataMap map = new JobDataMap();
 map.put("planId", planId);
JobKey jobKey = new JobKey("ROLLING_PLAN_" + planId, "ROLLING_PLAN");
 TriggerKey triggerKey = new TriggerKey("ROLLING_PLAN_" + planId, "ROLLING_PLAN");
 quartzService.updateJob(RollingPlanUpdateJob.class, jobKey, triggerKey, map, " 0 0 3 * * ?");

job和trigger是一对多的关系,jobkey和triggerkey中的name和group,在同一个分组下不同的任务或者触发器的name不要相同,最好根据业务标识设置成唯一,这样避免数据的覆盖。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,456评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,763评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,265评论 0 238
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,716评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,071评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,435评论 1 212
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,751评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,437评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,148评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,411评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,912评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,281评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,913评论 3 234
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,020评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,776评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,462评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,350评论 2 262

推荐阅读更多精彩内容