一、定时任务

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. 明解C语言 中级篇 第四章答案

    练习4-1 /* 珠玑妙算 */ #include <time.h> #include <ctype.h> #include <stdio.h> #include ...

  2. sudo: ulimit: command not found

    在这看到的:http://stackoverflow.com/questions/17483723/command-not-found-when-using-sudo-ulimit 修改系统文件打开数 ...

  3. 机器学习(十一)-------- 异常检测(Anomaly Detection)

    异常检测(Anomaly Detection) 给定数据集

  4. kmv 学习笔记 工具

    qemu:kmv的文本管理工具,包括qemu-kvm.qemu-img libvirt:是一套免费.开源的支持Linux下主流虚拟化工具的C函数库,libvirtd是运行的守护进程的名称.包括GUI: ...

  5. java架构之路-(JVM优化与原理)JVM的运行时内存模型

    还是我们上次的图,我们上次大概讲解了类加载子系统的执行过程,验证,准备,解析,初始化四个过程.还有我们的双亲委派机制. 我们这次来说一下运行时内存模型.上一段小代码. public class Mai ...

  6. CentOS 配置阿里云 yum 源

    CentOS 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的Cent ...

  7. Spring-AOP源码分析随手记(一)

    1.@EnableAspectJAutoProxy(proxyTargetClass = true) 就是弄了个"org.springframework.aop.config.interna ...

  8. ASP.NET MVC EF 连接数据库(三)-----Code First

    Code first (VS2015 ,Sql Server2014) 新建MVC项目 实例: 在数据库中会有个新建的数据库和表 源码地址:https://note.youdao.com/ynotes ...

  9. putty连接centos慢

    用的vmware下的centos minimal镜像,开发时,用putty连接很慢,一分多钟, 解决方案: 禁用GSSAPI认证有两个方式:客户端和服务端 直接配置你ssh客户端的文件/etc/ssh ...

  10. SSM登陆

    简单的SSM登陆 jsp <form action="${pageContext.request.contextPath }/user/login.action"> & ...