Spring Boot 知识笔记(定时任务与异步)
一、定时任务
1、启动类里面增加注入
@SpringBootApplication //@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan
@Configuration
@ServletComponentScan //扫描过滤器等servlet、filter注解
@MapperScan("net.Eleven.demo.Mapper") //扫描对应的Mapper文件 @EnableScheduling //定时任务注解,扫描包里面所有子类中的定时任务
@EnableAsync //开启异步任务
public class XdclassApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(XdclassApplication.class);
}
public static void main(String[] args){ SpringApplication.run(XdclassApplication.class,args);
}
}
2、新建一个定时任务类
package net.Eleven.demo.task; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.util.Date; /**
* 功能描述:定时任务业务类
*
*/ @Component
public class TestTask {
// @Scheduled(fixedRate = 2000) //每两秒执行一次
@Scheduled(cron = "*/3 * * * * *") //每三秒执行一次
public void sendEmail(){
System.out.println("当前时间:"+new Date());
}
}
3、定时任务的几种配置方法
3.1、cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
3.2、fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
3.3、fixedDelay: 上一次执行结束时间点后xx秒再次执行
3.4、fixedDelayString: 字符串形式,可以通过配置文件指定
二、异步任务
1、启动类增加注解(@EnableAsync //开启异步任务)
2、新建一个异步任务类
package net.Eleven.demo.task; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; @Component
@Async //类中所有的方法都是异步
public class AsyncTask {
@Async //被标注的方法是异步
public void task1() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(1000L);
long end = System.currentTimeMillis();
System.out.println("任务1耗时:"+(end-begin));
} public void task2() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(2000L);
long end = System.currentTimeMillis();
System.out.println("任务2耗时:"+(end-begin));
} public void task3() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(3000L);
long end = System.currentTimeMillis();
System.out.println("任务3耗时:"+(end-begin));
}
}
3、在controller里面调用这个任务
@Autowired
private AsyncTask asyncTask;
@GetMapping("/api/async")
public JsonData doTask() throws InterruptedException{
long begin = System.currentTimeMillis();
asyncTask.task1();
asyncTask.task2();
asyncTask.task3();
long end = System.currentTimeMillis();
long totalTime = end-begin;
System.out.println("执行耗时:"+totalTime);
return JsonData.buildSuccess(totalTime);
}
4、执行结果
Spring Boot 知识笔记(定时任务与异步)的更多相关文章
- Spring Boot 知识笔记(整合Mybatis)
一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...
- Spring Boot 知识笔记(配置文件)
Spring boot 提供了两种常用的配置文件,properties和yml文件. 1.yml yml是YAML(YAML Ain't Markup Language)语言的文件,以数据为中心,比j ...
- Spring Boot 知识笔记(热部署)
热部署原理: 使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader ...
- Spring Boot 知识笔记(创建maven项目、HTTP接口)
一.使用Maven手工创建SpringBoot应用(IDEA) 1. 点击File——New——Project——Maven——Next,填写相关信息,创建项目. 2. 在pom.xml中添加相关 ...
- Spring Boot 知识笔记(thymleaf模板引擎)
一.pom.xml中引入 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...
- Spring Boot 知识笔记(整合Redis)
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot 知识笔记(集成zookeeper)
一.本机搭建zookeeper伪集群 1.下载安装包,复制三份 2.每个安装包目录下面新建一个data文件夹,用于存放数据目录 3.安装包的conf目录下,修改zoo.cfg配置文件 # The nu ...
- Spring Boot 知识笔记(整合Mybatis续-补充增删改查)
续上篇,补充数据库增删改查的其他场景. 一.Mapper中添加其他场景操作 package net.Eleven.demo.Mapper; import net.Eleven.demo.domain. ...
- Spring Boot 知识笔记(servlet、监听器、拦截器)
一.通过注解自定义servlet package net.Eleven.demo.servlet; import javax.servlet.ServletException; import java ...
随机推荐
- [LeetCode#180]Consecutive Numbers
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- Knative 初体验:CICD 极速入门
Knative 社区很早就在讨论用 Tekton 替换 Build 模块的相关事宜.Knative Build 官方已经正式说明不再建议使用 Knative Build 了. 如果你知道 Knativ ...
- Window权限维持(十):Netsh Helper DLL
Netsh是Windows实用程序,管理员可以使用它来执行与系统的网络配置有关的任务,并在基于主机的Windows防火墙上进行修改.可以通过使用DLL文件来扩展Netsh功能.此功能使红队可以使用此工 ...
- sso单点登录的入门(Session跨域、Spring-Session共享)
1.单点登录,就是多系统,单一位置登录,实现多系统同时登录的一种技术.单点登录一般是用于互相授信的系统,实现单一位置登录,全系统有效的. 区分与三方登录(第三方登录) ,三方登录:某系统,使用其他系统 ...
- WebApi使用Unity实现IOC
最近在学习ASP.NET MVC,使用Unity作为依赖注入容器.分别在WebAPI和MVC中使用.这篇文章介绍WebAPI,MVC的在下篇文章中介绍.下面是学习的一点经验. 一 IOC简单介绍 Io ...
- VS Code 自动修改和保存 代码风格 == eslint+prettier
最近因为用到VS Code,需要统一所有人的代码风格(前端语言js/html/css等,或者后端语言 go/python等也可以这么用). 所以参考了一些网络资料,记录下设置步骤,以便后续查阅. St ...
- C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成
前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码生成. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https:/ ...
- C# .NET 使用 NPOI 读取 .xlsx 格式 Excel
string filePath = @"C:\Users\yangqinglin\Desktop\test.xlsx"; IWorkbook wk = null; string e ...
- C# 分布式自增ID算法snowflake(雪花算法)
概述 分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的.有些时候我们希望能使用一种简 ...
- Java获取客户端真实IP地址
Java代码 import javax.servlet.http.HttpServletRequest; /** * 获取对象的IP地址等信息 */ public class IPUtil { /** ...