spring boot整合quartz 任务信息存放到SqlServer数据库

第一步:添加依赖<!-- quartz --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-quartz</artifactId></dependency>

第二步:写定时任务public class TestTask extends QuartzJobBean {    @Override    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println("TestQuartz01----" + sdf.format(new Date()));    }}


@Configuration

public class QuartzConfig {    @Bean    public JobDetail testTask(){        return JobBuilder.newJob(TestTask.class).withIdentity("testTask").storeDurably().build();    }    @Bean    public Trigger testTestTask(){        return TriggerBuilder.newTrigger()                .forJob(testTask())                .withIdentity("testTask")                .withSchedule(CronScheduleBuilder.cronSchedule("*/5 * * * * ?"))                .build();    }}

第三步:执行SqlServer脚本--# thanks to George Papastamatopoulos for submitting this ... and Marko Lahma for--# updating it.--#--# In your Quartz properties file, you'll need to set --# org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.MSSQLDelegate--#--# you shouse enter your DB instance's name on the next line in place of "enter_db_name_here"--#--#--# From a helpful (but anonymous) Quartz user:--#--# Regarding this error message:  --#--#    [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.--#--#--#    I added "SelectMethod=cursor;" to my Connection URL in the config file. --#    It Seems to work, hopefully no side effects.--#--# example:--# "jdbc:microsoft:sqlserver://dbmachine:1433;SelectMethod=cursor"; --#--# Another user has pointed out that you will probably need to use the --# JTDS driver--#USE [enter_db_name_here]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_QRTZ_TRIGGERS_QRTZ_JOB_DETAILS]') AND OBJECTPROPERTY(id, N'ISFOREIGNKEY') = 1)ALTER TABLE [dbo].[QRTZ_TRIGGERS] DROP CONSTRAINT FK_QRTZ_TRIGGERS_QRTZ_JOB_DETAILSGOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_QRTZ_CRON_TRIGGERS_QRTZ_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISFOREIGNKEY') = 1)ALTER TABLE [dbo].[QRTZ_CRON_TRIGGERS] DROP CONSTRAINT FK_QRTZ_CRON_TRIGGERS_QRTZ_TRIGGERSGOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_QRTZ_SIMPLE_TRIGGERS_QRTZ_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISFOREIGNKEY') = 1)ALTER TABLE [dbo].[QRTZ_SIMPLE_TRIGGERS] DROP CONSTRAINT FK_QRTZ_SIMPLE_TRIGGERS_QRTZ_TRIGGERSGOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_QRTZ_SIMPROP_TRIGGERS_QRTZ_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISFOREIGNKEY') = 1)ALTER TABLE [dbo].[QRTZ_SIMPROP_TRIGGERS] DROP CONSTRAINT FK_QRTZ_SIMPROP_TRIGGERS_QRTZ_TRIGGERSGOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_CALENDARS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_CALENDARS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_CRON_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_CRON_TRIGGERS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_BLOB_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_BLOB_TRIGGERS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_FIRED_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_FIRED_TRIGGERS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_PAUSED_TRIGGER_GRPS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_PAUSED_TRIGGER_GRPS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_SCHEDULER_STATE]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_SCHEDULER_STATE]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_LOCKS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_LOCKS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_JOB_DETAILS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_JOB_DETAILS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_SIMPLE_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_SIMPLE_TRIGGERS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_SIMPROP_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_SIMPROP_TRIGGERS]GOIF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[QRTZ_TRIGGERS]') AND OBJECTPROPERTY(id, N'ISUSERTABLE') = 1)DROP TABLE [dbo].[QRTZ_TRIGGERS]GOCREATE TABLE [dbo].[QRTZ_CALENDARS] (  [SCHED_NAME] [VARCHAR] (120)  NOT NULL ,  [CALENDAR_NAME] [VARCHAR] (200)  NOT NULL ,  [CALENDAR] [IMAGE] NOT NULL) ON [PRIMARY]GOCREATE TABLE [dbo].[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) ) ON [PRIMARY]GOCREATE TABLE [dbo].[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] NOT NULL ,  [SCHED_TIME] [BIGINT] 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 ) ON [PRIMARY]GOCREATE TABLE [dbo].[QRTZ_PAUSED_TRIGGER_GRPS] (  [SCHED_NAME] [VARCHAR] (120)  NOT NULL ,  [TRIGGER_GROUP] [VARCHAR] (200)  NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[QRTZ_SCHEDULER_STATE] (  [SCHED_NAME] [VARCHAR] (120)  NOT NULL ,  [INSTANCE_NAME] [VARCHAR] (200)  NOT NULL ,  [LAST_CHECKIN_TIME] [BIGINT] NOT NULL ,  [CHECKIN_INTERVAL] [BIGINT] NOT NULL) ON [PRIMARY]GOCREATE TABLE [dbo].[QRTZ_LOCKS] (  [SCHED_NAME] [VARCHAR] (120)  NOT NULL ,  [LOCK_NAME] [VARCHAR] (40)  NOT NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[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] [IMAGE] NULL) ON [PRIMARY]GOCREATE TABLE [dbo].[QRTZ_SIMPLE_TRIGGERS] (  [SCHED_NAME] [VARCHAR] (120)  NOT NULL ,  [TRIGGER_NAME] [VARCHAR] (200)  NOT NULL ,  [TRIGGER_GROUP] [VARCHAR] (200)  NOT NULL ,  [REPEAT_COUNT] [BIGINT] NOT NULL ,  [REPEAT_INTERVAL] [BIGINT] NOT NULL ,  [TIMES_TRIGGERED] [BIGINT] NOT NULL) ON [PRIMARY]GOCREATE TABLE [dbo].[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,) ON [PRIMARY]GOCREATE TABLE [dbo].[QRTZ_BLOB_TRIGGERS] (  [SCHED_NAME] [VARCHAR] (120)  NOT NULL ,  [TRIGGER_NAME] [VARCHAR] (200)  NOT NULL ,  [TRIGGER_GROUP] [VARCHAR] (200)  NOT NULL ,  [BLOB_DATA] [IMAGE] NULL) ON [PRIMARY]GOCREATE TABLE [dbo].[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] NULL ,  [PREV_FIRE_TIME] [BIGINT] NULL ,  [PRIORITY] [INTEGER] NULL ,  [TRIGGER_STATE] [VARCHAR] (16)  NOT NULL ,  [TRIGGER_TYPE] [VARCHAR] (8)  NOT NULL ,  [START_TIME] [BIGINT] NOT NULL ,  [END_TIME] [BIGINT] NULL ,  [CALENDAR_NAME] [VARCHAR] (200)  NULL ,  [MISFIRE_INSTR] [SMALLINT] NULL ,  [JOB_DATA] [IMAGE] NULL) ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_CALENDARS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_CALENDARS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [CALENDAR_NAME]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_CRON_TRIGGERS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_CRON_TRIGGERS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_FIRED_TRIGGERS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_FIRED_TRIGGERS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [ENTRY_ID]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_PAUSED_TRIGGER_GRPS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_PAUSED_TRIGGER_GRPS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [TRIGGER_GROUP]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_SCHEDULER_STATE] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_SCHEDULER_STATE] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [INSTANCE_NAME]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_LOCKS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_LOCKS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [LOCK_NAME]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_JOB_DETAILS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_JOB_DETAILS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [JOB_NAME],    [JOB_GROUP]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_SIMPLE_TRIGGERS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_SIMPLE_TRIGGERS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_SIMPROP_TRIGGERS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_SIMPROP_TRIGGERS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_TRIGGERS] WITH NOCHECK ADD  CONSTRAINT [PK_QRTZ_TRIGGERS] PRIMARY KEY  CLUSTERED  (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  )  ON [PRIMARY]GOALTER TABLE [dbo].[QRTZ_CRON_TRIGGERS] ADD  CONSTRAINT [FK_QRTZ_CRON_TRIGGERS_QRTZ_TRIGGERS] FOREIGN KEY  (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  ) REFERENCES [dbo].[QRTZ_TRIGGERS] (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  ) ON DELETE CASCADEGOALTER TABLE [dbo].[QRTZ_SIMPLE_TRIGGERS] ADD  CONSTRAINT [FK_QRTZ_SIMPLE_TRIGGERS_QRTZ_TRIGGERS] FOREIGN KEY  (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  ) REFERENCES [dbo].[QRTZ_TRIGGERS] (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  ) ON DELETE CASCADEGOALTER TABLE [dbo].[QRTZ_SIMPROP_TRIGGERS] ADD  CONSTRAINT [FK_QRTZ_SIMPROP_TRIGGERS_QRTZ_TRIGGERS] FOREIGN KEY  (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  ) REFERENCES [dbo].[QRTZ_TRIGGERS] (    [SCHED_NAME],    [TRIGGER_NAME],    [TRIGGER_GROUP]  ) ON DELETE CASCADEGOALTER TABLE [dbo].[QRTZ_TRIGGERS] ADD  CONSTRAINT [FK_QRTZ_TRIGGERS_QRTZ_JOB_DETAILS] FOREIGN KEY  (    [SCHED_NAME],    [JOB_NAME],    [JOB_GROUP]  ) REFERENCES [dbo].[QRTZ_JOB_DETAILS] (    [SCHED_NAME],    [JOB_NAME],    [JOB_GROUP]  )GO

第四步:修改yml配置文件spring: quartz: job-store-type: jdbc 然后运行~~

之后报错 第 1 行: 只有 DECLARE CURSOR 才允许使用 FOR UPDATE 子句。 网上找了很久,最后结合了一下,在yml文件中添加 spring:  quartz:    job-store-type: jdbc    properties:      org:        quartz:          jobStore:            selectWithLockSQL: SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?问题顺利解决~~~~

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

推荐阅读更多精彩内容