Springboot-async(异步)初识
通过@Async注解实现一个简单的异步任务处理
首先,假设一个全自动化的工厂车间每天需要开启四台互不影响的机器开关来完成生产量,于是车间主任A委派“同步甲”和“异步乙”轮
流完成每天打开机器开关的任务;
“同步甲”是个做事谨慎,认真细心的人,每次进了工厂都是确保一台机器从开启到生产结束无异常后,才开启下一台机器,最后离开车间。
“异步乙”是个做事粗心,好吃懒做的人,每次进了工厂都是一次性打开四台机器,便扬长离开车间。
具体的代码如下:
①Springboot启动类,加入@EnableAsync注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication
@EnableAsync
@ComponentScan(basePackages = "com.example")
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
②四台机器Service类,执行异步时,添加@Async注解
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import java.util.Random; /**
* Created by Administrator on 2019/4/18.
*/
@Service
@Slf4j
public class TaskService {
public static Random random=new Random(); @Async
public void doTaskOne(String i) throws Exception {
log.info("机器"+i+"开始生产...");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("机器"+i+"停止生产,耗时:" + (end - start) + "毫秒");
} @Async
public void doTaskTwo(String i) throws Exception {
log.info("机器"+i+"开始生产...");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("机器"+i+"停止生产,耗时:" + (end - start) + "毫秒");
} @Async
public void doTaskThree(String i) throws Exception {
log.info("机器"+i+"开始生产...");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
log.info("机器"+i+"停止生产,耗时:" + (end - start) + "毫秒");
} @Async
public void doTaskFour(String j){
log.info("机器"+j+"开始生产...");
long start = System.currentTimeMillis();
long count = 0;
for (int i = 0 ; i < 10000000; i ++){
count = count + i ;
}
long end = System.currentTimeMillis();
log.info("机器"+j+"停止生产,耗时:" + (end - start) + "毫秒");
}
}
③Controller
import com.example.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.concurrent.Future; /**
* Created by Administrator on 2019/4/18.
*/
@Controller
@Slf4j
public class TaskController { private TaskService taskService; @Autowired
public TaskController(TaskService taskService) {
this.taskService = taskService;
} @ResponseBody
@RequestMapping("/test")
public String testAsync() throws Exception {
log.info("工作人员走进厂房,准备开始一天的工作。。" );
long start = System.currentTimeMillis(); taskService.doTaskOne("A");
taskService.doTaskTwo("B");
taskService.doTaskThree("C");
taskService.doTaskFour("D"); long end = System.currentTimeMillis(); log.info("工作人员离开厂房,耗时" + (end - start) + "毫秒" );
return "执行成功!!!";
}
}
④启动springboot,访问http://localhost:8080/test,查看同步与异步的区别。




代码地址:https://github.com/liuchunbo24/springboot-async
初步的简单认识,有待继续研究。。。
Springboot-async(异步)初识的更多相关文章
- SpringBoot @Async 异步处理业务逻辑和发短信逻辑
有个业务场景,业务数据审核通过后需要给用户发短信,发短信过程比较耗时,可能需要几秒甚至十几秒,因此使用异步发短信 使用了注解@Async来实现: 1.SpringApplication启用注解@Ena ...
- springboot+async异步接口实现和调用
什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行. 如何实现异步调用? 多线程, ...
- springboot+@async异步线程池的配置及应用
示例: 1. 配置 @EnableAsync @Configuration public class TaskExecutorConfiguration { @Autowired private Ta ...
- @Async异步注解与SpringBoot结合使用
当你在service层需要启动异步线程去执行某些分支任务,又不希望显式使用Thread等线程相关类,只想专注于实现业务逻辑代码开发,可以使用@Async异步注解. 1. 使用@Async 异步注解 C ...
- SpringBoot中异步请求和异步调用(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...
- spring boot使用自定义配置的线程池执行Async异步任务
一.增加配置属性类 package com.chhliu.springboot.async.configuration; import org.springframework.boot.context ...
- SpringBoot:异步开发之异步调用
前言 除了异步请求,一般上我们用的比较多的应该是异步调用.通常在开发过程中,会遇到一个方法是和实际业务无关的,没有紧密性的.比如记录日志信息等业务.这个时候正常就是启一个新线程去做一些业务处理,让主线 ...
- Spring Boot -- Spring Boot之@Async异步调用、Mybatis、事务管理等
这一节将在上一节的基础上,继续深入学习Spring Boot相关知识,其中主要包括@Async异步调用,@Value自定义参数.Mybatis.事务管理等. 本节所使用的代码是在上一节项目代码中,继续 ...
- Springboot:异步业务处理(十二)
说明 当正常业务处理调用一个复杂业务或者耗时较长的请求时,客户等待时间会比较长,造成不好的用户体验,所以这时候需要用的异步处理 构建一个群发邮件的service接口及实现(模拟) 接口:com\spr ...
- SpringBoot使用异步线程池实现生产环境批量数据推送
前言 SpringBoot使用异步线程池: 1.编写线程池配置类,自定义一个线程池: 2.定义一个异步服务: 3.使用@Async注解指向定义的线程池: 这里以我工作中使用过的一个案例来做描述,我所在 ...
随机推荐
- 浅谈Flutter(一):搭建Flutter开发环境
学习内容来自: Flutter中文网 . Flutter实战 -------------------------------------------------------------------- ...
- Android项目实战欢迎界面
欢迎界面 首先同理把欢迎界面的图片导入到drawable目录下,在导入时 Android Studio 会提示如下 drawable 具体本人尚未弄明白,待理解后会重新补全本部分内容,在此本人选了第一 ...
- typescript中的泛型
泛型:软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性. 组件不仅能够支持当前的数据类型,同时也能支持未来的数据类型,这在创建大型系统时为你提供了十分灵活的功能. 在像C#和Ja ...
- Linux(Manjaro) - Docker - MySQL 安装配置
Linux(Manjaro) - Docker - MySQL 安装配置 拉取mysql镜像 # 使用网易的 MySQL 镜像地址 docker pull hub.c.163.com/library/ ...
- ANT property三种使用方式
方式一:引入*.properties文件 1.在cms.properties文件中定义属性 userName=admin 2.在build.xml中引入属性 <property file=&qu ...
- k8s 集群部署问题整理
1.hostname “master” could not be reached在host中没有加解析 2.curl -sSL http://localhost:10248/healthzcurl: ...
- composer包(发布到github上)同步到Packagist
在上一篇文章里面,探讨了如何一步步建立composer包–创建你的一个composer包 创建完成后,我们需要做的就是讲自建的包发布到Packagist上.至于说什么是Packagist,这个就不用我 ...
- linux下的别名机制
相当于用户自己创建一个属于自己的命令.在当前用户的家目录下有一个.bashrc文件,编辑该文件: eg:alias cls='clear' 如果命令要生效需要重新登录.用户输入cls就可以达到清屏的目 ...
- webapi返回json字符串
第一种 直接在方法中返回json. public class DefaultController : ApiController { [HttpGet] public IHttpActionResul ...
- remote: Permission to user_name/Code.git denied to other_user_name. fatal: unable to access 'https://github.com/user_name/Code.git/': The requested URL returned error: 403
Error msg: $ git push remote: Permission to xxx/Code.git denied to xxxxxx. fatal: unable to access ' ...