赞
踩
Quartz是一个开源的任务调度服务,它可以独立使用,也可与其它的Java EE,Java SE应用整合使用。无论是执行十个,一百个工作的简单任务,还是执行成百上千个工作的复杂调度任务都可以使用Quartz来实现,此外,Quartz还提供了很多企业级应用的特色,如JTA事务支持及集群。
如果你的应用中有任务需要在特定的时间执行,亦或周期循环执行,则Quartz也许是你理想的选择。
corn表达式是:由若干数字、空格、符号按一定的规则,组成的一组字符串,从而表达时间的信息。
Cron 表达式是一个字符串,分为 6 或 7 个域,每一个域代表一个含义
Cron 有如下两种语法格式:
字段 | 是否必填 | 允许填写的值 | 允许的通配符 |
---|---|---|---|
秒 | 是 | 0-59 | , - * / |
分 | 是 | 0-59 | , - * / |
小时 | 是 | 0-23 | , - * / |
日 | 是 | 1-31 | , - * ? / L W |
月 | 是 | 1-12 or JAN-DEC | , - * / |
周 | 是 | 1-7 or SUN-SAT | , - * ? / L # |
年 | 否 | 留空 或 1970-2099 | , - * / |
通配符说明:
星号(*)
:表示所有值
~~~~
例如:在分的字段上设置 “*”,表示每一分钟都会触发。
减号(-)
:表示区间
~~~~~~
例如在小时上设置 “10-12”,表示 10,11,12点都会触发。
逗号(,)
:列出枚举
~~~~~~
例如在分钟里,"5,15"表示5分钟和20分钟触发
斜杠(/)
:指定增量
~~~~~~~
例如在分钟里,"3/15”表示从3分钟开始,没隔15分钟执行一次
问号(?)
: 只在日期域和星期域中使用,表示忽略该字段
~~~~
例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ?
L: 表示last,例如星期中表示7或SAT,月份中表示最后一天31或30,6L表示这个月倒数第6天,FRIL表示一个星期五
W: 只能用在月份中,表示最接近指定天的工作日
#: 只能用在星期中,表示这个月的第几个周几,例如6#3表示这个月的第3个周五
可通过在线生成Cron表达式的工具:https://www.bejson.com/othertools/cron/来生成自己想要的表达式。
常用示例:
表达式 | 含义 |
---|---|
0 * * * * ? | 每1分钟触发一次 |
0 0 * * * ? | 每天每1小时触发一次 |
0 0 10 * * ? | 每天10点触发一次 |
0 * 14 * * ? | 在每天下午2点到下午2:59期间的每1分钟触发 |
0 30 9 1 * ? | 每月1号上午9点半 |
0 15 10 15 * ? | 每月15日上午10:15触发 |
*/5 * * * * ? | 每隔5秒执行一次 |
0 */1 * * * ? | 每隔1分钟执行一次 |
0 0 5-15 * * ? | 每天5-15点整点触发 |
0 0/3 * * * ? | 每三分钟触发一次 |
0 0 0 1 * ? | 每月1号凌晨执行一次 |
【示例】创建SpringBoot项目并整合Quartz框架,实现定时任务功能。
(1)创建SpringBoot项目,项目结构如下图:
(2)使用Maven添加依赖文件
在pom.xml配置信息文件中,添加SpringBoot整合Quartz框架的相关依赖:
<!-- SpringBoot 整合 Quartz 定时任务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
(1)实现定时任务业务逻辑
自定义一个job。
package com.by.job; import org.quartz.JobExecutionException; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; @Component public class PrintTimeJob { //任务 public void printTime() throws JobExecutionException { //创建格式化日期对象 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //打印当前时刻 System.out.println("现在时刻:"+simpleDateFormat.format(new Date())); } }
(2)配置定时任务
编写quartz配置类。
package com.by.config; import com.by.job.PrintTimeJob; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.quartz.CronTriggerFactoryBean; import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import java.util.Objects; @Configuration public class QuartzConfig { @Bean //job:要做的事 public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean( PrintTimeJob printTimeJob){ MethodInvokingJobDetailFactoryBean JobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean(); //绑定要执行的job类 JobDetailFactoryBean.setTargetObject(printTimeJob); //绑定要定时执行的方法 JobDetailFactoryBean.setTargetMethod("printTime"); return JobDetailFactoryBean; } @Bean //trigger:什么时候做 public CronTriggerFactoryBean cronTriggerFactoryBean( MethodInvokingJobDetailFactoryBean JobDetailFactoryBean){ CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean(); //配置cron表达时,设置定时规则 triggerFactoryBean.setCronExpression("* * * * * ?"); //绑定job对象 triggerFactoryBean.setJobDetail(Objects.requireNonNull(JobDetailFactoryBean.getObject())); return triggerFactoryBean; } @Bean //scheduler:将Job和Trigger组装起来,使定时任务被真正执行 public SchedulerFactoryBean schedulerFactoryBean( CronTriggerFactoryBean triggerFactoryBean){ SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean(); //绑定trigger对象 schedulerFactoryBean.setTriggers(triggerFactoryBean.getObject()); return schedulerFactoryBean; } }
(3)编写启动类
package com.by;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QuartzTestApp {
public static void main(String[] args) {
SpringApplication.run(QuartzTestApp.class,args);
}
}
(4)测试
完成上述代码与配置后,就可以启动 SpringBoot 项目,进行运行测试。
执行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。