170719、springboot编程之异步调用@Async
1、在pom.xml中增加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、在主类上开启注解
package com.rick; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication
@EnableAsync
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3、新建任务测试类
package com.rick.task; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component; import java.util.Random;
import java.util.concurrent.Future; /**
* Desc : 任务类
* User : RICK
* Time : 2017/8/29 9:56
*/ @Component
public class Task1 { public Random random = new Random(); /**
* Desc : @Async所修饰的函数不要定义为static类型,否则异步调用不会生效
* 这里通过返回Future<T>来返回异步调用的结果,实现异步回调
* User : RICK
* Time : 2017/8/29 10:30
*/ //任务一
@Async
public Future<String> doTaskOne() throws Exception{
System.out.println("开始做任务一");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("test1 is done!");
} //任务二;
@Async
public Future<String> doTaskTwo() throws Exception {
System.out.println("开始做任务二");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("test2 is done!");
} //任务3;
@Async
public Future<String> doTaskThree() throws Exception {
System.out.println("开始做任务三");
long start = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
long end = System.currentTimeMillis();
System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");
return new AsyncResult<>("test3 is done!");
} }
4、创建测试控制器
package com.rick.controller; import com.rick.task.Task1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.Future; @RestController
public class TaskController { @Autowired
private Task1 task1; @RequestMapping("/task1")
public String task1() throws Exception{
Future<String> test1 = task1.doTaskOne();
Future<String> test2 = task1.doTaskTwo();
Future<String> test3 = task1.doTaskThree(); while (true){
if(test1.isDone()){
System.out.println("====================test1 is done=========================");
} if(test2.isDone()){
System.out.println("====================test2 is done=========================");
} if(test3.isDone()){
System.out.println("====================test3 is done=========================");
} if(test1.isDone() && test2.isDone() && test3.isDone()){
break;
}
Thread.sleep(1000);
} return "task1";
} }
5、启动项目测试http://localhost:8080/task1
项目代码:https://github.com/zrbfree/spring-boot-async.git
170719、springboot编程之异步调用@Async的更多相关文章
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- springboot:异步调用@Async
在后端开发中经常遇到一些耗时或者第三方系统调用的情况,我们知道Java程序一般的执行流程是顺序执行(不考虑多线程并发的情况),但是顺序执行的效率肯定是无法达到我们的预期的,这时就期望可以并行执行,常规 ...
- springboot:使用异步注解@Async的那些坑
springboot:使用异步注解@Async的那些坑 一.引言 在java后端开发中经常会碰到处理多个任务的情况,比如一个方法中要调用多个请求,然后把多个请求的结果合并后统一返回,一般情况下调用其他 ...
- springboot:使用异步注解@Async的前世今生
在前边的文章中,和小伙伴一起认识了异步执行的好处,以及如何进行异步开发,对,就是使用@Async注解,在使用异步注解@Async的过程中也存在一些坑,不过通过正确的打开方式也可以很好的避免,今天想和大 ...
- springboot 异步调用Async使用方法
引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3. ...
- springboot之异步调用@Async
原文:http://www.cnblogs.com/xuwenjin/p/8858050.html 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交 ...
- SpringBoot系列:Spring Boot异步调用@Async
在实际开发中,有时候为了及时处理请求和进行响应,我们可能会多任务同时执行,或者先处理主任务,也就是异步调用,异步调用的实现有很多,例如多线程.定时任务.消息队列等, 这一章节,我们就来讲讲@Async ...
- SpringBoot集成篇(二) 异步调用Async
什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行. 如何实现异步调用? 多线程, ...
- SpringBoot异步调用--@Async详解
1. 概述 在日常开发中,为了提高主线程的效率,往往需要采用异步调用处理,例如系统日志等.在实际业务场景中,可以使用消息中间件如RabbitMQ.RocketMQ.Kafka等来解决.假如对高可用 ...
随机推荐
- ajax传值 乱码问题
ajax.overrideMimeType("text/xml;charset=GBK");
- css的id选择器与thinkphp结合
<head> <style type="text/css"> #a2{ border:1px solid blue; width:140px; height ...
- selenium测试(Java)--执行JS(十八)
1. 操作滚动条 package com.test.js; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; ...
- Struts tags--Data tags
struts tags详解之<s:bean> Description Bean标签,当然需要一个JavaBean.它的属性值的操作是经由Bean标签中的参数属性来进行赋值.当然 ...
- imx6 fec分析
/***************************************************************************** * imx6 fec分析 * 本文主要分析 ...
- Loadrunner进行md5加密方法
本文主要介绍使用Loadrunner进行字符串md5加密的方法. 使用Loadrunner进行md5比较简单,首先是加载md5.h头文件,后使用头文件中的加密函数即可. 1. md5.h头文件内容如下 ...
- 【ML】概率图模型
http://wenku.baidu.com/link?url=-Fa32JAnvwS8fyWgdPjYLNGvmor42lWCT6N7TehNQAnx4ZVmJtC0L0SgnaLtEFMB9Gzw ...
- mybatis由浅入深day02_8spring和mybatis整合
8 spring和mybatis整合 8.1 整合思路 需要spring通过单例方式管理SqlSessionFactory.mapper接口. spring和mybatis整合生成代理对象,使用Sql ...
- HTML&CSS精选笔记_浮动与定位
浮动与定位 元素的浮动 元素的浮动属性float 什么是浮动? 元素的浮动是指设置了浮动属性的元素会脱离标准文档流的控制,移动到其父元素中指定位置的过程. 如何定义浮动? 在CSS中,通过float属 ...
- UITextField 全属性
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...