springboot异步调用demo
异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完成之后才能执行,而异步调用则无需等待上一步程序执行完成即可执行。最好往数据库里多添加数据,效果明显。
1、IDEA初始化springboot项目,勾选相关工具mybatis
2、创建user数据表,往里面添加数据,越多越好,我添加了10w条,数据可以从GroupLens网站中下载
3、创建实体类,该实体类需要实现Serializable
package net.maple.springboot.model;import java.io.Serializable;public class User implements Serializable {private int id;private String username;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
4、创建dao,注意加@Mapper
package net.maple.springboot.dao;
import net.maple.springboot.model.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
import java.util.concurrent.Future;
/*** dao* @author Administrator*/
@Mapper
public interface UserDao {/*** 普通查询* @return*/List<User> getAll();/*** 异步查询* @return*/Future<List<User>> asynGetAll();}
5、创建service
package net.maple.springboot.service;
import net.maple.springboot.model.User;
import java.util.List;
import java.util.concurrent.Future;
/*** service* @author Administrator*/
public interface UserService {/*** 普通查询* @return*/List<User> getAll();/*** 异步查询* @return*/Future<List<User>> asynGetAll();}
6、创建impl,注意加@Service,异步查询的方法上加@Async
package net.maple.springboot.service.impl;import net.maple.springboot.dao.UserDao;
import net.maple.springboot.model.User;
import net.maple.springboot.service.UserService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;@Service
public class UserServiceImpl implements UserService {@Resourceprivate UserDao userDao;@Overridepublic List<User> getAll() {try {System.out.println("开始做任务");long start=System.currentTimeMillis();List<User> userList=userDao.getAll();long end=System.currentTimeMillis();System.out.println("完成任务,耗时:"+(end-start)+"毫秒");return userList;}catch (Exception e){return Collections.emptyList();}}@Override@Asyncpublic Future<List<User>> asynGetAll() {try {System.out.println("开始做任务");long start=System.currentTimeMillis();List<User> userList= (List<User>) userDao.asynGetAll();long end=System.currentTimeMillis();System.out.println("完成任务,耗时:"+(end-start)+"毫秒");return new AsyncResult<>(null);}catch (Exception e){return new AsyncResult<>(null);}}}
application.yml
spring:#数据源datasource:name: testurl: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghaiusername: 你的数据库用户名password: 你的数据库密码#druidtype: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driver#mybatis
mybatis:mapper-locations: classpath:mapper/*.xml #对应mapper映射xml文件的所在路径type-aliases-package: net.maple.springboot.model #对应实体类的路径
Usermapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="net.maple.springboot.dao.UserDao"><select id="getAll" resultType="net.maple.springboot.model.User">select id,username,password from user</select></mapper>
开始测试,先测试普通查询
@SpringBootTest
class SpringbootAsynApplicationTests {@Resourceprivate UserService userService;@Testpublic void test(){long start=System.currentTimeMillis();System.out.println("第1次查询用户");List<User> userList1=userService.getAll();System.out.println("第2次查询用户");List<User> userList2=userService.getAll();System.out.println("第3次查询用户");List<User> userList3=userService.getAll();long end=System.currentTimeMillis();System.out.println("总共耗时:"+(end-start)+"毫秒");}}
普通查询大概7s,再测试异步查询,注意添加@EnableAsync
@EnableAsync
@SpringBootTest
class SpringbootAsynApplicationTests {@Resourceprivate UserService userService;@Testpublic void testAsync() throws InterruptedException {long start=System.currentTimeMillis();System.out.println("第1次查询用户");Future<List<User>> userList1=userService.asynGetAll();System.out.println("第2次查询用户");Future<List<User>> userList2=userService.asynGetAll();System.out.println("第3次查询用户");Future<List<User>> userList3=userService.asynGetAll();while(true){if(userList1.isDone()&&userList2.isDone()&&userList3.isDone()){break;}else{Thread.sleep(10);}}long end=System.currentTimeMillis();System.out.println("总共耗时:"+(end-start)+"毫秒");}}
结果真是很amazing啊,只用了51ms!
springboot异步调用demo相关推荐
- SpringBoot异步调用方法
SpringBoot异步调用方法 一.spring boot--使用异步请求,提高系统的吞吐量 https://blog.csdn.net/liuchuanhong1/article/details/ ...
- SpringBoot异步调用
2.1.无返回值的异步方法 2.1.有返回值的异步方法 3.1.方法级别重写Executor 3.2.应用级别重写Executor 3.3.自定义线程池配置 "异步调用"对应的是& ...
- Java 异步调用实践
本文要点: 为什么需要异步调用 CompletableFuture 基本使用 RPC 异步调用 HTTP 异步调用 编排 CompletableFuture 提高吞吐量 为什么异步 BIO 模型 首先 ...
- springboot 多线程_SpringBoot异步调用@Async
一. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行. 二. 如何实现异步调用 ...
- springboot之异步调用@Async
引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3. ...
- 面试官 | SpringBoot 中如何实现异步请求和异步调用?
作者 | 会炼钢的小白龙 来源 | cnblogs.com/baixianlong/p/10661591.html 一.SpringBoot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放 ...
- .NET2.0中,Winform程序如何异步调用Web Service呢?[Demo下载]——与.net1.1环境下比较...
最近在MSDN上看到一个在.NET1.1中Winform程序异步调用WebService的例子 我准备模仿着迁移到.NET2.0环境中,遗憾的是,一切不是那么简单. 首先,.net1.1中调用的Web ...
- SpringBoot利用@Async注解实现异步调用
前言:异步编程是让程序并发运行的一种手段,使用异步编程可以大大提高我们程序的吞吐量,减少用户的等待时间.在Java并发编程中实现异步功能,一般是需要使用线程或者线程池.而实现一个线程,要么继承Thre ...
- springboot定时发送短信_spring boot 1.5.4 定时任务和异步调用(十)
1Spring Boot定时任务和异步调用 我们在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. sp ...
- springboot 异步mysql_spring boot 使用@Async实现异步调用方法
使用@Async实现异步调用 什么是"异步调用"与"同步调用" "同步调用"就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码 ...
最新文章
- win7 64bit下最新Apahe2.4.18+php7.0.2+MySQL5.7.10配置
- 安装jenkins时出现 No such plugin: cloudbees-folder的解决办法
- 使用jdk压缩war包
- C#开发笔记之05-迭代器中的状态机(State Machine)到底是什么?
- 笔记本电脑桌面的计算机不见了,小编为你分析win7系统笔记本电脑桌面计算机图标不见了的设置方案....
- 如何让 AI 产生意识?
- *第十六周*数据结构实践项目二【大数据集上排序算法性能的体验】
- Android NDK开发如何解决logcat日志打印不全
- word字间距怎么调整成一样的【word教程】
- 2019第十四届中国竞争情报国际年会将于4月在上海召开
- 周集中团队Nature子刊中网络图布局的R语言可视化复现
- android系统支持4T硬盘吗,电脑是否有可能,支持3T硬盘,但不支持4T硬盘
- WebView获取当前网页的页面元素
- 如何将本地项目存入华为云
- Excel转成vCard(vcf格式)的5种方法 | 古意人
- 555定时器及其应用
- 3dsmax建筑/室内/家具/生长动画脚本插件AutokeyV1.0
- 股市中的内盘、外盘、跌幅、震幅、现手、总手、换手是什么意思?
- FaceNet--Google的人脸识别(转)
- Gradle 自定义Plugin插件之360加固