【第一部分】历史文章:
SpringBoot总结(一)——第一个SpringBoot项目
SpringBoot总结(二)——Spring Boot的自动配置
SpringBoot总结(三)——SpringBoot的配置文件
SpringBoot总结(四)——@Value和@ConfigurationProperties的区别
SpringBoot总结(五)——@PropertySource注解与@ImportResource注解
SpringBoot总结(六)——SpringBoot配置文件占位符
SpringBoot总结(七)——Profile的使用
SpringBoot总结(八)——配置文件的加载位置
SpringBoot总结(九)——@Conditional注解与自动配置报告
SpringBoot总结(十)——SpringBoot+Mybatis实现数据库的CRUD(从创建到实现【超详细附代码】)
SpringBoot总结(十一)——SpringBoot的静态资源映射规则
SpringBoot总结(十二)——登录界面的实现
SpringBoot总结(十三)——修改嵌入式Servlet容器配置
SpringBoot总结(十四)——SpringBoot整合JDBCTemplate及Druid连接池
SpringBoot总结(十五)——接口架构风格(RESTful)
SpringBoot总结(十六)——Spring Boot的邮件发送
# Spring Boot的定时任务与异步任务 下面以简单的例子介绍Spring Boot的定时任务与异步任务:
# 前言 定时任务是企业级开发中的最常见的功能之一,例如:统计订单数、数据库的备份、定时发送短信和邮件、定时统计博客访客等;简单的定时任务可以通过Spring中的 `@Scheduled`注解来实现;而复杂的定时任务可以通过Quartz来实现。
# 一、定时任务 `@Scheduled`是由Spring提供的定时任务注解,使用较为方便、配置简单,可以很好的解决大部分的定时任务需求。 下面进行简单介绍定时任务的使用: ## 1.创建项目 创建一个Spring Boot的Web工程,具体创建过程这里不再介绍。 ## 2.开启定时任务 在项目的启动类上加上`@EnableScheduling` 注解来开启定时任务。 ```java package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; /** * @author */ @EnableScheduling @SpringBootApplication public class SpringbootTaskApplication { public static void main(String[] args) { SpringApplication.run(SpringbootTaskApplication.class, args); } } ``` ## 3.配置定时任务 注:定时任务主要通过 `@Scheduled`注解来进行配置,示例代码如下所示: ```java package com.example.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.util.Date; /** * @author */ @Service public class ScheduledService { @Scheduled(fixedDelay = 1000) public void scheduledTask1(){ System.out.println("fixedDelay:"+new Date()); } @Scheduled(fixedRate = 2000) public void scheduledTask2(){ System.out.println("fixedRate:"+new Date()); } @Scheduled(initialDelay = 1000,fixedRate = 2000) public void scheduledTask3(){ System.out.println("initialDelay:"+new Date()); } @Scheduled(cron = "0 * * * * ?") public void scheduledTask4(){ System.out.println("cron:"+new Date()); } } ``` 注: - `@Scheduled`注解来标注一个定时任务。 - fixedDelay=1000表示:`在当前任务执行结束1秒后开启另外一个任务`。 - fixedRate=2000表示:`当前任务开始执行2秒后开启另外一个定时任务`。 - initialDelay=1000表示:`首次执行的延迟时间`。 - 在@Scheduled注解中也可以使用cron表达式。 - 表达式:cron = "0 * * * * ?"表示`该定时任务每分钟执行一次`。 启动项目,控制台打印如下: ![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/2020102821392019.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzc1OTM1Mg==,size_16,color_FFFFFF,t_70#pic_center) # 二、异步任务 两个注解: - `@EnableAsync` 开启异步注解功能 - `@Async` 一般用在类的方法上;若用在类上,表示这个类的所有的方法都是异步执行。 ## 1.开启异步任务 在项目的启动类上加上`@EnableAsync` 注解。 ```java package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; /** * @author */ @EnableAsync @EnableScheduling @SpringBootApplication public class SpringbootTaskApplication { public static void main(String[] args) { SpringApplication.run(SpringbootTaskApplication.class, args); } } ``` ## 2.简单使用示例 ```java package com.example.task; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; /** * @author */ @Component public class AsyncTask { @Async public void Task1() throws Exception{ long start = System.currentTimeMillis(); Thread.sleep(1000); long end = System.currentTimeMillis(); System.out.println("task1耗时:"+(end-start)+"毫秒!"); } public void Task2() throws Exception{ long start = System.currentTimeMillis(); Thread.sleep(2000); long end = System.currentTimeMillis(); System.out.println("task2耗时:"+(end-start)+"毫秒!"); } public void Task3() throws Exception{ long start = System.currentTimeMillis(); Thread.sleep(3000); long end = System.currentTimeMillis(); System.out.println("task3耗时:"+(end-start)+"毫秒!"); } } ``` ## 3.测试 ```java @GetMapping("/asynctasktest") public String AsyncTaskTest() throws Exception { long start = System.currentTimeMillis(); asyncTask.Task1(); asyncTask.Task2(); asyncTask.Task3(); long end = System.currentTimeMillis(); System.out.println("全部完成一共耗时:" + (end - start) + "毫秒"); return "执行完成!!!"; } ```
# 总结 以上分别介绍了Spring Boot的定时任务与异步任务,以及用简单的示例进行了演示。希望通过以上例子能给予大家帮助。😊 关于本项目的示例代码获取方式:关注+私信并回复:【定时任务与异步任务】即可获取哦!!!