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 ...
随机推荐
- 发布.net core项目 System.AggregateException: 发生一个或多个错误
背景:之前创建.net core webapi项目的时候SDK是2.2的版本,后改成2.1,发布的时候报错. 发布的时候报错,展示的信息是: 其实这里也大致能看到部分信息,但由于信息量太小,没办法知道 ...
- JDBC与Druid简单介绍及Druid与MyBatis连接数据库
序言 java程序与数据建立连接,首先要从jdbc说起,然后直接上阿里认为宇宙最好的数据库连接池druid,然后再说上层程序对象与数据源映射关联关系的orm-mybatis. JDBC介绍 JDBC( ...
- Asp.Net SignalR 使用记录
工作上遇到一个推送消息的功能的实现.本着面向百度编程的思想.网上百度了一大堆.主要的实现方式是原生的WebSocket,和SignalR,再次写一个关于Asp.Net SignalR 的demo 这里 ...
- MQ 分布式事务 -- 微服务应用
1.背景 友情链接:https://www.cnblogs.com/Agui520/p/11187972.html https://blog.csdn.net/fd2025/article/detai ...
- wpf 打开win8系统软件盘
三个函数 一) /// <summary> /// 判断进程是否正在运行 /// </summary> /// <param name="process&quo ...
- 【推荐】全球最全面的Telegram组群频道的集合网站 持续收集中
全球最全面的Telegram组群频道的集合网站 https://www.telegramgroup.org Telegram 组群频道分享 可搜索自己想找的组群频道 从小白到大神,一个 telegra ...
- Google Analytics 学习笔记一 —— GA简介
GA的原理 网页页面添加GA跟踪代码,以"一像素"传递信息给服务器 hit(交互) --> sessions(会话) --> user(用户) 竞品对比 Firebas ...
- Web Service 接口测试
Web Service 接口测试 Web service 概念 Web service使用与平台和编程语言无关的方式进行通讯的一项技术, web service 是一个接口, 他描述了一组可以在网络上 ...
- linux date 设置系统时间
设置 系统时间 注意时间格式 date -s "date" [root@localhost c]# date -s "2019-05-29 10:58:00" ...
- Jmeter接口测试,变量是订单和订单明细,怎么一起传?
", "price": 12.0, "orderDate": "2019-07-05 10:40:00", "order ...