quartz2.1.3+spring3.2.18.RELEASE 实现spring整合quartz第一天
ps:这个版本问题真的很难搞,头疼不已。
大概要点说一下:
1 <dependency> 2 <groupId>org.slf4j</groupId> 3 <artifactId>slf4j-api</artifactId> 4 <version>1.6.0</version> 5 </dependency> 6 <dependency> 7 <groupId>org.slf4j</groupId> 8 <artifactId>slf4j-simple</artifactId> 9 <version>1.7.7</version> 10 </dependency> 11 <dependency> 12 <groupId>org.slf4j</groupId> 13 <artifactId>slf4j-log4j12</artifactId> 14 <version>1.7.25</version> 15 </dependency> 16 <dependency> 17 <groupId>log4j</groupId> 18 <artifactId>log4j</artifactId> 19 <version>1.2.16</version> 20 </dependency>
1 <spring.version>3.2.18.RELEASE</spring.version> 2 3 <dependency> 4 <groupId>org.springframework</groupId> 5 <artifactId>spring-core</artifactId> 6 <version>${spring.version}</version> 7 </dependency> 8 <dependency> 9 <groupId>org.springframework</groupId> 10 <artifactId>spring-expression</artifactId> 11 <version>${spring.version}</version> 12 </dependency> 13 <dependency> 14 <groupId>org.springframework</groupId> 15 <artifactId>spring-beans</artifactId> 16 <version>${spring.version}</version> 17 </dependency> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId>spring-aop</artifactId> 21 <version>${spring.version}</version> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework</groupId> 25 <artifactId>spring-context</artifactId> 26 <version>${spring.version}</version> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework</groupId> 30 <artifactId>spring-context-support</artifactId> 31 <version>${spring.version}</version> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-tx</artifactId> 36 <version>${spring.version}</version> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-jdbc</artifactId> 41 <version>${spring.version}</version> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-orm</artifactId> 46 <version>${spring.version}</version> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework</groupId> 50 <artifactId>spring-oxm</artifactId> 51 <version>${spring.version}</version> 52 </dependency> 53 <dependency> 54 <groupId>org.springframework</groupId> 55 <artifactId>spring-web</artifactId> 56 <version>${spring.version}</version> 57 </dependency> 58 <dependency> 59 <groupId>org.springframework</groupId> 60 <artifactId>spring-webmvc</artifactId> 61 <version>${spring.version}</version> 62 </dependency> 63 <dependency> 64 <groupId>org.springframework</groupId> 65 <artifactId>spring-webmvc-portlet</artifactId> 66 <version>${spring.version}</version> 67 </dependency> 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-test</artifactId> 71 <version>${spring.version}</version> 72 <scope>test</scope> 73 </dependency> 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-aspects</artifactId> 77 <version>${spring.version}</version> 78 </dependency>
1 <dependency> 2 <groupId>org.quartz-scheduler</groupId> 3 <artifactId>quartz</artifactId> 4 <version>2.1.3</version> 5 </dependency>
1 SET ANSI_NULLS ON 2 GO 3 SET QUOTED_IDENTIFIER ON 4 GO 5 SET ANSI_PADDING ON 6 GO 7 CREATE TABLE [dbo].[QRTZ_BLOB_TRIGGERS]( 8 [SCHED_NAME] [varchar](120) NOT NULL, 9 [TRIGGER_NAME] [varchar](200) NOT NULL, 10 [TRIGGER_GROUP] [varchar](200) NOT NULL, 11 [BLOB_DATA] [image] NULL, 12 PRIMARY KEY CLUSTERED 13 ( 14 [SCHED_NAME] ASC, 15 [TRIGGER_NAME] ASC, 16 [TRIGGER_GROUP] ASC 17 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 18 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 19 20 GO 21 SET ANSI_PADDING OFF 22 GO 23 /****** Object: Table [dbo].[QRTZ_CALENDARS] Script Date: 2019-1-10 9:45:58 ******/ 24 SET ANSI_NULLS ON 25 GO 26 SET QUOTED_IDENTIFIER ON 27 GO 28 SET ANSI_PADDING ON 29 GO 30 CREATE TABLE [dbo].[QRTZ_CALENDARS]( 31 [SCHED_NAME] [varchar](120) NOT NULL, 32 [CALENDAR_NAME] [varchar](200) NOT NULL, 33 [CALENDAR] [image] NOT NULL, 34 PRIMARY KEY CLUSTERED 35 ( 36 [SCHED_NAME] ASC, 37 [CALENDAR_NAME] ASC 38 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 39 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 40 41 GO 42 SET ANSI_PADDING OFF 43 GO 44 /****** Object: Table [dbo].[QRTZ_CRON_TRIGGERS] Script Date: 2019-1-10 9:45:58 ******/ 45 SET ANSI_NULLS ON 46 GO 47 SET QUOTED_IDENTIFIER ON 48 GO 49 SET ANSI_PADDING ON 50 GO 51 CREATE TABLE [dbo].[QRTZ_CRON_TRIGGERS]( 52 [SCHED_NAME] [varchar](120) NOT NULL, 53 [TRIGGER_NAME] [varchar](200) NOT NULL, 54 [TRIGGER_GROUP] [varchar](200) NOT NULL, 55 [CRON_EXPRESSION] [varchar](200) NOT NULL, 56 [TIME_ZONE_ID] [varchar](80) NULL, 57 PRIMARY KEY CLUSTERED 58 ( 59 [SCHED_NAME] ASC, 60 [TRIGGER_NAME] ASC, 61 [TRIGGER_GROUP] ASC 62 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 63 ) ON [PRIMARY] 64 65 GO 66 SET ANSI_PADDING OFF 67 GO 68 /****** Object: Table [dbo].[QRTZ_FIRED_TRIGGERS] Script Date: 2019-1-10 9:45:58 ******/ 69 SET ANSI_NULLS ON 70 GO 71 SET QUOTED_IDENTIFIER ON 72 GO 73 SET ANSI_PADDING ON 74 GO 75 CREATE TABLE [dbo].[QRTZ_FIRED_TRIGGERS]( 76 [SCHED_NAME] [varchar](120) NOT NULL, 77 [ENTRY_ID] [varchar](95) NOT NULL, 78 [TRIGGER_NAME] [varchar](200) NOT NULL, 79 [TRIGGER_GROUP] [varchar](200) NOT NULL, 80 [INSTANCE_NAME] [varchar](200) NOT NULL, 81 [FIRED_TIME] [bigint] NOT NULL, 82 [PRIORITY] [int] NOT NULL, 83 [STATE] [varchar](16) NOT NULL, 84 [JOB_NAME] [varchar](200) NULL, 85 [JOB_GROUP] [varchar](200) NULL, 86 [IS_NONCONCURRENT] [varchar](1) NULL, 87 [REQUESTS_RECOVERY] [varchar](1) NULL, 88 PRIMARY KEY CLUSTERED 89 ( 90 [SCHED_NAME] ASC, 91 [ENTRY_ID] ASC 92 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 93 ) ON [PRIMARY] 94 95 GO 96 SET ANSI_PADDING OFF 97 GO 98 /****** Object: Table [dbo].[QRTZ_JOB_DETAILS] Script Date: 2019-1-10 9:45:58 ******/ 99 SET ANSI_NULLS ON 100 GO 101 SET QUOTED_IDENTIFIER ON 102 GO 103 SET ANSI_PADDING ON 104 GO 105 CREATE TABLE [dbo].[QRTZ_JOB_DETAILS]( 106 [SCHED_NAME] [varchar](120) NOT NULL, 107 [JOB_NAME] [varchar](200) NOT NULL, 108 [JOB_GROUP] [varchar](200) NOT NULL, 109 [DESCRIPTION] [varchar](250) NULL, 110 [JOB_CLASS_NAME] [varchar](250) NOT NULL, 111 [IS_DURABLE] [varchar](1) NOT NULL, 112 [IS_NONCONCURRENT] [varchar](1) NOT NULL, 113 [IS_UPDATE_DATA] [varchar](1) NOT NULL, 114 [REQUESTS_RECOVERY] [varchar](1) NOT NULL, 115 [JOB_DATA] [image] NULL, 116 PRIMARY KEY CLUSTERED 117 ( 118 [SCHED_NAME] ASC, 119 [JOB_NAME] ASC, 120 [JOB_GROUP] ASC 121 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 122 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 123 124 GO 125 SET ANSI_PADDING OFF 126 GO 127 /****** Object: Table [dbo].[QRTZ_LOCKS] Script Date: 2019-1-10 9:45:58 ******/ 128 SET ANSI_NULLS ON 129 GO 130 SET QUOTED_IDENTIFIER ON 131 GO 132 SET ANSI_PADDING ON 133 GO 134 CREATE TABLE [dbo].[QRTZ_LOCKS]( 135 [SCHED_NAME] [varchar](120) NOT NULL, 136 [LOCK_NAME] [varchar](40) NOT NULL, 137 PRIMARY KEY CLUSTERED 138 ( 139 [SCHED_NAME] ASC, 140 [LOCK_NAME] ASC 141 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 142 ) ON [PRIMARY] 143 144 GO 145 SET ANSI_PADDING OFF 146 GO 147 /****** Object: Table [dbo].[QRTZ_PAUSED_TRIGGER_GRPS] Script Date: 2019-1-10 9:45:58 ******/ 148 SET ANSI_NULLS ON 149 GO 150 SET QUOTED_IDENTIFIER ON 151 GO 152 SET ANSI_PADDING ON 153 GO 154 CREATE TABLE [dbo].[QRTZ_PAUSED_TRIGGER_GRPS]( 155 [SCHED_NAME] [varchar](120) NOT NULL, 156 [TRIGGER_GROUP] [varchar](200) NOT NULL, 157 PRIMARY KEY CLUSTERED 158 ( 159 [SCHED_NAME] ASC, 160 [TRIGGER_GROUP] ASC 161 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 162 ) ON [PRIMARY] 163 164 GO 165 SET ANSI_PADDING OFF 166 GO 167 /****** Object: Table [dbo].[QRTZ_SCHEDULER_STATE] Script Date: 2019-1-10 9:45:58 ******/ 168 SET ANSI_NULLS ON 169 GO 170 SET QUOTED_IDENTIFIER ON 171 GO 172 SET ANSI_PADDING ON 173 GO 174 CREATE TABLE [dbo].[QRTZ_SCHEDULER_STATE]( 175 [SCHED_NAME] [varchar](120) NOT NULL, 176 [INSTANCE_NAME] [varchar](200) NOT NULL, 177 [LAST_CHECKIN_TIME] [bigint] NOT NULL, 178 [CHECKIN_INTERVAL] [bigint] NOT NULL, 179 PRIMARY KEY CLUSTERED 180 ( 181 [SCHED_NAME] ASC, 182 [INSTANCE_NAME] ASC 183 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 184 ) ON [PRIMARY] 185 186 GO 187 SET ANSI_PADDING OFF 188 GO 189 /****** Object: Table [dbo].[QRTZ_SIMPLE_TRIGGERS] Script Date: 2019-1-10 9:45:58 ******/ 190 SET ANSI_NULLS ON 191 GO 192 SET QUOTED_IDENTIFIER ON 193 GO 194 SET ANSI_PADDING ON 195 GO 196 CREATE TABLE [dbo].[QRTZ_SIMPLE_TRIGGERS]( 197 [SCHED_NAME] [varchar](120) NOT NULL, 198 [TRIGGER_NAME] [varchar](200) NOT NULL, 199 [TRIGGER_GROUP] [varchar](200) NOT NULL, 200 [REPEAT_COUNT] [bigint] NOT NULL, 201 [REPEAT_INTERVAL] [bigint] NOT NULL, 202 [TIMES_TRIGGERED] [bigint] NOT NULL, 203 PRIMARY KEY CLUSTERED 204 ( 205 [SCHED_NAME] ASC, 206 [TRIGGER_NAME] ASC, 207 [TRIGGER_GROUP] ASC 208 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 209 ) ON [PRIMARY] 210 211 GO 212 SET ANSI_PADDING OFF 213 GO 214 /****** Object: Table [dbo].[QRTZ_SIMPROP_TRIGGERS] Script Date: 2019-1-10 9:45:58 ******/ 215 SET ANSI_NULLS ON 216 GO 217 SET QUOTED_IDENTIFIER ON 218 GO 219 SET ANSI_PADDING ON 220 GO 221 CREATE TABLE [dbo].[QRTZ_SIMPROP_TRIGGERS]( 222 [SCHED_NAME] [varchar](120) NOT NULL, 223 [TRIGGER_NAME] [varchar](200) NOT NULL, 224 [TRIGGER_GROUP] [varchar](200) NOT NULL, 225 [STR_PROP_1] [varchar](512) NULL, 226 [STR_PROP_2] [varchar](512) NULL, 227 [STR_PROP_3] [varchar](512) NULL, 228 [INT_PROP_1] [int] NULL, 229 [INT_PROP_2] [int] NULL, 230 [LONG_PROP_1] [bigint] NULL, 231 [LONG_PROP_2] [bigint] NULL, 232 [DEC_PROP_1] [numeric](13, 4) NULL, 233 [DEC_PROP_2] [numeric](13, 4) NULL, 234 [BOOL_PROP_1] [varchar](1) NULL, 235 [BOOL_PROP_2] [varchar](1) NULL, 236 PRIMARY KEY CLUSTERED 237 ( 238 [SCHED_NAME] ASC, 239 [TRIGGER_NAME] ASC, 240 [TRIGGER_GROUP] ASC 241 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 242 ) ON [PRIMARY] 243 244 GO 245 SET ANSI_PADDING OFF 246 GO 247 /****** Object: Table [dbo].[QRTZ_TRIGGERS] Script Date: 2019-1-10 9:45:58 ******/ 248 SET ANSI_NULLS ON 249 GO 250 SET QUOTED_IDENTIFIER ON 251 GO 252 SET ANSI_PADDING ON 253 GO 254 CREATE TABLE [dbo].[QRTZ_TRIGGERS]( 255 [SCHED_NAME] [varchar](120) NOT NULL, 256 [TRIGGER_NAME] [varchar](200) NOT NULL, 257 [TRIGGER_GROUP] [varchar](200) NOT NULL, 258 [JOB_NAME] [varchar](200) NOT NULL, 259 [JOB_GROUP] [varchar](200) NOT NULL, 260 [DESCRIPTION] [varchar](250) NULL, 261 [NEXT_FIRE_TIME] [bigint] NULL, 262 [PREV_FIRE_TIME] [bigint] NULL, 263 [PRIORITY] [int] NULL, 264 [TRIGGER_STATE] [varchar](16) NOT NULL, 265 [TRIGGER_TYPE] [varchar](8) NOT NULL, 266 [START_TIME] [bigint] NOT NULL, 267 [END_TIME] [bigint] NULL, 268 [CALENDAR_NAME] [varchar](200) NULL, 269 [MISFIRE_INSTR] [smallint] NULL, 270 [JOB_DATA] [image] NULL, 271 PRIMARY KEY CLUSTERED 272 ( 273 [SCHED_NAME] ASC, 274 [TRIGGER_NAME] ASC, 275 [TRIGGER_GROUP] ASC 276 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 277 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 278 279 GO 280 SET ANSI_PADDING OFF 281 GO 282 INSERT [dbo].[QRTZ_CRON_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP], [CRON_EXPRESSION], [TIME_ZONE_ID]) VALUES (N'quartzScheduler', N'triggerName1', N'triggerGroupName1', N'0/10 * * * * ?', N'Asia/Shanghai') 283 INSERT [dbo].[QRTZ_CRON_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP], [CRON_EXPRESSION], [TIME_ZONE_ID]) VALUES (N'quartzScheduler', N'triggerName2', N'triggerGroupName1', N'0 0 */1 * * ?', N'Asia/Shanghai') 284 INSERT [dbo].[QRTZ_JOB_DETAILS] ([SCHED_NAME], [JOB_NAME], [JOB_GROUP], [DESCRIPTION], [JOB_CLASS_NAME], [IS_DURABLE], [IS_NONCONCURRENT], [IS_UPDATE_DATA], [REQUESTS_RECOVERY], [JOB_DATA]) VALUES (N'quartzScheduler', N'job1', N'jobGroup1', NULL, N'com.vimtech.quartz.scheduler.TempMessageProjJob', N'0', N'0', N'0', N'0', 0x230D0A23467269204465632032312031363A35373A32332043535420323031380D0A) 285 INSERT [dbo].[QRTZ_JOB_DETAILS] ([SCHED_NAME], [JOB_NAME], [JOB_GROUP], [DESCRIPTION], [JOB_CLASS_NAME], [IS_DURABLE], [IS_NONCONCURRENT], [IS_UPDATE_DATA], [REQUESTS_RECOVERY], [JOB_DATA]) VALUES (N'quartzScheduler', N'job2', N'jobGroup1', NULL, N'com.vimtech.quartz.scheduler.AccessTokenJob', N'0', N'0', N'0', N'0', 0x230D0A23467269204465632032312031363A35393A30372043535420323031380D0A) 286 INSERT [dbo].[QRTZ_LOCKS] ([SCHED_NAME], [LOCK_NAME]) VALUES (N'quartzScheduler', N'TRIGGER_ACCESS') 287 INSERT [dbo].[QRTZ_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP], [JOB_NAME], [JOB_GROUP], [DESCRIPTION], [NEXT_FIRE_TIME], [PREV_FIRE_TIME], [PRIORITY], [TRIGGER_STATE], [TRIGGER_TYPE], [START_TIME], [END_TIME], [CALENDAR_NAME], [MISFIRE_INSTR], [JOB_DATA]) VALUES (N'quartzScheduler', N'triggerName1', N'triggerGroupName1', N'job1', N'jobGroup1', NULL, 1545383200000, 1545383190000, 5, N'PAUSED', N'CRON', 1545382643000, 0, NULL, 0, 0x) 288 INSERT [dbo].[QRTZ_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP], [JOB_NAME], [JOB_GROUP], [DESCRIPTION], [NEXT_FIRE_TIME], [PREV_FIRE_TIME], [PRIORITY], [TRIGGER_STATE], [TRIGGER_TYPE], [START_TIME], [END_TIME], [CALENDAR_NAME], [MISFIRE_INSTR], [JOB_DATA]) VALUES (N'quartzScheduler', N'triggerName2', N'triggerGroupName1', N'job2', N'jobGroup1', NULL, 1545382800000, -1, 5, N'PAUSED', N'CRON', 1545382747000, 0, NULL, 0, 0x) 289 ALTER TABLE [dbo].[QRTZ_BLOB_TRIGGERS] WITH CHECK ADD FOREIGN KEY([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 290 REFERENCES [dbo].[QRTZ_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 291 GO 292 ALTER TABLE [dbo].[QRTZ_CRON_TRIGGERS] WITH CHECK ADD FOREIGN KEY([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 293 REFERENCES [dbo].[QRTZ_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 294 GO 295 ALTER TABLE [dbo].[QRTZ_SIMPLE_TRIGGERS] WITH CHECK ADD FOREIGN KEY([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 296 REFERENCES [dbo].[QRTZ_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 297 GO 298 ALTER TABLE [dbo].[QRTZ_SIMPROP_TRIGGERS] WITH CHECK ADD FOREIGN KEY([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 299 REFERENCES [dbo].[QRTZ_TRIGGERS] ([SCHED_NAME], [TRIGGER_NAME], [TRIGGER_GROUP]) 300 GO 301 ALTER TABLE [dbo].[QRTZ_TRIGGERS] WITH CHECK ADD FOREIGN KEY([SCHED_NAME], [JOB_NAME], [JOB_GROUP]) 302 REFERENCES [dbo].[QRTZ_JOB_DETAILS] ([SCHED_NAME], [JOB_NAME], [JOB_GROUP]) 303 GO
1 package com.vimtech.bms.webchat.quartz.service; 2 3 public interface QuartzService { 4 5 /** 6 * addJob(方法描述:添加一个定时任务) <br /> 7 * (方法适用条件描述: – 可选) 8 * 9 * @param jobName 10 * 作业名称 11 * @param jobGroupName 12 * 作业组名称 13 * @param triggerName 14 * 触发器名称 15 * @param triggerGroupName 16 * 触发器组名称 17 * @param cls 18 * 定时任务的class 19 * @param cron 20 * 时间表达式 void 21 * @exception 22 * @since 1.0.0 23 */ 24 public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class cls, String cron); 25 26 /** 27 * 28 * @param oldjobName 原job name 29 * @param oldjobGroup 原job group 30 * @param oldtriggerName 原 trigger name 31 * @param oldtriggerGroup 原 trigger group 32 * @param jobName 33 * @param jobGroup 34 * @param triggerName 35 * @param triggerGroup 36 * @param cron 37 */ 38 public boolean modifyJobTime(String oldjobName, String oldjobGroup, String oldtriggerName, String oldtriggerGroup, String jobName, String jobGroup, String triggerName, String triggerGroup, String cron); 39 40 /** 41 * 修改触发器调度时间 42 * @param triggerName 触发器名称 43 * @param triggerGroupName 触发器组名称 44 * @param cron cron表达式 45 */ 46 public void modifyJobTime(String triggerName, 47 String triggerGroupName, String cron); 48 49 50 /** 51 * 暂停指定的任务 52 * @param jobName 任务名称 53 * @param jobGroupName 任务组名称 54 * @return 55 */ 56 public void pauseJob(String jobName, String jobGroupName); 57 58 /** 59 * 恢复指定的任务 60 * @param jobName 任务名称 61 * @param jobGroupName 任务组名称 62 * @return 63 */ 64 public void resumeJob(String jobName, String jobGroupName); 65 66 /** 67 * 删除指定组任务 68 * @param jobName 作业名称 69 * @param jobGroupName 作业组名称 70 * @param triggerName 触发器名称 71 * @param triggerGroupName 触发器组名称 72 */ 73 public void removeJob(String jobName, String jobGroupName, 74 String triggerName, String triggerGroupName); 75 76 77 /** 78 * 开始所有定时任务。启动调度器 79 */ 80 public void startSchedule(); 81 82 /** 83 * 关闭调度器 84 */ 85 public void shutdownSchedule(); 86 }
1 package com.vimtech.bms.webchat.quartz.service.impl; 2 3 import lombok.Getter; 4 import lombok.Setter; 5 6 import org.quartz.*; 7 8 import com.vimtech.bms.webchat.quartz.service.QuartzService; 9 10 public class QuartzServiceImpl implements QuartzService{ 11 12 @Getter @Setter 13 private Scheduler quartzScheduler; 14 15 //添加一个定时任务 16 @Override 17 public void addJob(String jobName, String jobGroupName, String triggerName, 18 String triggerGroupName, Class cls, String cron) { 19 try { 20 // 获取调度器 21 Scheduler sched = quartzScheduler; 22 // 创建一项作业 23 JobDetail job = JobBuilder.newJob(cls) 24 .withIdentity(jobName, jobGroupName).build(); 25 // 创建一个触发器 26 CronTrigger trigger = TriggerBuilder.newTrigger() 27 .withIdentity(triggerName, triggerGroupName) 28 .withSchedule(CronScheduleBuilder.cronSchedule(cron)) 29 .build(); 30 // 告诉调度器使用该触发器来安排作业 31 sched.scheduleJob(job, trigger); 32 // 启动 33 if (!sched.isShutdown()) { 34 sched.start(); 35 } 36 } catch (Exception e) { 37 throw new RuntimeException(e); 38 } 39 } 40 41 /** 42 * 修改定时器任务信息 43 */ 44 @Override 45 public boolean modifyJobTime(String oldjobName, String oldjobGroup, String oldtriggerName, String oldtriggerGroup, String jobName, String jobGroup, 46 String triggerName, String triggerGroup, String cron) { 47 try { 48 Scheduler sched = quartzScheduler; 49 CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey 50 .triggerKey(oldtriggerName, oldtriggerGroup)); 51 if (trigger == null) { 52 return false; 53 } 54 55 JobKey jobKey = JobKey.jobKey(oldjobName, oldjobGroup); 56 TriggerKey triggerKey = TriggerKey.triggerKey(oldtriggerName, 57 oldtriggerGroup); 58 59 JobDetail job = sched.getJobDetail(jobKey); 60 Class jobClass = job.getJobClass(); 61 // 停止触发器 62 sched.pauseTrigger(triggerKey); 63 // 移除触发器 64 sched.unscheduleJob(triggerKey); 65 // 删除任务 66 sched.deleteJob(jobKey); 67 68 addJob(jobName, jobGroup, triggerName, triggerGroup, jobClass, 69 cron); 70 71 return true; 72 } catch (Exception e) { 73 throw new RuntimeException(e); 74 } 75 76 } 77 78 @Override 79 public void modifyJobTime(String triggerName, String triggerGroupName, 80 String time) { 81 try { 82 Scheduler sched = quartzScheduler; 83 CronTrigger trigger = (CronTrigger) sched.getTrigger(TriggerKey 84 .triggerKey(triggerName, triggerGroupName)); 85 if (trigger == null) { 86 return; 87 } 88 String oldTime = trigger.getCronExpression(); 89 if (!oldTime.equalsIgnoreCase(time)) { 90 CronTrigger ct = (CronTrigger) trigger; 91 // 修改时间 92 ct.getTriggerBuilder() 93 .withSchedule(CronScheduleBuilder.cronSchedule(time)) 94 .build(); 95 // 重启触发器 96 sched.resumeTrigger(TriggerKey.triggerKey(triggerName, 97 triggerGroupName)); 98 } 99 } catch (Exception e) { 100 throw new RuntimeException(e); 101 } 102 } 103 104 @Override 105 public void removeJob(String jobName, String jobGroupName, 106 String triggerName, String triggerGroupName) { 107 try { 108 Scheduler sched = quartzScheduler; 109 // 停止触发器 110 sched.pauseTrigger(TriggerKey.triggerKey(triggerName, 111 triggerGroupName)); 112 // 移除触发器 113 sched.unscheduleJob(TriggerKey.triggerKey(triggerName, 114 triggerGroupName)); 115 // 删除任务 116 sched.deleteJob(JobKey.jobKey(jobName, jobGroupName)); 117 } catch (Exception e) { 118 throw new RuntimeException(e); 119 } 120 } 121 122 @Override 123 public void startSchedule() { 124 try { 125 Scheduler sched = quartzScheduler; 126 sched.start(); 127 } catch (Exception e) { 128 throw new RuntimeException(e); 129 } 130 } 131 132 @Override 133 public void shutdownSchedule() { 134 try { 135 Scheduler sched = quartzScheduler; 136 if (!sched.isShutdown()) { 137 sched.shutdown(); 138 } 139 } catch (Exception e) { 140 throw new RuntimeException(e); 141 } 142 } 143 144 @Override 145 public void pauseJob(String jobName, String jobGroupName) { 146 try { 147 quartzScheduler.pauseJob( JobKey.jobKey(jobName, jobGroupName)); 148 } catch (SchedulerException e) { 149 e.printStackTrace(); 150 } 151 152 } 153 154 @Override 155 public void resumeJob(String jobName, String jobGroupName) { 156 try { 157 quartzScheduler.resumeJob(JobKey.jobKey(jobName, jobGroupName)); 158 } catch (SchedulerException e) { 159 e.printStackTrace(); 160 } 161 } 162 163 164 }
1 package com.vimtech.bms.webchat.quartz.action; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Set; 8 9 import com.alibaba.fastjson.JSONObject; 10 import com.vimtech.bms.common.util.UtilTools; 11 import com.vimtech.bms.webchat.quartz.domian.JobEntity; 12 import com.vimtech.bms.webchat.quartz.service.QuartzService; 13 import com.vimtech.ui.BaseAction; 14 import com.vimtech.ui.domain.Result; 15 16 import org.quartz.*; 17 import org.quartz.impl.matchers.GroupMatcher; 18 import org.springframework.web.bind.annotation.RequestParam; 19 20 import lombok.Getter; 21 import lombok.Setter; 22 23 public class QuartzController extends BaseAction{ 24 25 /** 26 * 27 */ 28 private static final long serialVersionUID = 1L; 29 @Getter @Setter 30 private Scheduler quartzScheduler; 31 32 @Getter @Setter 33 private QuartzService quartzService; 34 @Getter @Setter 35 private String jobName; 36 @Getter @Setter 37 private String jobGroup; 38 @Getter @Setter 39 private String jobGroupName; 40 @Getter @Setter 41 private String triggerName; 42 @Getter @Setter 43 private String triggerGroupName; 44 @Getter @Setter 45 private String cron; 46 @Getter @Setter 47 private String clazz; 48 49 /** 50 * 定时列表页 51 * 52 * @return 53 * @throws SchedulerException 54 */ 55 public String queryJob() throws SchedulerException { 56 List<JobEntity> jobInfos = this.getSchedulerJobInfo(); 57 request().setAttribute("jobInfos", jobInfos); 58 return "queryJob"; 59 } 60 61 /** 62 * 跳转到新增 63 * 64 * @return 65 * @throws SchedulerException 66 * @throws ClassNotFoundException 67 */ 68 public String addPage() throws SchedulerException { 69 return "addJob"; 70 } 71 72 /** 73 * 新增job 74 * 75 * @return 76 * @throws SchedulerException 77 * @throws ClassNotFoundException 78 */ 79 public String addJob() throws SchedulerException, ClassNotFoundException { 80 Class cls = Class.forName(clazz); 81 quartzService.addJob(jobName, jobGroupName, triggerName, triggerGroupName, cls, cron); 82 request().setAttribute("message", "添加任务成功!"); 83 request().setAttribute("opName", "添加任务!"); 84 return "message"; 85 } 86 87 /** 88 * 跳转到编辑 89 * 90 * @return 91 * @throws SchedulerException 92 * @throws ClassNotFoundException 93 */ 94 public String editPage() throws SchedulerException { 95 96 JobKey jobKey = JobKey.jobKey(jobName, jobGroup); 97 JobDetail jd = quartzScheduler.getJobDetail(jobKey); 98 List<CronTrigger> triggers = (List<CronTrigger>) quartzScheduler.getTriggersOfJob(jobKey); 99 CronTrigger trigger = triggers.get(0); 100 TriggerKey triggerKey = trigger.getKey(); 101 String cron = trigger.getCronExpression(); 102 Map<String, String> pd = new HashMap<String, String>(); 103 pd.put("jobName", jobKey.getName()); 104 pd.put("jobGroup", jobKey.getGroup()); 105 pd.put("triggerName", triggerKey.getName()); 106 pd.put("triggerGroupName", triggerKey.getGroup()); 107 pd.put("cron", cron); 108 pd.put("clazz", jd.getJobClass().getCanonicalName()); 109 request().setAttribute("pd", pd); 110 request().setAttribute("msg", "edit"); 111 112 return "editJob"; 113 } 114 115 116 /** 117 * 编辑job 118 * 119 * @return 120 * @throws SchedulerException 121 * @throws ClassNotFoundException 122 */ 123 public String editJob() throws SchedulerException, ClassNotFoundException { 124 Class cls = Class.forName(clazz); 125 String oldjobName = request().getParameter("oldjobName"); 126 String oldjobGroup = request().getParameter("oldjobGroup"); 127 String oldtriggerName = request().getParameter("oldtriggerName"); 128 String oldtriggerGroup = request().getParameter("oldtriggerGroup"); 129 130 boolean result = quartzService.modifyJobTime(oldjobName, oldjobGroup, oldtriggerName, oldtriggerGroup, 131 jobName, jobGroupName, triggerName, triggerGroupName, cron); 132 if(result){ 133 request().setAttribute("message", "修改任务成功!"); 134 }else{ 135 request().setAttribute("message", "修改任务失败!"); 136 } 137 request().setAttribute("opName", "更新任务!"); 138 return "message"; 139 } 140 141 142 public String pauseJob(){ 143 JSONObject json = new JSONObject(); 144 if(UtilTools.isEmpty(jobName) || UtilTools.isEmpty(jobGroupName)){ 145 json.put("status", "wrong"); 146 }else{ 147 quartzService.pauseJob(jobName, jobGroupName); 148 json.put("status", "success"); 149 } 150 this.setResult(new Result()); 151 this.getResult().setData(json.toJSONString()); 152 return "json"; 153 } 154 155 public String resumeJob(){ 156 JSONObject json = new JSONObject(); 157 if(UtilTools.isEmpty(jobName) || UtilTools.isEmpty(jobGroupName)){ 158 json.put("status", "wrong"); 159 }else{ 160 quartzService.resumeJob(jobName, jobGroupName); 161 json.put("status", "success"); 162 } 163 this.setResult(new Result()); 164 this.getResult().setData(json.toJSONString()); 165 return "json"; 166 } 167 168 public String deleteJob(){ 169 JSONObject json = new JSONObject(); 170 171 if(UtilTools.isEmpty(jobName) || UtilTools.isEmpty(jobGroupName) || 172 UtilTools.isEmpty(triggerName) || UtilTools.isEmpty(triggerGroupName) ){ 173 json.put("status", "wrong"); 174 }else{ 175 quartzService.removeJob(jobName, jobGroupName, triggerName, triggerGroupName); 176 json.put("status", "success"); 177 } 178 this.setResult(new Result()); 179 this.getResult().setData(json.toJSONString()); 180 return "json"; 181 } 182 183 184 private List<JobEntity> getSchedulerJobInfo() throws SchedulerException { 185 List<JobEntity> jobInfos = new ArrayList<JobEntity>(); 186 List<String> triggerGroupNames = quartzScheduler.getTriggerGroupNames(); 187 for (String triggerGroupName : triggerGroupNames) { 188 Set<TriggerKey> triggerKeySet = quartzScheduler 189 .getTriggerKeys(GroupMatcher 190 .triggerGroupEquals(triggerGroupName)); 191 for (TriggerKey triggerKey : triggerKeySet) { 192 Trigger t = quartzScheduler.getTrigger(triggerKey); 193 if (t instanceof CronTrigger) { 194 CronTrigger trigger = (CronTrigger) t; 195 JobKey jobKey = trigger.getJobKey(); 196 JobDetail jd = quartzScheduler.getJobDetail(jobKey); 197 JobEntity jobInfo = new JobEntity(); 198 jobInfo.setJobName(jobKey.getName()); 199 jobInfo.setJobGroup(jobKey.getGroup()); 200 jobInfo.setTriggerName(triggerKey.getName()); 201 jobInfo.setTriggerGroupName(triggerKey.getGroup()); 202 jobInfo.setCronExpr(trigger.getCronExpression()); 203 jobInfo.setNextFireTime(trigger.getNextFireTime()); 204 jobInfo.setPreviousFireTime(trigger.getPreviousFireTime()); 205 jobInfo.setStartTime(trigger.getStartTime()); 206 jobInfo.setEndTime(trigger.getEndTime()); 207 jobInfo.setJobClass(jd.getJobClass().getCanonicalName()); 208 // jobInfo.setDuration(Long.parseLong(jd.getDescription())); 209 Trigger.TriggerState triggerState = quartzScheduler 210 .getTriggerState(trigger.getKey()); 211 jobInfo.setJobStatus(triggerState.toString());// NONE无, 212 // NORMAL正常, 213 // PAUSED暂停, 214 // COMPLETE完全, 215 // ERROR错误, 216 // BLOCKED阻塞 217 JobDataMap map = quartzScheduler.getJobDetail(jobKey) 218 .getJobDataMap(); 219 if (null != map&&map.size() != 0) { 220 jobInfo.setCount(Integer.parseInt((String) map 221 .get("count"))); 222 jobInfo.setJobDataMap(map); 223 } else { 224 jobInfo.setJobDataMap(new JobDataMap()); 225 } 226 jobInfos.add(jobInfo); 227 } 228 } 229 } 230 return jobInfos; 231 } 232 233 }
本文来自博客园,作者:升级打怪猴,转载请注明原文链接:https://www.cnblogs.com/soul113/p/10509701.html