赞
踩
先说为什么会用Quartz吧, 业务需求是在一个时间段比如 12:00---16:00,开始前一个小时需要提醒一次用户,结束前一个小时需要用户,12:00整的时候也需要提醒一次用户,并且可能多个存在多个这样相同的任务.(所以就需要一个任务时间不会错乱的,timertask可能会导致任务时间错乱,所以首先就排除,quartz每一个任务都是一个新的线程,因为使用到了线程池+时间轮),整合如下:
1.导入依赖
- <!--spring与quartz依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-quartz</artifactId>
- <version>2.6.6</version>
- </dependency>
因为这里我选择了把任务持久化到数据库,防止宕机过后任务丢失(记得更换自己的数据库账号密码)
- #调度配置
- #调度器实例名称
- org.quartz.scheduler.instanceName=SsmScheduler
- #调度器实例编号自动生成
- org.quartz.scheduler.instanceId=AUTO
- #是否在Quartz执行一个job前使用UserTransaction
- org.quartz.scheduler.wrapJobExecutionInUserTransaction=false
-
- #线程池配置
- #线程池的实现类
- org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
- #线程池中的线程数量
- org.quartz.threadPool.threadCount=10
- #线程优先级
- org.quartz.threadPool.threadPriority=5
- #配置是否启动自动加载数据库内的定时任务,默认true
- org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true
- #是否设置为守护线程,设置后任务将不会执行
- #org.quartz.threadPool.makeThreadsDaemons=true
-
- #持久化方式配置
- #JobDataMaps是否都为String类型
- org.quartz.jobStore.useProperties=true
- #数据表的前缀,默认QRTZ_
- org.quartz.jobStore.tablePrefix=qrtz_
- #最大能忍受的触发超时时间
- org.quartz.jobStore.misfireThreshold=60000
- #是否以集群方式运行
- org.quartz.jobStore.isClustered=true
- #调度实例失效的检查时间间隔,单位毫秒
- org.quartz.jobStore.clusterCheckinInterval=2000
- #数据保存方式为数据库持久化
- org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
- #数据库代理类,一般org.quartz.impl.jdbcjobstore.StdJDBCDelegate可以满足大部分数据库
- org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
- #数据库别名 随便取
- org.quartz.jobStore.dataSource=qzDS
-
- #数据库连接池,将其设置为druid
- org.quartz.dataSource.qzDS.connectionProvider.class=com.ruoyi.system.config.DruidConnectionProvider
- #数据库引擎
- org.quartz.dataSource.qzDS.driver=com.mysql.cj.jdbc.Driver
- #数据库连接
- org.quartz.dataSource.qzDS.URL=jdbc:mysql://127.0.0.1:3306/ry-cloud?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
- #数据库用户
- org.quartz.dataSource.qzDS.user=root
- #数据库密码
- org.quartz.dataSource.qzDS.password=abc123
- #允许最大连接
- org.quartz.dataSource.qzDS.maxConnection=5
- #验证查询sql,可以不设置
- #org.quartz.dataSource.qzDS.validationQuery=select 0 from dual
- #数据库连接池,将其设置为druid
添加数据库表,这里官方已经提供了sql文件,官方网站:http://quartz-scheduler.org/
路径:quartz-2.3.0-SNAPSHOT\src\org\quartz\impl\jdbcjobstore\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(190) NOT NULL,
- JOB_GROUP VARCHAR(190) 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(190) NOT NULL,
- TRIGGER_GROUP VARCHAR(190) NOT NULL,
- JOB_NAME VARCHAR(190) NOT NULL,
- JOB_GROUP VARCHAR(190) 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(190) 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(190) NOT NULL,
- TRIGGER_GROUP VARCHAR(190) 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(190) NOT NULL,
- TRIGGER_GROUP VARCHAR(190) 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(190) NOT NULL,
- TRIGGER_GROUP VARCHAR(190) 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(190) NOT NULL,
- TRIGGER_GROUP VARCHAR(190) 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(190) 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(190) 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(190) NOT NULL,
- TRIGGER_GROUP VARCHAR(190) NOT NULL,
- INSTANCE_NAME VARCHAR(190) 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(190) NULL,
- JOB_GROUP VARCHAR(190) 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(190) 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;
配置连接池(需要导入druid依赖,上面忘了说),
-
-
- import com.alibaba.druid.pool.DruidDataSource;
- import com.alibaba.druid.pool.DruidPooledConnection;
- import org.quartz.SchedulerException;
- import org.quartz.utils.ConnectionProvider;
-
- import java.sql.SQLException;
-
- public class DruidConnectionProvider implements ConnectionProvider {
-
- /**
- * 常量配置,与quartz.properties文件的key保持一致(去掉前缀),同时提供set方法,Quartz框架自动注入值。
- * @return
- * @throws SQLException
- */
-
- //JDBC驱动
- public String driver;
- //JDBC连接串
- public String URL;
- //数据库用户名
- public String user;
- //数据库用户密码
- public String password;
- //数据库最大连接数
- public int maxConnection;
- //数据库SQL查询每次连接返回执行到连接池,以确保它仍然是有效的。
- public String validationQuery;
-
- private boolean validateOnCheckout;
-
- private int idleConnectionValidationSeconds;
-
- public String maxCachedStatementsPerConnection;
-
- private String discardIdleConnectionsSeconds;
-
- public static final int DEFAULT_DB_MAX_CONNECTIONS = 10;
-
- public static final int DEFAULT_DB_MAX_CACHED_STATEMENTS_PER_CONNECTION = 120;
-
- //Druid连接池
- private DruidDataSource datasource;
-
- @Override
- public DruidPooledConnection getConnection() throws SQLException {
- return datasource.getConnection();
- }
-
- @Override
- public void shutdown() throws SQLException {
- datasource.close();
- }
-
- @Override
- public void initialize() throws SQLException {
- if (this.URL == null) {
- throw new SQLException("DBPool could not be created: DB URL cannot be null");
- }
-
- if (this.driver == null) {
- throw new SQLException("DBPool driver could not be created: DB driver class name cannot be null!");
- }
-
- if (this.maxConnection < 0) {
- throw new SQLException("DBPool maxConnectins could not be created: Max connections must be greater than zero!");
- }
-
- datasource = new DruidDataSource();
- try{
- datasource.setDriverClassName(this.driver);
- } catch (Exception e) {
- try {
- throw new SchedulerException("Problem setting driver class name on datasource: " + e.getMessage(), e);
- } catch (SchedulerException e1) {
- }
- }
-
- datasource.setUrl(this.URL);
- datasource.setUsername(this.user);
- datasource.setPassword(this.password);
- datasource.setMaxActive(this.maxConnection);
- datasource.setMinIdle(1);
- datasource.setMaxWait(0);
- datasource.setMaxPoolPreparedStatementPerConnectionSize(DEFAULT_DB_MAX_CONNECTIONS);
-
- if (this.validationQuery != null) {
- datasource.setValidationQuery(this.validationQuery);
- if(!this.validateOnCheckout)
- datasource.setTestOnReturn(true);
- else
- datasource.setTestOnBorrow(true);
- datasource.setValidationQueryTimeout(this.idleConnectionValidationSeconds);
- }
- }
-
- public String getDriver() {
- return driver;
- }
-
- public void setDriver(String driver) {
- this.driver = driver;
- }
-
- public String getURL() {
- return URL;
- }
-
- public void setURL(String URL) {
- this.URL = URL;
- }
-
- public String getUser() {
- return user;
- }
-
- public void setUser(String user) {
- this.user = user;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public int getMaxConnection() {
- return maxConnection;
- }
-
- public void setMaxConnection(int maxConnection) {
- this.maxConnection = maxConnection;
- }
-
- public String getValidationQuery() {
- return validationQuery;
- }
-
- public void setValidationQuery(String validationQuery) {
- this.validationQuery = validationQuery;
- }
-
- public boolean isValidateOnCheckout() {
- return validateOnCheckout;
- }
-
- public void setValidateOnCheckout(boolean validateOnCheckout) {
- this.validateOnCheckout = validateOnCheckout;
- }
-
- public int getIdleConnectionValidationSeconds() {
- return idleConnectionValidationSeconds;
- }
-
- public void setIdleConnectionValidationSeconds(int idleConnectionValidationSeconds) {
- this.idleConnectionValidationSeconds = idleConnectionValidationSeconds;
- }
-
- public DruidDataSource getDatasource() {
- return datasource;
- }
-
- public void setDatasource(DruidDataSource datasource) {
- this.datasource = datasource;
- }
-
- public String getDiscardIdleConnectionsSeconds() {
- return discardIdleConnectionsSeconds;
- }
-
- public void setDiscardIdleConnectionsSeconds(String discardIdleConnectionsSeconds) {
- this.discardIdleConnectionsSeconds = discardIdleConnectionsSeconds;
- }
- }
这里配好过后要更改一下上面quartz.properties里面的这个,后面路径写你自己的
- #数据库连接池,将其设置为druid
- org.quartz.dataSource.qzDS.connectionProvider.class=com.ruoyi.system.config.DruidConnectionPr
注册调度工厂
- package com.ruoyi.system.config;
-
- import org.quartz.Scheduler;
- import org.quartz.SchedulerException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.PropertiesFactoryBean;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.scheduling.quartz.SchedulerFactoryBean;
-
- import java.io.IOException;
-
- @Configuration
- public class QuartzConfig {
-
- @Autowired
- private QuartzJobFactory jobFactory;
-
-
- @Bean
- public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
- //获取配置属性
- PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
- propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
- //在quartz.properties中的属性被读取并注入后再初始化对象
- propertiesFactoryBean.afterPropertiesSet();
- //创建SchedulerFactoryBean
- SchedulerFactoryBean factory = new SchedulerFactoryBean();
- factory.setQuartzProperties(propertiesFactoryBean.getObject());
- factory.setJobFactory(jobFactory);//支持在JOB实例中注入其他的业务对象
- factory.setApplicationContextSchedulerContextKey("applicationContextKey");
- factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时,会等待所有已经启动的quartz job结束后spring才能完全shutdown。
- factory.setOverwriteExistingJobs(false);//是否覆盖己存在的Job
- factory.setStartupDelay(10);//QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动
-
- return factory;
- }
-
- /**
- * 通过SchedulerFactoryBean获取Scheduler的实例
- * @return
- * @throws IOException
- * @throws SchedulerException
- */
- @Bean(name = "scheduler")
- public Scheduler scheduler() throws IOException, SchedulerException {
- Scheduler scheduler = schedulerFactoryBean().getScheduler();
- return scheduler;
- }
- }
注册任务工厂
-
- 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;
- import org.springframework.stereotype.Component;
-
- @Component
- public class QuartzJobFactory extends AdaptableJobFactory {
-
- @Autowired
- private AutowireCapableBeanFactory capableBeanFactory;
-
- @Override
- protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
- //调用父类的方法
- Object jobInstance = super.createJobInstance(bundle);
- //进行注入
- capableBeanFactory.autowireBean(jobInstance);
- return jobInstance;
- }
- }
新建一个任务job,你想定时执行的任务
- package com.ruoyi.system.task;
-
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- public class Msmjob implements Job {
-
- private static final Logger log = LoggerFactory.getLogger(Msmjob.class);
- @Override
- public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
- System.out.println("您的核酸任务于一个小时候开启,请及时完成核算任务");
- }
- }
添加service
- import java.util.Map;
-
-
- public interface QuartzService {
-
- /**
- * 添加任务可以传参数
- * @param clazzName
- * @param jobName
- * @param groupName
- * @param cronExp
- * @param param
- */
- void addJob(String clazzName, String jobName, String groupName, String cronExp, Map<String, Object> param);
-
- /**
- * 暂停任务
- * @param jobName
- * @param groupName
- */
- void pauseJob(String jobName, String groupName);
-
- /**
- * 恢复任务
- * @param jobName
- * @param groupName
- */
- void resumeJob(String jobName, String groupName);
-
- /**
- * 立即运行一次定时任务
- * @param jobName
- * @param groupName
- */
- void runOnce(String jobName, String groupName);
-
- /**
- * 更新任务
- * @param jobName
- * @param groupName
- * @param cronExp
- * @param param
- */
- void updateJob(String jobName, String groupName, String cronExp, Map<String, Object> param);
-
- /**
- * 删除任务
- * @param jobName
- * @param groupName
- */
- void deleteJob(String jobName, String groupName);
-
- /**
- * 启动所有任务
- */
- void startAllJobs();
-
- /**
- * 暂停所有任务
- */
- void pauseAllJobs();
-
- /**
- * 恢复所有任务
- */
- void resumeAllJobs();
-
- /**
- * 关闭所有任务
- */
- void shutdownAllJobs();
-
- }
添加实现类
- package com.ruoyi.system.service.impl;
-
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.ruoyi.system.service.QuartzService;
- import org.quartz.*;
- import org.quartz.impl.matchers.GroupMatcher;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
- import org.springframework.scheduling.quartz.JobDetailFactoryBean;
- import org.springframework.scheduling.quartz.SchedulerFactoryBean;
- import org.springframework.stereotype.Service;
-
- import javax.sql.DataSource;
- import java.text.SimpleDateFormat;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
- /**
-
- */
- @Service
- public class QuartzServiceImpl implements QuartzService {
- private static final Logger log = LoggerFactory.getLogger(QuartzServiceImpl.class);
-
- @Autowired
- private Scheduler scheduler;
-
- @Override
- public void addJob(String clazzName, String jobName, String groupName, String cronExp, Map<String, Object> param) {
- try {
- // 启动调度器,默认初始化的时候已经启动
- // scheduler.start();
- //构建job信息
- Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(clazzName);
- JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, groupName).build();
- //表达式调度构建器(即任务执行的时间)
- CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExp);
- //按新的cronExpression表达式构建一个新的trigger
- CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, groupName).withSchedule(scheduleBuilder).build();
- //获得JobDataMap,写入数据
- if (param != null) {
- trigger.getJobDataMap().putAll(param);
- }
- scheduler.scheduleJob(jobDetail, trigger);
- } catch (Exception e) {
- log.error("创建任务失败", e);
- }
- }
-
- @Override
- public void pauseJob(String jobName, String groupName) {
- try {
- scheduler.pauseJob(JobKey.jobKey(jobName, groupName));
- } catch (SchedulerException e) {
- log.error("暂停任务失败", e);
- }
- }
-
- @Override
- public void resumeJob(String jobName, String groupName) {
- try {
- scheduler.resumeJob(JobKey.jobKey(jobName, groupName));
- } catch (SchedulerException e) {
- log.error("恢复任务失败", e);
- }
- }
-
- @Override
- public void runOnce(String jobName, String groupName) {
- try {
- scheduler.triggerJob(JobKey.jobKey(jobName, groupName));
- } catch (SchedulerException e) {
- log.error("立即运行一次定时任务失败", e);
- }
- }
-
- @Override
- public void updateJob(String jobName, String groupName, String cronExp, Map<String, Object> param) {
- try {
- TriggerKey triggerKey = TriggerKey.triggerKey(jobName, groupName);
- CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
- if (cronExp != null) {
- // 表达式调度构建器
- CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExp);
- // 按新的cronExpression表达式重新构建trigger
- trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
- }
- //修改map
- if (param != null) {
- trigger.getJobDataMap().putAll(param);
- }
- // 按新的trigger重新设置job执行
- scheduler.rescheduleJob(triggerKey, trigger);
- } catch (Exception e) {
- log.error("更新任务失败", e);
- }
- }
-
- @Override
- public void deleteJob(String jobName, String groupName) {
- try {
- //暂停、移除、删除
- scheduler.pauseTrigger(TriggerKey.triggerKey(jobName, groupName));
- scheduler.unscheduleJob(TriggerKey.triggerKey(jobName, groupName));
- scheduler.deleteJob(JobKey.jobKey(jobName, groupName));
- } catch (Exception e) {
- log.error("删除任务失败", e);
- }
- }
-
- @Override
- public void startAllJobs() {
- try {
- scheduler.start();
- } catch (Exception e) {
- log.error("开启所有的任务失败", e);
- }
- }
-
- @Override
- public void pauseAllJobs() {
- try {
- scheduler.pauseAll();
- } catch (Exception e) {
- log.error("暂停所有任务失败", e);
- }
- }
-
- @Override
- public void resumeAllJobs() {
- try {
- scheduler.resumeAll();
- } catch (Exception e) {
- log.error("恢复所有任务失败", e);
- }
- }
-
- @Override
- public void shutdownAllJobs() {
- try {
-
- if (!scheduler.isShutdown()) {
- // 需谨慎操作关闭scheduler容器
- // scheduler生命周期结束,无法再 start() 启动scheduler
- scheduler.shutdown(true);
- }
- } catch (Exception e) {
- log.error("关闭所有的任务失败", e);
- }
- }
- }
这里再提供一个 Date类型转换Cron表达式的工具类,这样就能自定义时间了.
- package com.ruoyi.system.util;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- public class TimeUtil {
-
- private static String formatDateByPattern(Date date, String dateFormat){
- SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
- String formatTimeStr = null;
- if (date != null) {
- formatTimeStr = sdf.format(date);
- }
- return formatTimeStr;
- }
-
- /**
- * Date转为cron表达式
- * @param date
- * @return
- */
- public static String getCron(java.util.Date date){
- String dateFormat="ss mm HH dd MM ? yyyy";
- return formatDateByPattern(date, dateFormat);
- }
- }
接下来只需要在你使用的地方注入service调用方法即可
- @Autowired
- private QuartzService quartzService;
-
-
-
-
- quartzService.addJob(Msmjob.class.getName(),Msmjob.class.getName(),
- "duanxin", TimeUtil.getCron(hsBuildingTask.getStartTime()),null);
数据库也有了
才疏学浅,有错误欢迎指出
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。