在 Spring + SpringMVC 环境中,通常来讲,要实现定时任务,咱们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另外一种就是使用第三方框架 Quartz ,Spring Boot 源自 Spring+SpringMVC ,所以自然具有这两个 Spring 中的定时任务实现策略,固然也支持 Quartz,本文咱们就来看下 Spring Boot 中两种定时任务的实现方式。java
使用 @Scheduled 很是容易,直接建立一个 Spring Boot 项目,而且添加 web 依赖 spring-boot-starter-web
,项目建立成功后,添加 @EnableScheduling
注解,开启定时任务:web
@SpringBootApplication @EnableScheduling public class ScheduledApplication { public static void main(String[] args) { SpringApplication.run(ScheduledApplication.class, args); } }
接下来配置定时任务:spring
@Scheduled(fixedRate = 2000) public void fixedRate() { System.out.println("fixedRate>>>"+new Date()); } @Scheduled(fixedDelay = 2000) public void fixedDelay() { System.out.println("fixedDelay>>>"+new Date()); } @Scheduled(initialDelay = 2000,fixedDelay = 2000) public void initialDelay() { System.out.println("initialDelay>>>"+new Date()); }
上面这是一个基本用法,除了这几个基本属性以外,@Scheduled 注解也支持 cron 表达式,使用 cron 表达式,能够很是丰富的描述定时任务的时间。cron 表达式格式以下:框架
[秒] [分] [小时] [日] [月] [周] [年]ide
具体取值以下:spring-boot
序号 | 说明 | 是否必填 | 容许填写的值 | 容许的通配符 |
---|---|---|---|---|
1 | 秒 | 是 | 0-59 | - * / |
2 | 分 | 是 | 0-59 | - * / |
3 | 时 | 是 | 0-23 | - * / |
4 | 日 | 是 | 1-31 | - * ? / L W |
5 | 月 | 是 | 1-12 or JAN-DEC | - * / |
6 | 周 | 是 | 1-7 or SUN-SAT | - * ? / L # |
7 | 年 | 否 | 1970-2099 | - * / |
这一块须要你们注意的是,月份中的日期和星期可能会起冲突,所以在配置时这两个得有一个是 ?
微服务
通配符含义:this
?
表示不指定值,即不关心某个字段的取值时使用。须要注意的是,月份中的日期和星期可能会起冲突,所以在配置时这两个得有一个是 ?
*
表示全部值,例如:在秒的字段上设置 *
,表示每一秒都会触发,
用来分开多个值,例如在周字段上设置 "MON,WED,FRI" 表示周一,周三和周五触发-
表示区间,例如在秒上设置 "10-12",表示 10,11,12秒都会触发/
用于递增触发,如在秒上面设置"5/15" 表示从5秒开始,每增15秒触发(5,20,35,50)#
序号(表示每个月的第几个周几),例如在周字段上设置"6#3"表示在每个月的第三个周六,(用 在母亲节和父亲节再合适不过了)L
表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,若是是二月还会自动判断是不是润年), 在周字段上表示星期六,至关于"7"或"SAT"(注意周日算是第一天)。若是在"L"前加上数字,则表示该数据的最后一个。例如在周字段上设置"6L"这样的格式,则表示"本月最后一个星期五"W
表示离指定日期的最近工做日(周一至周五),例如在日字段上设置"15W",表示离每个月15号最近的那个工做日触发。若是15号正好是周六,则找最近的周五(14号)触发, 若是15号是周未,则找最近的下周一(16号)触发,若是15号正好在工做日(周一至周五),则就在该天触发。若是指定格式为 "1W",它则表示每个月1号日后最近的工做日触发。若是1号正是周六,则将在3号下周一触发。(注,"W"前只能设置具体的数字,不容许区间"-")L
和 W
能够一组合使用。若是在日字段上设置"LW",则表示在本月的最后一个工做日触发(通常指发工资 )例如,在 @Scheduled 注解中来一个简单的 cron 表达式,每隔5秒触发一次,以下:code
@Scheduled(cron = "0/5 * * * * *") public void cron() { System.out.println(new Date()); }
上面介绍的是使用 @Scheduled 注解的方式来实现定时任务,接下来咱们再来看看如何使用 Quartz 实现定时任务。视频
通常在项目中,除非定时任务涉及到的业务实在是太简单,使用 @Scheduled 注解来解决定时任务,不然大部分状况可能都是使用 Quartz 来作定时任务。在 Spring Boot 中使用 Quartz ,只须要在建立项目时,添加 Quartz 依赖便可:
项目建立完成后,也须要添加开启定时任务的注解:
@SpringBootApplication @EnableScheduling public class QuartzApplication { public static void main(String[] args) { SpringApplication.run(QuartzApplication.class, args); } }
Quartz 在使用过程当中,有两个关键概念,一个是JobDetail(要作的事情),另外一个是触发器(何时作),要定义 JobDetail,须要先定义 Job,Job 的定义有两种方式:
第一种方式,直接定义一个Bean:
@Component public class MyJob1 { public void sayHello() { System.out.println("MyJob1>>>"+new Date()); } }
关于这种定义方式说两点:
第二种定义方式,则是继承 QuartzJobBean 并实现默认的方法:
public class MyJob2 extends QuartzJobBean { HelloService helloService; public HelloService getHelloService() { return helloService; } public void setHelloService(HelloService helloService) { this.helloService = helloService; } @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { helloService.sayHello(); } } public class HelloService { public void sayHello() { System.out.println("hello service >>>"+new Date()); } }
和第1种方式相比,这种方式支持传参,任务启动时,executeInternal 方法将会被执行。
Job 有了以后,接下来建立类,配置 JobDetail 和 Trigger 触发器,以下:
@Configuration public class QuartzConfig { @Bean MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() { MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean(); bean.setTargetBeanName("myJob1"); bean.setTargetMethod("sayHello"); return bean; } @Bean JobDetailFactoryBean jobDetailFactoryBean() { JobDetailFactoryBean bean = new JobDetailFactoryBean(); bean.setJobClass(MyJob2.class); JobDataMap map = new JobDataMap(); map.put("helloService", helloService()); bean.setJobDataMap(map); return bean; } @Bean SimpleTriggerFactoryBean simpleTriggerFactoryBean() { SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean(); bean.setStartTime(new Date()); bean.setRepeatCount(5); bean.setJobDetail(methodInvokingJobDetailFactoryBean().getObject()); bean.setRepeatInterval(3000); return bean; } @Bean CronTriggerFactoryBean cronTrigger() { CronTriggerFactoryBean bean = new CronTriggerFactoryBean(); bean.setCronExpression("0/10 * * * * ?"); bean.setJobDetail(jobDetailFactoryBean().getObject()); return bean; } @Bean SchedulerFactoryBean schedulerFactoryBean() { SchedulerFactoryBean bean = new SchedulerFactoryBean(); bean.setTriggers(cronTrigger().getObject(), simpleTriggerFactoryBean().getObject()); return bean; } @Bean HelloService helloService() { return new HelloService(); } }
关于这个配置说以下几点:
所有定义完成后,启动 Spring Boot 项目就能够看到定时任务的执行了。
这里主要向你们展现了 Spring Boot 中整合两种定时任务的方法,整合成功以后,剩下的用法基本上就和在 SSM 中使用一致了,再也不赘述。
关注公众号【江南一点雨】,专一于 Spring Boot+微服务,按期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货!