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类中,想直接使用 ...
随机推荐
- [Oracle]ORA-14400:插入的分区关键字未映射到任何分区
今天在使用测试库的时候发生ORA-14400:inserted partition key does not map to any partition 解决过程: 经过百度,发现出现ORA-14400 ...
- python ui学习过程,使用pyqt5实现
首先安装pyqt5的包,然后打开notebook就可以编写了.当然这样编写,也可以用designer进行. 它是pyqt5-tools的一个exe软件,\Anaconda3\Lib\site-pack ...
- mysql之零碎知识
一 视图 什么是视图:视图就是一张虚拟表.方便查看. 创建视图:create view 起名 as sql语句 #两张有关系的表 mysql> select * from course; +-- ...
- Python10/22--面向对象编程/类与对象/init函数
类: 语法: class关键字 类名# 类名规范 大写开头 驼峰命名法class SHOldboyStudent: # 描述该类对象的特征 school = "上海Oldboy" ...
- 通过http.client解析url返回的数据时为什么中文变成了unicode码
今天在解析json数据的时候得到了一堆这样的数据:{"errNum":0,"errMsg":"success","retData& ...
- 2018.12.31 NOIP训练 czy的后宫5(树形dp)
传送门 题意:给一棵有根树,树有点权,最多选出mmm个点,如果要选一个点必须先选其祖先,问选出来的点权和最大值是多少. 直接背包转移就行了. 代码
- 2018.11.01 NOIP训练 递增数列(迭代加深)
传送门 直接迭代加深搜索. 发现每次最多增加一倍,最少增加一,于是果断上下界剪枝. 代码
- PHP array
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- linux 下安装nginx
下载 下载版本 nginx-1.13.4.tar.gz 安装 1.解压 tar -zxvf nginx-1.13.4.tar.gz 2.配置安装目录 ./configure —prefix=/usr ...
- mysql 在linux下的完整安装过程
1.下载RPM包 https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar 2.先使用命令删 ...