├── .gitignore ├── README.md ├── pom.xml ├── springboot-schedule.iml └── src └── main ├── java └── com │ └── mmzs │ └── springboot │ └── s00 │ └── application │ ├── StaticScheduleTaskApplication.java │ ├── controller │ ├── DynamicScheduleTask.java │ ├── MultithreadScheduleTask.java │ └── SaticScheduleTask.java │ └── dao │ └── ICronMapper.java └── resources ├── application.yml └── mybatis ├── mapper └── CronMapper.xml └── mybatis.cfg.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | target/ 24 | !.mvn/wrapper/maven-wrapper.jar 25 | 26 | ### STS ### 27 | .apt_generated 28 | .classpath 29 | .factorypath 30 | .project 31 | .settings 32 | .springBeans 33 | 34 | ### IntelliJ IDEA ### 35 | .idea 36 | *.iws 37 | *.iml 38 | *.ipr 39 | *.log 40 | *.log* 41 | 42 | ### NetBeans ### 43 | nbproject/private/ 44 | build/ 45 | nbbuild/ 46 | dist/ 47 | nbdist/ 48 | .nb-gradle/ 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # springboot-schedule 2 | springboot定时任务demo 3 | ## 使用SpringBoot创建定时任务非常简单, 4 | 目前主要有以下三种创建方式: 5 | - 一、基于注解(@Scheduled) 6 | - 二、基于接口(SchedulingConfigurer) 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库中读取指定时间来动态执行定时任务,这时候基于接口的定时任务就派上用场了。 7 | - 三、基于注解设定多线程定时任务 8 | ### 详细内容,参见此处 9 | [https://www.mmzsblog.cn/](https://www.mmzsblog.cn/articles/2019/08/08/1565247960802.html) 10 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.mmzs.springboot 8 | springboot-schedule 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.apache.maven.plugins 14 | maven-compiler-plugin 15 | 16 | 8 17 | 8 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter 26 | 2.0.4.RELEASE 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | mysql 36 | mysql-connector-java 37 | 38 | 39 | org.mybatis.spring.boot 40 | mybatis-spring-boot-starter 41 | 1.3.1 42 | 43 | 44 | org.mybatis 45 | mybatis 46 | 3.4.5 47 | compile 48 | 49 | 50 | org.mybatis 51 | mybatis-spring 52 | 1.3.1 53 | compile 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /springboot-schedule.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /src/main/java/com/mmzs/springboot/s00/application/StaticScheduleTaskApplication.java: -------------------------------------------------------------------------------- 1 | package com.mmzs.springboot.s00.application; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.ComponentScan; 7 | import org.springframework.scheduling.annotation.EnableScheduling; 8 | 9 | @SpringBootApplication 10 | //@MapperScan( "com.mmzs.springboot.s00.application") 11 | public class StaticScheduleTaskApplication { 12 | public static void main(String[] args) { 13 | SpringApplication.run(StaticScheduleTaskApplication.class, args); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/com/mmzs/springboot/s00/application/controller/DynamicScheduleTask.java: -------------------------------------------------------------------------------- 1 | package com.mmzs.springboot.s00.application.controller; 2 | 3 | import com.mmzs.springboot.s00.application.dao.ICronMapper; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Select; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.scheduling.annotation.EnableScheduling; 9 | import org.springframework.scheduling.annotation.SchedulingConfigurer; 10 | import org.springframework.scheduling.config.ScheduledTaskRegistrar; 11 | import org.springframework.scheduling.support.CronTrigger; 12 | import org.springframework.stereotype.Component; 13 | import org.springframework.util.StringUtils; 14 | 15 | import java.time.LocalDateTime; 16 | 17 | @Component 18 | @Configuration //1.主要用于标记配置类,兼备Component的效果。 19 | @EnableScheduling // 2.开启定时任务 20 | public class DynamicScheduleTask implements SchedulingConfigurer { 21 | 22 | @Mapper 23 | public interface CronMapper { 24 | @Select("select cron from cron limit 1") 25 | public String getCron(); 26 | } 27 | 28 | @Autowired //注入mapper 29 | @SuppressWarnings("all") 30 | CronMapper cronMapper; 31 | 32 | /** 33 | * 执行定时任务. 34 | */ 35 | @Override 36 | public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 37 | 38 | taskRegistrar.addTriggerTask( 39 | //1.添加任务内容(Runnable) 40 | () -> System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime()), 41 | //2.设置执行周期(Trigger) 42 | triggerContext -> { 43 | //2.1 从数据库获取执行周期 44 | String cron = cronMapper.getCron(); 45 | //2.2 合法性校验. 46 | if (StringUtils.isEmpty(cron)) { 47 | // Omitted Code .. 48 | } 49 | //2.3 返回执行周期(Date) 50 | return new CronTrigger(cron).nextExecutionTime(triggerContext); 51 | } 52 | ); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /src/main/java/com/mmzs/springboot/s00/application/controller/MultithreadScheduleTask.java: -------------------------------------------------------------------------------- 1 | package com.mmzs.springboot.s00.application.controller; 2 | 3 | import org.springframework.scheduling.annotation.Async; 4 | import org.springframework.scheduling.annotation.EnableAsync; 5 | import org.springframework.scheduling.annotation.EnableScheduling; 6 | import org.springframework.scheduling.annotation.Scheduled; 7 | import org.springframework.stereotype.Component; 8 | import java.time.LocalDateTime; 9 | 10 | 11 | //@Component注解用于对那些比较中立的类进行注释; 12 | //相对与在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释 13 | @Component 14 | @EnableScheduling // 1.开启定时任务 15 | @EnableAsync // 2.开启多线程 16 | public class MultithreadScheduleTask { 17 | 18 | @Async 19 | @Scheduled(fixedDelay = 1000) //间隔1秒 20 | public void first() throws InterruptedException { 21 | System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName()); 22 | System.out.println(); 23 | Thread.sleep(1000 * 10); 24 | } 25 | 26 | @Async 27 | @Scheduled(fixedDelay = 2000) 28 | public void second() { 29 | System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName()); 30 | System.out.println(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/mmzs/springboot/s00/application/controller/SaticScheduleTask.java: -------------------------------------------------------------------------------- 1 | package com.mmzs.springboot.s00.application.controller; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.scheduling.annotation.EnableScheduling; 5 | import org.springframework.scheduling.annotation.Scheduled; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.time.LocalDateTime; 9 | 10 | @Component 11 | @Configuration //1.主要用于标记配置类,兼备Component的效果。 12 | @EnableScheduling // 2.开启定时任务 13 | public class SaticScheduleTask { 14 | //3.添加定时任务 15 | @Scheduled(cron = "0/5 * * * * ?") 16 | private void configureTasks() { 17 | System.err.println("执行静态定时任务时间: " + LocalDateTime.now()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/mmzs/springboot/s00/application/dao/ICronMapper.java: -------------------------------------------------------------------------------- 1 | package com.mmzs.springboot.s00.application.dao; 2 | 3 | import org.apache.ibatis.annotations.Mapper; 4 | 5 | 6 | @Mapper 7 | public interface ICronMapper { 8 | String getCron(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | #application.yml 配置如下: 2 | spring: 3 | datasource: 4 | url: jdbc:mysql://localhost:3306/socks 5 | username: root 6 | password: 123456 7 | 8 | #mybatis: 9 | # config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 10 | # type-aliases-package: com.mmzs.springboot.common.entities # 所有Entity别名类所在包 11 | # mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件 12 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mapper/CronMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mybatis.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | --------------------------------------------------------------------------------