spring boot实现异步调用
今天在这里学习下使用springboot的异步调用async
首先使用@EnableAsync开启异步功能
/**
* @author fengzp
* @date 17/5/8
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试类
/**
* @author fengzp
* @date 17/5/8
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@Component
public class AsyncTest {
public static Random random =new Random();
/**
* @Async所修饰的函数不要定义为static类型,否则异步调用不会生效
*
* 这里通过返回Future<T>来返回异步调用的结果,实现异步回调
*/
@Async
public Future<String> test1() throws InterruptedException {
System.out.println("test1 begin");
long begin = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
System.out.println("test1 end " + (System.currentTimeMillis() - begin));
return new AsyncResult<String>("test1 is done!");
}
@Async
public Future<String> test2() throws InterruptedException {
System.out.println("test2 begin");
long begin = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
System.out.println("test2 end " + (System.currentTimeMillis() - begin));
return new AsyncResult<String>("test2 is done!");
}
@Async
public Future<String> test3() throws InterruptedException {
System.out.println("test3 begin");
long begin = System.currentTimeMillis();
Thread.sleep(random.nextInt(10000));
System.out.println("test3 end " + (System.currentTimeMillis() - begin));
return new AsyncResult<String>("test3 is done!");
}
}
测试
/**
* @author fengzp
* @date 17/5/8
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class Test {
@Autowired
AsyncTest asyncTest;
@org.junit.Test
public void test() throws InterruptedException {
System.out.println("begin");
long begin = System.currentTimeMillis();
Future<String> test1 = asyncTest.test1();
Future<String> test2 = asyncTest.test2();
Future<String> test3 = asyncTest.test3();
while(true) {
if(test1.isDone() && test2.isDone() && test3.isDone())
break;
Thread.sleep(500);
}
System.out.println("end 耗时: " + (System.currentTimeMillis() - begin));
}
}
运行结果

spring boot实现异步调用的更多相关文章
- Spring Boot 异步方法的调用
Spring Boot 异步方法的调用 参考资料: 1.Spring Boot中使用@Async实现异步调用 使用方法 两个步骤: 1.开启配置 @EnableAsync,这一步特别容易忘记,导致测试 ...
- Spring boot 配置异步处理执行器
示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- Spring @Async实现异步调用示例
什么是“异步调用”? “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用的语句返回结果 ...
- Spring Boot Async异步执行
异步调用就是不用等待结果的返回就执行后面的逻辑,同步调用则需要等带结果再执行后面的逻辑. 通常我们使用异步操作都会去创建一个线程执行一段逻辑,然后把这个线程丢到线程池中去执行,代码如下: Execut ...
- 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...
- Spring Boot普通类调用bean
1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...
- Spring Boot @Async 异步任务执行
1.任务执行和调度 Spring用TaskExecutor和TaskScheduler接口提供了异步执行和调度任务的抽象. Spring的TaskExecutor和java.util.concurre ...
- Spring Boot发布和调用RESTful web service
Spring Boot可以非常简单的发布和调用RESTful web service,下面参考官方指导体验一下 1.首先访问 http://start.spring.io/ 生成Spring Boot ...
- (17)Spring Boot普通类调用bean【从零开始学Spring Boot】
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
随机推荐
- PHP字符串大小写转换函数
string strtolower(string $str) 将字符串转换为小写 string strtoupper(string $str) 将字符串转换为大写 $str1 = 'html'; $s ...
- AOP的异常通知
一.配置异常通知的步骤 (Aspectj方式) 1.只有当切点报异常才能触发异常通知 2.在spring中有Aspectj 方式提供了异常通知方法 2.1 如果希望通过 schema-base 实 ...
- Python学习杂记
Python中关键字yield有什么作用? 首先得理解generators,而理解generators前还要理解iterables: 你可以用在for...in...语句中的都是可迭代的:比如list ...
- c语言spline
#define NRANSI #include "nrutil.h" void spline(float x[], float y[], int n, float yp1, flo ...
- Oracle数据库常用的sql语句
1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...
- ZOJ 3156 Taxi (二分 + 二分匹配)
题意:给定 n 个人坐标, m 辆车的坐标,还有人的速度,要求每个人要进一辆不同的车,问你所有都进车的最短时间是多少. 析:首先二分时间 mid,很明显就是最后那个人进车的时间,然后如果把第 i 个人 ...
- oss上传文件夹
最近公司做工程项目,实现文件夹云存储上传. 网上找了很久,发现网上很多项目都存在相似问题,最后终于找到了一个符合我要求的项目. 工程如下: 这里对项目的文件夹云存储上传进行分析,实现文件夹上传,如何进 ...
- Linux后台开发工具箱
https://files-cdn.cnblogs.com/files/aquester/Linux后台开发工具箱.pdf 目录 目录 1 1. 前言 3 2. 脚本类工具 3 2.1. sed命令- ...
- NodeJs回调操作Promise化
mongoose是一个NodeJs下MongoDB的ORM库.使用这个库,您从DB到表(collection)都不用创建了.只需要在项目中定义好Model. 下面就是用上一篇的代码来演示如何把mong ...
- Android: Custom View和include标签的区别
Custom View, 使用的时候是这样的: <com.example.home.alltest.view.MyCustomView android:id="@+id/customV ...