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 ...
随机推荐
- Dubbo从入门到实战:入门篇
很多时候,其实我们使用这个技术的时候,可能都是因为项目需要,所以,我们就用了,但是,至于为什么我们需要用到这个技术,可能自身并不是很了解的,但是,其实了解技术的来由及背景知识,对于理解一项技术还是有帮 ...
- [STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary
1.set 集合 哦....对了,set有自动按照字典序排序功能..... 声明和插入操作 #include <cstdio> #include <vector> #inclu ...
- 苹果审核之遇到IPV6问题被拒的解决方法
情景: 等待苹果审核上线时,发现因为IPV6被拒了.这是悲剧,以下是苹果审核给我的理由: We discovered one or more bugs on Wi-Fi connected to an ...
- 【Spring Boot】Spring Boot之使用ImportSelector类实现动态注册Bean
一.ImportSelector类介绍 可以通过指定的选择条件来决定哪些类被注册到Spring中.与ImportBeanDefinitionRegistrar类功能相似,通过@Import的方 ...
- Maven+SSM框架,实现单表简单的增删改查
目录 1.创建web Maven项目 2.创建java源码文件和resources资源文件 3.创建数据库配置文件:jdbc.properties 4.项目总体目录: 5.添加spring配置文件:a ...
- Eric6 黑色风格配置
界面风格-黑色主题 1.设置-首选项-界面-风格选择Fusion,再配置题样式表选择路径下的eric6\Styles选择[Chinese_Dark.qss]进行修改. 编辑器风格 2.选择完毕后, ...
- 其它综合-CentOS7 解决忘记root密码
CentOS7 解决忘记root密码 1.重启 长时间不用的 CentOS 机器再次开机的时候忽然忘记了密码,总不能就重装一台吧,还有好多服务在机器上,于是决定重置 root 的密码. 如果是已经开启 ...
- 当请求进入Nginx后,每个HTTP执行阶段的作用
阶段顺序 阶段名称 作用 1 NGX_HTTP_POSTREAD_PHASE = 0 接收并读取请求阶段 2 NGX_HTTP_SERVER_REWRITE_PHASE 修改url阶段,通常有重定向和 ...
- jenkins如何构建C#代码写的网站
纯粹是因为同事习惯了写C#代码,开发的网站用C#编译, 对于习惯了用Maven编译的测试人员,真是一头雾水.不用jenkins吧,效率特别低,每次收到开发发过来的版本,还要进行数据库相关配置,是非常累 ...
- 04发送请求,将值赋给data--动态传递参数
03==>发送青丘,将值赋给data. 注意:赋值使用的是 _this.setData({ }) 是以冒号的形式赋值, 提前保存好this data: { arrlistdata:[], }, ...