摘要:通過(guò)再添加其他的元素可在中放置多個(gè)觸發(fā)器。停止觸發(fā)器重啟觸發(fā)器停止觸發(fā)器移除觸發(fā)器添加調(diào)度的任務(wù)類調(diào)度的任務(wù)。執(zhí)行的張表入數(shù)據(jù)庫(kù)添加啟動(dòng)類簡(jiǎn)單微服務(wù),支持集群分布式,并支持動(dòng)態(tài)修改任務(wù)的執(zhí)行時(shí)間。
SpringCloud(第 010 篇)簡(jiǎn)單 Quartz-Cluster 微服務(wù),支持集群分布式,并支持動(dòng)態(tài)修改 Quartz 任務(wù)的 cronExpression 執(zhí)行時(shí)間
-
一、大致介紹1、根據(jù)上一章節(jié)的單臺(tái)測(cè)試,本章節(jié)修改 Quartz 了支持分布式,因?yàn)檫@是更多的企業(yè)開(kāi)發(fā)場(chǎng)景所需要的開(kāi)發(fā)模式; 2、而且在定時(shí)任務(wù)執(zhí)行的過(guò)程中,通過(guò)修改 Quartz 觸發(fā)器表的 cronExpression 表達(dá)式值,從而達(dá)到動(dòng)態(tài)修改定時(shí)任務(wù)的執(zhí)行時(shí)間; 3、注意:配置文件中的 mysql 數(shù)據(jù)庫(kù)鏈接配置大家就各自配置自己的哈;二、實(shí)現(xiàn)步驟 2.1 添加 maven 引用包
2.2 添加應(yīng)用配置文件(springms-simple-quartz-cluster/src/main/resources/application.yml)4.0.0 springms-simple-quartz-cluster 1.0-SNAPSHOT jar com.springms.cloud springms-spring-cloud 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.quartz-scheduler quartz 2.3.0 org.quartz-scheduler quartz-jobs 2.3.0 org.springframework spring-context-support com.alibaba druid 1.1.3
server: port: 8395 spring: application: name: springms-simple-quartz-cluster #全部小寫(xiě) ##################################################################################################### # mysql 屬性配置 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://ip:port/hmilyylimh?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true username: username password: password jpa: hibernate: #ddl-auto: create #ddl-auto:設(shè)為create表示每次都重新建表 ddl-auto: update #ddl-auto:設(shè)為update表示每次都不會(huì)重新建表 show-sql: true ##################################################################################################### ##################################################################################################### #########mysql######### 注釋先不用這些屬性 #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #spring.datasource.driver-class-name=com.mysql.jdbc.Driver #spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true #spring.datasource.username=test #spring.datasource.password=test # # #spring.datasource.initialSize=5 #spring.datasource.minIdle=5 #spring.datasource.maxActive=20 #spring.datasource.maxWait=60000 # # #spring.datasource.timeBetweenEvictionRunsMillis=3600000 #spring.datasource.minEvictableIdleTimeMillis=18000000 # # #spring.datasource.validationQuery=SELECT 1 FROM DUAL #spring.datasource.testWhileIdle=true #spring.datasource.testOnBorrow=true #spring.datasource.testOnReturn=true #spring.datasource.poolPreparedStatements=true #spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 #spring.datasource.filters=stat,wall,log4j #spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ##################################################################################################### ##################################################################################################### # 打印日志 logging: level: root: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE com.springms: DEBUG #####################################################################################################2.3 添加 quartz 配置文件(springms-simple-quartz-cluster/src/main/resources/quartz.properties)
org.quartz.scheduler.instanceName = quartzScheduler org.quartz.scheduler.instanceId = AUTO 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.useProperties = false org.quartz.jobStore.clusterCheckinInterval = 20000 org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 10 org.quartz.threadPool.threadPriority = 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true2.4 添加 quartz 任務(wù)配置文件(springms-simple-quartz-cluster/src/main/resources/quartz.xml)
2.5 添加定時(shí)任務(wù)作業(yè)類(springms-simple-quartz-cluster/src/main/java/com/springms/cloud/job/DetailQuartzJobBean.java)com.springms.cloud.job.DetailQuartzJobBean 0/10 * * * * ?
package com.springms.cloud.job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.quartz.QuartzJobBean; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; /** * 定時(shí)任務(wù)作業(yè)類。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/9/18 * */ public class DetailQuartzJobBean extends QuartzJobBean { private String targetObject; private String targetMethod; private ApplicationContext ctx; // 配置中設(shè)定了 // ① targetMethod: 指定需要定時(shí)執(zhí)行scheduleInfoAction中的simpleJobTest()方法 // ② concurrent:對(duì)于相同的JobDetail,當(dāng)指定多個(gè)Trigger時(shí), 很可能第一個(gè)job完成之前, // 第二個(gè)job就開(kāi)始了。指定concurrent設(shè)為false,多個(gè)job不會(huì)并發(fā)運(yùn)行,第二個(gè)job將不會(huì)在第一個(gè)job完成之前開(kāi)始。 // ③ cronExpression:0/10 * * * * ?表示每10秒執(zhí)行一次,具體可參考附表。 // ④ triggers:通過(guò)再添加其他的ref元素可在list中放置多個(gè)觸發(fā)器。 scheduleInfoAction中的simpleJobTest()方法 @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { Object otargetObject = ctx.getBean(targetObject); Method m = null; System.out.println(targetObject + " - " + targetMethod + " - " + ((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date()))); try { m = otargetObject.getClass().getMethod(targetMethod, new Class[] { JobExecutionContext.class }); m.invoke(otargetObject, new Object[] { context }); } catch (SecurityException e) { // Logger.error(e); System.out.println(e.getMessage()); } catch (NoSuchMethodException e) { // Logger.error(e); System.out.println(e.getMessage()); } } catch (Exception e) { System.out.println(e.getMessage()); throw new JobExecutionException(e); } } public void setApplicationContext(ApplicationContext applicationContext) { this.ctx = applicationContext; } public void setTargetObject(String targetObject) { this.targetObject = targetObject; } public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; } }2.6 添加調(diào)度服務(wù)接口(springms-simple-quartz-cluster/src/main/java/com/springms/cloud/service/ISchedulerService.java)
package com.springms.cloud.service; import java.util.Date; import org.quartz.CronExpression; /** * 調(diào)度服務(wù)接口。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/18 * */ public interface ISchedulerService { /** * 根據(jù) Quartz Cron Expression 調(diào)試任務(wù) * * @param cronExpression * Quartz Cron 表達(dá)式,如 "0/10 * * ? * * *"等 */ void schedule(String cronExpression); /** * 根據(jù) Quartz Cron Expression 調(diào)試任務(wù) * * @param name * Quartz CronTrigger名稱 * @param cronExpression * Quartz Cron 表達(dá)式,如 "0/10 * * ? * * *"等 */ void schedule(String name, String cronExpression); /** * 根據(jù) Quartz Cron Expression 調(diào)試任務(wù) * * @param name * Quartz CronTrigger名稱 * @param group * Quartz CronTrigger組 * @param cronExpression * Quartz Cron 表達(dá)式,如 "0/10 * * ? * * *"等 */ void schedule(String name, String group, String cronExpression); /** * 根據(jù) Quartz Cron Expression 調(diào)試任務(wù) * * @param cronExpression * Quartz CronExpression */ void schedule(CronExpression cronExpression); /** * 根據(jù) Quartz Cron Expression 調(diào)試任務(wù) * * @param name * Quartz CronTrigger名稱 * @param cronExpression * Quartz CronExpression */ void schedule(String name, CronExpression cronExpression); /** * 根據(jù) Quartz Cron Expression 調(diào)試任務(wù) * * @param name * Quartz CronTrigger名稱 * @param group * Quartz CronTrigger組 * @param cronExpression * Quartz CronExpression */ void schedule(String name, String group, CronExpression cronExpression); /** * 在startTime時(shí)執(zhí)行調(diào)試一次 * * @param startTime * 調(diào)度開(kāi)始時(shí)間 */ void schedule(Date startTime); void schedule(Date startTime, String group); /** * 在startTime時(shí)執(zhí)行調(diào)試一次 * * @param name * Quartz SimpleTrigger 名稱 * @param startTime * 調(diào)度開(kāi)始時(shí)間 */ void schedule(String name, Date startTime); void schedule(String name, Date startTime, String group); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度 * * @param startTime * 調(diào)度開(kāi)始時(shí)間 * @param endTime * 調(diào)度結(jié)束時(shí)間 */ void schedule(Date startTime, Date endTime); void schedule(Date startTime, Date endTime, String group); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度 * * @param name * Quartz SimpleTrigger 名稱 * @param startTime * 調(diào)度開(kāi)始時(shí)間 * @param endTime * 調(diào)度結(jié)束時(shí)間 */ void schedule(String name, Date startTime, Date endTime); void schedule(String name, Date startTime, Date endTime, String group); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度,重復(fù)執(zhí)行repeatCount次 * * @param startTime * 調(diào)度開(kāi)始時(shí)間 * @param repeatCount * 重復(fù)執(zhí)行次數(shù) */ void schedule(Date startTime, int repeatCount); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度,重復(fù)執(zhí)行repeatCount次 * * @param startTime * 調(diào)度開(kāi)始時(shí)間 * @param endTime * 調(diào)度結(jié)束時(shí)間 * @param repeatCount * 重復(fù)執(zhí)行次數(shù) */ void schedule(Date startTime, Date endTime, int repeatCount); void schedule(Date startTime, Date endTime, int repeatCount, String group); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度,重復(fù)執(zhí)行repeatCount次 * * @param name * Quartz SimpleTrigger 名稱 * @param startTime * 調(diào)度開(kāi)始時(shí)間 * @param endTime * 調(diào)度結(jié)束時(shí)間 * @param repeatCount * 重復(fù)執(zhí)行次數(shù) */ void schedule(String name, Date startTime, Date endTime, int repeatCount); void schedule(String name, Date startTime, Date endTime, int repeatCount, String group); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度,重復(fù)執(zhí)行repeatCount次,每隔repeatInterval秒執(zhí)行一次 * * @param startTime * 調(diào)度開(kāi)始時(shí)間 * * @param repeatCount * 重復(fù)執(zhí)行次數(shù) * @param repeatInterval * 執(zhí)行時(shí)間隔間 */ void schedule(Date startTime, int repeatCount, long repeatInterval); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度,重復(fù)執(zhí)行repeatCount次,每隔repeatInterval秒執(zhí)行一次 * * @param startTime * 調(diào)度開(kāi)始時(shí)間 * @param endTime * 調(diào)度結(jié)束時(shí)間 * @param repeatCount * 重復(fù)執(zhí)行次數(shù) * @param repeatInterval * 執(zhí)行時(shí)間隔間 */ void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval); void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group); /** * 在startTime時(shí)執(zhí)行調(diào)試,endTime結(jié)束執(zhí)行調(diào)度,重復(fù)執(zhí)行repeatCount次,每隔repeatInterval秒執(zhí)行一次 * * @param name * Quartz SimpleTrigger 名稱 * @param startTime * 調(diào)度開(kāi)始時(shí)間 * @param endTime * 調(diào)度結(jié)束時(shí)間 * @param repeatCount * 重復(fù)執(zhí)行次數(shù) * @param repeatInterval * 執(zhí)行時(shí)間隔間 */ void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval); void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group); /** * 暫停觸發(fā)器 * * @param triggerName * 觸發(fā)器名稱 */ void pauseTrigger(String triggerName); /** * 暫停觸發(fā)器 * * @param triggerName * 觸發(fā)器名稱 * @param group * 觸發(fā)器組 */ void pauseTrigger(String triggerName, String group); /** * 恢復(fù)觸發(fā)器 * * @param triggerName * 觸發(fā)器名稱 */ void resumeTrigger(String triggerName); /** * 恢復(fù)觸發(fā)器 * * @param triggerName * 觸發(fā)器名稱 * @param group * 觸發(fā)器組 */ void resumeTrigger(String triggerName, String group); /** * 刪除觸發(fā)器 * * @param triggerName * 觸發(fā)器名稱 * @return */ boolean removeTrigdger(String triggerName); /** * 刪除觸發(fā)器 * * @param triggerName * 觸發(fā)器名稱 * @param group * 觸發(fā)器組 * @return */ boolean removeTrigdger(String triggerName, String group); }2.7 添加調(diào)度服務(wù)實(shí)現(xiàn)類(springms-simple-quartz-cluster/src/main/java/com/springms/cloud/service/impl/SchedulerServiceImpl.java)
package com.springms.cloud.service.impl; import java.text.ParseException; import java.util.Date; import java.util.UUID; import com.springms.cloud.service.ISchedulerService; import org.quartz.CronExpression; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.TriggerKey; import org.quartz.impl.triggers.CronTriggerImpl; import org.quartz.impl.triggers.SimpleTriggerImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * 調(diào)度服務(wù)實(shí)現(xiàn)類。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/18 * */ @Service("schedulerService") public class SchedulerServiceImpl implements ISchedulerService { private static final String NULLSTRING = null; private static final Date NULLDATE = null; @Autowired private Scheduler scheduler; @Autowired private JobDetail jobDetail; @Override public void schedule(String cronExpression) { schedule(NULLSTRING, cronExpression); } @Override public void schedule(String name, String cronExpression) { schedule(name, NULLSTRING, cronExpression); } @Override public void schedule(String name, String group, String cronExpression) { try { schedule(name, group, new CronExpression(cronExpression)); } catch (ParseException e) { throw new IllegalArgumentException(e); } } @Override public void schedule(CronExpression cronExpression) { schedule(NULLSTRING, cronExpression); } @Override public void schedule(String name, CronExpression cronExpression) { schedule(name, NULLSTRING, cronExpression); } @Override public void schedule(String name, String group, CronExpression cronExpression) { if (isValidExpression(cronExpression)) { if (name == null || name.trim().equals("")) { name = UUID.randomUUID().toString(); } CronTriggerImpl trigger = new CronTriggerImpl(); trigger.setCronExpression(cronExpression); TriggerKey triggerKey = new TriggerKey(name, group); trigger.setJobName(jobDetail.getKey().getName()); trigger.setKey(triggerKey); try { scheduler.addJob(jobDetail, true); if (scheduler.checkExists(triggerKey)) { scheduler.rescheduleJob(triggerKey, trigger); } else { scheduler.scheduleJob(trigger); } } catch (SchedulerException e) { throw new IllegalArgumentException(e); } } } @Override public void schedule(Date startTime) { schedule(startTime, NULLDATE); } @Override public void schedule(Date startTime, String group) { schedule(startTime, NULLDATE, group); } @Override public void schedule(String name, Date startTime) { schedule(name, startTime, NULLDATE); } @Override public void schedule(String name, Date startTime, String group) { schedule(name, startTime, NULLDATE, group); } @Override public void schedule(Date startTime, Date endTime) { schedule(startTime, endTime, 0); } @Override public void schedule(Date startTime, Date endTime, String group) { schedule(startTime, endTime, 0, group); } @Override public void schedule(String name, Date startTime, Date endTime) { schedule(name, startTime, endTime, 0); } @Override public void schedule(String name, Date startTime, Date endTime, String group) { schedule(name, startTime, endTime, 0, group); } @Override public void schedule(Date startTime, int repeatCount) { schedule(null, startTime, NULLDATE, 0); } @Override public void schedule(Date startTime, Date endTime, int repeatCount) { schedule(null, startTime, endTime, 0); } @Override public void schedule(Date startTime, Date endTime, int repeatCount, String group) { schedule(null, startTime, endTime, 0, group); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount) { schedule(name, startTime, endTime, 0, 0L); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount, String group) { schedule(name, startTime, endTime, 0, 0L, group); } @Override public void schedule(Date startTime, int repeatCount, long repeatInterval) { schedule(null, startTime, NULLDATE, repeatCount, repeatInterval); } @Override public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval) { schedule(null, startTime, endTime, repeatCount, repeatInterval); } @Override public void schedule(Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) { schedule(null, startTime, endTime, repeatCount, repeatInterval, group); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval) { schedule(name, startTime, endTime, repeatCount, repeatInterval, NULLSTRING); } @Override public void schedule(String name, Date startTime, Date endTime, int repeatCount, long repeatInterval, String group) { if (this.isValidExpression(startTime)) { if (name == null || name.trim().equals("")) { name = UUID.randomUUID().toString(); } TriggerKey triggerKey = new TriggerKey(name, group); SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setKey(triggerKey); trigger.setJobName(jobDetail.getKey().getName()); trigger.setStartTime(startTime); trigger.setEndTime(endTime); trigger.setRepeatCount(repeatCount); trigger.setRepeatInterval(repeatInterval); try { scheduler.addJob(jobDetail, true); if (scheduler.checkExists(triggerKey)) { scheduler.rescheduleJob(triggerKey, trigger); } else { scheduler.scheduleJob(trigger); } } catch (SchedulerException e) { throw new IllegalArgumentException(e); } } } @Override public void pauseTrigger(String triggerName) { pauseTrigger(triggerName, NULLSTRING); } @Override public void pauseTrigger(String triggerName, String group) { try { scheduler.pauseTrigger(new TriggerKey(triggerName, group));// 停止觸發(fā)器 } catch (SchedulerException e) { throw new RuntimeException(e); } } @Override public void resumeTrigger(String triggerName) { resumeTrigger(triggerName, NULLSTRING); } @Override public void resumeTrigger(String triggerName, String group) { try { scheduler.resumeTrigger(new TriggerKey(triggerName, group));// 重啟觸發(fā)器 } catch (SchedulerException e) { throw new RuntimeException(e); } } @Override public boolean removeTrigdger(String triggerName) { return removeTrigdger(triggerName, NULLSTRING); } @Override public boolean removeTrigdger(String triggerName, String group) { TriggerKey triggerKey = new TriggerKey(triggerName, group); try { scheduler.pauseTrigger(triggerKey);// 停止觸發(fā)器 return scheduler.unscheduleJob(triggerKey);// 移除觸發(fā)器 } catch (SchedulerException e) { throw new RuntimeException(e); } } private boolean isValidExpression(final CronExpression cronExpression) { CronTriggerImpl trigger = new CronTriggerImpl(); trigger.setCronExpression(cronExpression); Date date = trigger.computeFirstFireTime(null); return date != null && date.after(new Date()); } private boolean isValidExpression(final Date startTime) { SimpleTriggerImpl trigger = new SimpleTriggerImpl(); trigger.setStartTime(startTime); Date date = trigger.computeFirstFireTime(null); return date != null && date.after(new Date()); } }2.8 添加調(diào)度的任務(wù)類(springms-simple-quartz-cluster/src/main/java/com/springms/cloud/task/ScheduleTask.java)
package com.springms.cloud.task; import org.quartz.JobExecutionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.stereotype.Component; /** * 調(diào)度的任務(wù)。 * * testScheduleTask 字符串名稱在 quartz.xml 中配置為屬性 targetObject 的 value 值。 * sayHello 方法名稱在 quartz.xml 中配置為屬性 targetMethod 的 value 值。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/18 * */ @Configuration @Component("testScheduleTask") @EnableScheduling public class ScheduleTask { private static final Logger Logger = LoggerFactory.getLogger(ScheduleTask.class); public void sayHello(JobExecutionContext context){ Logger.info("==== sayHello 123456789 ===="); System.out.println("==== sayHello 123456789 ===="); } }2.9 添加Web層Controller測(cè)試類(springms-simple-quartz-cluster/src/main/java/com/springms/cloud/controller/QuartzClusterController.java)
package com.springms.cloud.controller; import com.springms.cloud.service.ISchedulerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; /** * QuartzCluster 分布式修改調(diào)度服務(wù)的Controller。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/18 * */ @RestController public class QuartzClusterController { @Autowired private ISchedulerService schedulerService; /** * 每隔多少秒調(diào)度一次。 * * @param seconds * @return */ @GetMapping("/modify/{seconds}") public String modifyStartQuartz(@PathVariable String seconds){ // eg: 0/10 * * ? * * * try { schedulerService.schedule("testJobTrigger", "DEFAULT", "0/" + seconds + " * * ? * * *"); } catch (Exception e) { return "Failed"; } return "Successful"; } }2.10 執(zhí)行 Quartz 的 11 張表入數(shù)據(jù)庫(kù)(springms-simple-quartz-cluster/quartz-tables.log)
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) ); 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) ); 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) ); 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(200) 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) ); 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) ); 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), FOREIGN KEY (SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) REFERENCES QRTZ_TRIGGERS(SCHED_NAME,TRIGGER_NAME,TRIGGER_GROUP) ); 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) ); CREATE TABLE QRTZ_PAUSED_TRIGGER_GRPS ( SCHED_NAME VARCHAR(120) NOT NULL, TRIGGER_GROUP VARCHAR(200) NOT NULL, PRIMARY KEY (SCHED_NAME,TRIGGER_GROUP) ); 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) ); 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) ); CREATE TABLE QRTZ_LOCKS ( SCHED_NAME VARCHAR(120) NOT NULL, LOCK_NAME VARCHAR(40) NOT NULL, PRIMARY KEY (SCHED_NAME,LOCK_NAME) ); commit;2.11 添加 Quartz-Cluster 啟動(dòng)類(springms-simple-quartz-cluster/src/main/java/com/springms/cloud/SimpleQuartzClusterApplication.java)
package com.springms.cloud; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; /** * 簡(jiǎn)單 Quartz-Cluster 微服務(wù),支持集群分布式,并支持動(dòng)態(tài)修改 Quartz 任務(wù)的 cronExpression 執(zhí)行時(shí)間。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/9/18 * */ @SpringBootApplication @ImportResource("quartz.xml") public class SimpleQuartzClusterApplication { private static final Logger Logger = LoggerFactory.getLogger(SimpleQuartzClusterApplication.class); public static void main(String[] args) { Logger.info("簡(jiǎn)單Quartz-Cluster微服務(wù)入口函數(shù)編碼-" + System.getProperty("file.encoding")); SpringApplication.run(SimpleQuartzClusterApplication.class, args); System.out.println("【【【【【【 簡(jiǎn)單Quartz-Cluster微服務(wù) 】】】】】】已啟動(dòng)."); } }三、測(cè)試
/**************************************************************************************** 一、簡(jiǎn)單 Quartz-Cluster 微服務(wù),支持集群分布式,并支持動(dòng)態(tài)修改 Quartz 任務(wù)的 cronExpression 執(zhí)行時(shí)間: 1、添加 Quartz 相關(guān)配置文件; 2、啟動(dòng) springms-simple-quartz-cluster 模塊服務(wù),啟動(dòng)1個(gè)端口(8395); 3、然后查看日志, ScheduleTask 類的 sayHello 方法被有規(guī)律的調(diào)用,并打印日志出來(lái); 4、啟動(dòng) springms-simple-quartz-cluster 模塊服務(wù),再啟動(dòng)2個(gè)端口(8396、8397); 5、然后看到 3 臺(tái)服務(wù)器只有 1 臺(tái)服務(wù)器調(diào)用了 sayHello 方法,因此 Quartz 的集群分布式也算是部署成功了; ****************************************************************************************/ /**************************************************************************************** 二、簡(jiǎn)單 Quartz-Cluster 微服務(wù),支持集群分布式,并支持動(dòng)態(tài)修改 Quartz 任務(wù)的 cronExpression 執(zhí)行時(shí)間(動(dòng)態(tài)修改定時(shí)任務(wù)的 cronExpression 時(shí)間表達(dá)式): 1、添加 Quartz 相關(guān)配置文件; 2、啟動(dòng) springms-simple-quartz-cluster 模塊服務(wù),啟動(dòng)3個(gè)端口(8395、8396、8397); 3、然后看到 3 臺(tái)服務(wù)器只有 1 臺(tái)服務(wù)器調(diào)用了 sayHello 方法打印了日志,因此 Quartz 的集群分布式也算是部署成功了; 4、然后新起網(wǎng)頁(yè)輸入 http://localhost:8395/modify/5 修改定時(shí)任務(wù)的觸發(fā)時(shí)間; 5、再等一會(huì)兒就看到 3 臺(tái)服務(wù)器只有 1 臺(tái)服務(wù)器每隔 5 秒調(diào)用一次 sayHello 方法,因此修改定時(shí)任務(wù)的克隆表達(dá)式也算是成功了; ****************************************************************************************/ /**************************************************************************************** 三、簡(jiǎn)單 Quartz-Cluster 微服務(wù),支持集群分布式,并支持動(dòng)態(tài)修改 Quartz 任務(wù)的 cronExpression 執(zhí)行時(shí)間(動(dòng)態(tài)刪除其中 1 臺(tái)活躍 Quartz 服務(wù)器,然后剩下的其中 1 臺(tái)自動(dòng)接替): 1、添加 Quartz 相關(guān)配置文件; 2、啟動(dòng) springms-simple-quartz-cluster 模塊服務(wù),啟動(dòng)3個(gè)端口(8395、8396、8397); 3、然后看到 3 臺(tái)服務(wù)器只有 1 臺(tái)服務(wù)器調(diào)用了 sayHello 方法打印了日志,因此 Quartz 的集群分布式也算是部署成功了; 4、然后關(guān)閉 1 臺(tái)活躍 Quartz 服務(wù)器; 5、再等一會(huì)兒就看到 2 臺(tái)服務(wù)器中的 1 臺(tái)服務(wù)器每隔一定的時(shí)間調(diào)用一次 sayHello 方法; ****************************************************************************************/四、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/70458.html
摘要:加載配置文件失敗加載配置文件失敗添加定時(shí)調(diào)度任務(wù)定時(shí)調(diào)度任務(wù)添加定時(shí)調(diào)度任務(wù)定時(shí)調(diào)度任務(wù)執(zhí)行的張表入數(shù)據(jù)庫(kù)添加啟動(dòng)類簡(jiǎn)單微服務(wù),采用注解配置分布式集群。 SpringCloud(第 054 篇)簡(jiǎn)單 Quartz-Cluster 微服務(wù),采用注解配置 Quartz 分布式集群 - 一、大致介紹 1、因網(wǎng)友提到有沒(méi)有采用注解式配置的Quartz例子,因此本人就貼上了這樣一個(gè)樣例; 2、至...
摘要:當(dāng)前時(shí)間打印當(dāng)前時(shí)間定時(shí)任務(wù)觸發(fā),操作多個(gè)添加數(shù)據(jù),事務(wù)中任一異常,都可以正常導(dǎo)致數(shù)據(jù)回滾。當(dāng)前時(shí)間當(dāng)前時(shí)間添加微服務(wù)啟動(dòng)類注解式配置定時(shí)任務(wù),不支持任務(wù)調(diào)度。 SpringCloud(第 046 篇)注解式Schedule配置定時(shí)任務(wù),不支持任務(wù)調(diào)度 - 一、大致介紹 1、很多時(shí)候我們需要隔一定的時(shí)間去執(zhí)行某個(gè)任務(wù),為了實(shí)現(xiàn)這樣的需求通常最普通的方式就是利用多線程來(lái)實(shí)現(xiàn); 2、但是有...
摘要:是一個(gè)相對(duì)比較新的微服務(wù)框架,年才推出的版本雖然時(shí)間最短但是相比等框架提供的全套的分布式系統(tǒng)解決方案。提供線程池不同的服務(wù)走不同的線程池,實(shí)現(xiàn)了不同服務(wù)調(diào)用的隔離,避免了服務(wù)器雪崩的問(wèn)題。通過(guò)互相注冊(cè)的方式來(lái)進(jìn)行消息同步和保證高可用。 Spring Cloud 是一個(gè)相對(duì)比較新的微服務(wù)框架,...
閱讀 2304·2023-04-25 14:22
閱讀 3743·2021-11-15 18:12
閱讀 1299·2019-08-30 15:44
閱讀 3221·2019-08-29 15:37
閱讀 651·2019-08-29 13:49
閱讀 3463·2019-08-26 12:11
閱讀 881·2019-08-23 18:28
閱讀 1590·2019-08-23 14:55