一、定时任务

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 知识笔记(定时任务与异步)的更多相关文章

  1. Spring Boot 知识笔记(整合Mybatis)

    一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...

  2. Spring Boot 知识笔记(配置文件)

    Spring boot 提供了两种常用的配置文件,properties和yml文件. 1.yml yml是YAML(YAML Ain't Markup Language)语言的文件,以数据为中心,比j ...

  3. Spring Boot 知识笔记(热部署)

    热部署原理: 使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader ...

  4. Spring Boot 知识笔记(创建maven项目、HTTP接口)

    一.使用Maven手工创建SpringBoot应用(IDEA) 1.  点击File——New——Project——Maven——Next,填写相关信息,创建项目. 2.  在pom.xml中添加相关 ...

  5. Spring Boot 知识笔记(thymleaf模板引擎)

    一.pom.xml中引入 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  6. Spring Boot 知识笔记(整合Redis)

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  7. Spring Boot 知识笔记(集成zookeeper)

    一.本机搭建zookeeper伪集群 1.下载安装包,复制三份 2.每个安装包目录下面新建一个data文件夹,用于存放数据目录 3.安装包的conf目录下,修改zoo.cfg配置文件 # The nu ...

  8. Spring Boot 知识笔记(整合Mybatis续-补充增删改查)

    续上篇,补充数据库增删改查的其他场景. 一.Mapper中添加其他场景操作 package net.Eleven.demo.Mapper; import net.Eleven.demo.domain. ...

  9. Spring Boot 知识笔记(servlet、监听器、拦截器)

    一.通过注解自定义servlet package net.Eleven.demo.servlet; import javax.servlet.ServletException; import java ...

随机推荐

  1. background-size:100% 100% 和 background-size:cover的区别简述

    下面我通过给下图背景图添加background-size属性的不同属性值,更直观的显示出100%和cover的区别   下图是添加background-size:100% 100% 后的背景图效果,背 ...

  2. redis集群之Sentinel

    目前我们讲的 Redis 还只是主从方案,最终一致性.读者们可思考过,如果主节点凌晨3 点突发宕机怎么办?就坐等运维从床上爬起来,然后手工进行从主切换,再通知所有的程序把地址统统改一遍重新上线么?毫无 ...

  3. redis之布隆过滤器

    布隆过滤器是什么? 布隆过滤器可以理解为一个不怎么精确的 set 结构,当你使用它的 contains 方法判断某个对象是否存在时,它可能会误判.但是布隆过滤器也不是特别不精确,只要参数设置的合理,它 ...

  4. dubbo入门教程-从零搭建dubbo服务

    [原创 转载请注明出处] 本文是学习了dubbo之后自己手动写的,比较通俗,很多都是自己学习之后的理解,写的过程中没有参考任何文章. 另外dubbo也有官方文档,但是比较官方,也可以多看看dubbo的 ...

  5. 在 .NET Core 中使用异步的 ADO.NET 的简单示例

    直接贴代码: Program.cs using Microsoft.Extensions.Configuration; using System; using System.Data; using S ...

  6. WPF toolkit AutoCompleteBox

    checked http://www.broculos.net/2014/04/wpf-autocompletebox-autocomplete-text.html#.WGNnq4N95aQ. 1.S ...

  7. jakarta-oro-2.0.8.jar-----------JAVA FTP相关

    资源不好找,找到了就和大家分享一下! 链接:https://share.weiyun.com/51kBB0y 密码:2hcxcu

  8. Visual Studio 2017使用ODT 连接Oracle 数据库出现异常

    2019.5.23 更新 突然发现原来是是sqlnet.ora在搞鬼,只要将SQLNET.AUTHENTICATION_SERVICES=(nts)  改为 SQLNET.AUTHENTICATION ...

  9. laravel npm run dev 错误 npm run dev error [npm ERR! code ELIFECYCLE]

    出现此问题是node_modules出现错误,需要执行: 1 rm -rf node_modules 2 rm package-lock.json 3 npm cache clear --force ...

  10. LinuxShell——变量

    LinuxShell——变量 摘要:本文主要学习了Shell命令中的变量. 什么是变量 简单的说,变量就是让某一个特定字串代表不固定的内容. 变量是计算机内存的单元,其中存放的值可以改变.当Shell ...