假如你想相识更多关于springboot的学问,能够点击:JAVA教程
运用SpringBoot建立定时使命非常简朴,现在重要有以下三种建立体式格局:
一、基于注解(@Scheduled)
二、基于接口(SchedulingConfigurer) 前者置信人人都很熟习,然则实际运用中我们每每想从数据库中读取指定时刻来动态实行定时使命,这时刻基于接口的定时使命就派上用场了。
三、基于注解设定多线程定时使命
一、静态:基于注解
基于注解@Scheduled
默以为单线程,开启多个使命时,使命的实行机遇会受上一个使命实行时刻的影响。
1、建立定时器
运用SpringBoot基于注解来建立定时使命非常简朴,只需几行代码便可完成。 代码以下:
@Component @Configuration //1.重要用于标记设置类,兼备Component的结果。 @EnableScheduling // 2.开启定时使命 public class SaticScheduleTask { //3.增加定时使命 @Scheduled(cron = "0/5 * * * * ?") //或直接指定时刻距离,比方:5秒 //@Scheduled(fixedRate=5000) private void configureTasks() { System.err.println("实行静态定时使命时刻: " + LocalDateTime.now()); } }
Cron表达式参数离别示意:
秒(0~59) 比方0/5示意每5秒
分(0~59)
时(0~23)
日(0~31)的某天,需盘算
月(0~11)
周几( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)
@Scheduled:除了支撑天真的参数表达式cron以外,还支撑简朴的延时操纵,比方 fixedDelay ,fixedRate 填写响应的毫秒数即可。
2、启动测试
启动运用,能够看到掌握台打印出以下信息:
明显,运用@Scheduled 注解很轻易,但瑕玷是当我们调整了实行周期的时刻,须要重启运用才见效,这若干有些不轻易。为了到达及时见效的结果,能够运用接口来完成定时使命。
二、动态:基于接口
基于接口(SchedulingConfigurer
)
1、导入依靠包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencies> <dependency><!--增加Web依靠 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><!--增加MySql依靠 --> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency><!--增加Mybatis依靠 设置mybatis的一些初始化的东西--> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency><!-- 增加mybatis依靠 --> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> <scope>compile</scope> </dependency> </dependencies>
2、增加数据库纪录
开启当地数据库mysql,随意翻开查询窗口,然后实行剧本内容,以下:
DROP DATABASE IF EXISTS `socks`; CREATE DATABASE `socks`; USE `SOCKS`; DROP TABLE IF EXISTS `cron`; CREATE TABLE `cron` ( `cron_id` varchar(30) NOT NULL PRIMARY KEY, `cron` varchar(30) NOT NULL ); INSERT INTO `cron` VALUES ('1', '0/5 * * * * ?');
然后在项目中的application.yml 增加数据源:
spring: datasource: url: jdbc:mysql://localhost:3306/socks username: root password: 123456
3、建立定时器
数据库准备好数据以后,我们编写定时使命,注重这里增加的是TriggerTask,目标是轮回读取我们在数据库设置好的实行周期,以及实行相干定时使命的内容。
细致代码以下:
@Component @Configuration //1.重要用于标记设置类,兼备Component的结果。 @EnableScheduling // 2.开启定时使命 public class DynamicScheduleTask implements SchedulingConfigurer { @Mapper public interface CronMapper { @Select("select cron from cron limit 1") public String getCron(); } @Autowired //注入mapper @SuppressWarnings("all") CronMapper cronMapper; /** * 实行定时使命. */ @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask( //1.增加使命内容(Runnable) () -> System.out.println("实行动态定时使命: " + LocalDateTime.now().toLocalTime()), //2.设置实行周期(Trigger) triggerContext -> { //2.1 从数据库猎取实行周期 String cron = cronMapper.getCron(); //2.2 合法性校验. if (StringUtils.isEmpty(cron)) { // Omitted Code .. } //2.3 返回实行周期(Date) return new CronTrigger(cron).nextExecutionTime(triggerContext); } ); } }
4、启动测试
启动运用后,检察掌握台,打印时刻是我们预期的每10秒一次:
然后翻开Navicat ,将实行周期修正成每6秒实行一次,如图:
检察掌握台,发明实行周期已转变,而且不须要我们重启运用,非常轻易。如图:
注重: 假如在数据库修正时花样涌现毛病,则定时使命会住手,纵然从新修正准确;此时只能从新启动项目才恢复。
三、多线程定时使命
基于注解设定多线程定时使命
1、建立多线程定时使命
//@Component注解用于对那些比较中立的类举行解释; //相对与在耐久层、营业层和掌握层离别采纳 @Repository、@Service 和 @Controller 对分层中的类举行解释 @Component @EnableScheduling // 1.开启定时使命 @EnableAsync // 2.开启多线程 public class MultithreadScheduleTask { @Async @Scheduled(fixedDelay = 1000) //距离1秒 public void first() throws InterruptedException { System.out.println("第一个定时使命最先 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName()); System.out.println(); Thread.sleep(1000 * 10); } @Async @Scheduled(fixedDelay = 2000) public void second() { System.out.println("第二个定时使命最先 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName()); System.out.println(); } }
2、启动测试
启动运用后,检察掌握台:
从掌握台能够看出,第一个定时使命和第二个定时使命互不影响;
而且,因为开启了多线程,第一个使命的实行时刻也不受其自身实行时刻的限定,所以须要注重可能会涌现反复操纵致使数据非常。
以上就是springboot定时使命怎样建立的细致内容,更多请关注ki4网别的相干文章!