quartz 动态修改时间_SpringBoot 集成Quartz发布、修改、暂停、删除定时任务

前言

>>>SpringBoot自带schedule

SpringBoot本身支持表达式等多种定时任务,使用起来也很方便,但是如果使用复杂的任务操作时,SpringBoot自带的稍显不足,使用SpringBoot自带的定时任务, 只需要在程序启动的时候加上@EnableScheduling

@Scheduled(cron="0/20 * * * * ?")public void task(){ System.out.println("task - 20秒执行一次");}

使用十分简单,本文不过多陈述

>>>为什么使用Quartz

多任务情况下,quartz更容易管理,可以实现动态配置 ,可随时删除和修改定时任务,方便使用

1、SpringBoot集成Quartz

项目目录:

a75d3f848c00074e1fe4afae45238719.png

由于一些quartz集成需要导入quartz自带的一些mysql库,使用起来稍显负复杂,本文采用自己创建任务库来管理简单的定时任务

pom.xml

<?xml version="1.0" encoding="UTF-8"?> learn com.lss1.04.0.02.2.1 quartz org.springframework.boot spring-boot-starter mysql mysql-connector-java org.springframework.boot spring-boot-starter-data-jpa org.quartz-scheduler quartz ${quertz.version}org.springframework.boot spring-boot-maven-plugin 

application.yml

spring: datasource: url: jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf8 username: root password: 12345678 driverClassName: com.mysql.jdbc.Driver jpa: database: mysql show-sql: true hibernate: ddl-auto: update naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl database-platform: org.hibernate.dialect.MySQL5Dialect quartz: #相关属性配置 properties: org: quartz: # dataSource: # default: # driver: com.mysql.jdbc.Driver # URL: jdbc:mysql://localhost:3306/jobconfig?useUnicode=true&characterEncoding=utf8 # user: root # password: 12345678 # maxConnections: 5 scheduler: instanceName: DefaultQuartzScheduler instanceId: AUTO jobStore: class: org.quartz.impl.jdbcjobstore.JobStoreTX driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate tablePrefix: qrtz_ isClustered: false clusterCheckinInterval: 10000 useProperties: true threadPool: class: org.quartz.simpl.SimpleThreadPool threadCount: 10 threadPriority: 5 threadsInheritContextClassLoaderOfInitializingThread: true #数据库方式 job-store-type: JDBC #初始化表结构 jdbc: initialize-schema: NEVER

2、项目配置

2.1、新建ScheduleQuartzJob类,实现quartz的Job接口

package com.lss.job;import lombok.extern.slf4j.Slf4j;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;@Slf4jpublic class ScheduleQuartzJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { String group = context.getJobDetail().getJobDataMap().get("group").toString(); String name = context.getJobDetail().getJobDataMap().get("name").toString(); log.info("执行了task...group:{}, name:{}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值