SpringBoot的异步调用介绍
参考博客: https://www.cnblogs.com/jebysun/p/9675345.html
https://blog.csdn.net/weixin_38399962/article/details/82146480
何为异步调用
说异步调用前,我们说说它对应的同步调用。通常开发过程中,一般上我们都是同步调用,即:程序按定义的顺序依次执行的过程,每一行代码执行过程必须等待上一行代码执行完毕后才执行。而异步调用指:程序在执行时,无需等待执行的返回值可继续执行后面的代码。显而易见,同步有依赖相关性,而异步没有,所以异步可并发执行,可提高执行效率,在相同的时间做更多的事情。
题外话:处理异步、同步外,还有一个叫回调。其主要是解决异步方法执行结果的处理方法,比如在希望异步调用结束时返回执行结果,这个时候就可以考虑使用回调机制。
先自定义一个线程池, 这里采用java的代码来配置, 如下面的ThreadPoolConfig.java内容:
import java.util.concurrent.ThreadPoolExecutor; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
public class ThreadPoolConfig {
@Bean(name = "asyncPoolTaskExecutor") // 这里bean的name值在下面要用到
public ThreadPoolTaskExecutor getAsyncThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(20);
taskExecutor.setMaxPoolSize(200);
taskExecutor.setQueueCapacity(25);
taskExecutor.setKeepAliveSeconds(200);
taskExecutor.setThreadNamePrefix("cmnd-");
//线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy/CallerRunsPolicy;默认为后者
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//调度器shutdown被调用时等待当前被调度的任务完成
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
//等待时长
taskExecutor.setAwaitTerminationSeconds(60);
taskExecutor.initialize();
return taskExecutor;
} }
这里简单说明下,关于ThreadPoolTaskExecutor参数说明:
corePoolSize:线程池维护线程的最少数量
keepAliveSeconds:允许的空闲时间,当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
maxPoolSize:线程池维护线程的最大数量,只有在缓冲队列满了之后才会申请超过核心线程数的线程
queueCapacity:缓存队列
rejectedExecutionHandler:线程池对拒绝任务(无线程可用)的处理策略。这里采用了CallerRunsPolicy策略,当线程池没有处理能力的时候,该策略会直接在 execute 方法的调用线程中运行被拒绝的任务;如果执行程序已关闭,则会丢弃该任务。还有一个是AbortPolicy策略:处理程序遭到拒绝将抛出运行时RejectedExecutionException。
而在一些场景下,若需要在关闭线程池时等待当前调度任务完成后才开始关闭,可以通过简单的配置,进行优雅的停机策略配置。关键就是通过setWaitForTasksToCompleteOnShutdown(true)和setAwaitTerminationSeconds方法。
setWaitForTasksToCompleteOnShutdown:表明等待所有线程执行完,默认为false。
setAwaitTerminationSeconds:等待的时间,因为不能无限的等待下去
再给出AsyncService.java内容
import java.util.concurrent.Future; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service; @Service(value = "asyncService")
public class AsyncService { private static final Logger LOG = LogManager.getLogger(AsyncService.class);
// 我这里写的异步任务是必须有返回值的, 使用AsyncResult来返回
@Async(value= "asyncPoolTaskExecutor") // 这里的value必须写成上面自定义的线程池name, 要不然会用默认的线程池来执行下面的代码
public Future<String> testAsync(int i) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOG.error(e.getMessage(), e);
Thread.currentThread().interrupt();
}
return new AsyncResult<>(String.format("testAsyncTask->{%s}", i));
} }
最后给出AsyncTaskController.java内容:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import com.xum.cmnd.service.AsyncService; @EnableAsync
@Controller(value = "asyncTaskController")
@RequestMapping(value = "/async")
public class AsyncTaskController { private static final Logger LOG = LogManager.getLogger(AsyncTaskController.class); @Autowired
private AsyncService asyncService; @PostMapping(value = "/test")
public void testAsync() throws InterruptedException, ExecutionException {
List<Future<String>> futures = new ArrayList<>();
for(int i = 1; i <= 10; i++) {
Future<String> future = asyncService.testAsync(i);
futures.add(future);
}
for (Future<String> future : futures) {
String result = future.get();
LOG.info("result:" + result);
}
} }
启动项目, 然后用postman或则在浏览器中输入http://localhost:8080/async/test这条api, 看eclipse的console中打印的log, 如下
...controller.AsyncTaskController][37]:result:testAsyncTask->{1}
...controller.AsyncTaskController][37]:result:testAsyncTask->{2}
...controller.AsyncTaskController][37]:result:testAsyncTask->{3}
...controller.AsyncTaskController][37]:result:testAsyncTask->{4}
...controller.AsyncTaskController][37]:result:testAsyncTask->{5}
...controller.AsyncTaskController][37]:result:testAsyncTask->{6}
...controller.AsyncTaskController][37]:result:testAsyncTask->{7}
...controller.AsyncTaskController][37]:result:testAsyncTask->{8}
...controller.AsyncTaskController][37]:result:testAsyncTask->{9}
...controller.AsyncTaskController][37]:result:testAsyncTask->{10}
注意:
1. 一定要批量读取结果, 否则不能达到异步的效果!!!
2、异步方法和调用类不要在同一个类中!!!
3、注解扫描时,要注意过滤,避免重复实例化,因为存在覆盖问题,@Async就失效了!!!
SpringBoot的异步调用介绍的更多相关文章
- springboot之异步调用@Async
原文:http://www.cnblogs.com/xuwenjin/p/8858050.html 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交 ...
- springboot实现异步调用
介绍 所谓的异步执行其实就是使用多线程的方式实现异步调用. 异步有什么好处呢? 如果一个业务逻辑执行完成需要多个步骤,也就是调用多个方法去执行, 这个时候异步执行比同步执行相应更快.不过要注意异步请求 ...
- springboot:异步调用@Async
在后端开发中经常遇到一些耗时或者第三方系统调用的情况,我们知道Java程序一般的执行流程是顺序执行(不考虑多线程并发的情况),但是顺序执行的效率肯定是无法达到我们的预期的,这时就期望可以并行执行,常规 ...
- springboot的异步调用
package com.handsight.platform.fras.aapp; import java.util.Locale; import org.slf4j.Logger; import o ...
- SpringBoot中异步请求和异步调用(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...
- spring boot实现异步调用
今天在这里学习下使用springboot的异步调用async 首先使用@EnableAsync开启异步功能 /** * @author fengzp * @date 17/5/8 * @email f ...
- springboot 异步调用Async使用方法
引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3. ...
- SpringBoot | 第二十一章:异步开发之异步调用
前言 上一章节,我们知道了如何进行异步请求的处理.除了异步请求,一般上我们用的比较多的应该是异步调用.通常在开发过程中,会遇到一个方法是和实际业务无关的,没有紧密性的.比如记录日志信息等业务.这个时候 ...
- springBoot中实现自定义属性配置、实现异步调用、多环境配置
springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...
随机推荐
- p4171&bzoj1823 满汉全席
传送门(洛谷) 传送门(bzoj) 题目 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能 ...
- Git 客户端在 WebIDE 中的实现
Coding WebIDE 是 Coding.net 自主研发的在线集成开发环境 (IDE).你可以通过 WebIDE 创建项目的工作空间, 进行在线开发, 调试等操作,有功能健全的 Terminal ...
- 9、scala函数式编程-集合操作
一.集合操作1 1.Scala的集合体系结构 // Scala中的集合体系主要包括:Iterable.Seq.Set.Map.其中Iterable是所有集合trait的根trai.这个结构与Java的 ...
- Python短小精悍的Orator查询构造器
查询构造器 介绍 这个数据库查询构造器,提供便利的接口可以创建和执行查询操作,可以在大多数数据库中使用. 查询select操作 查询表中所有的数据. users = db.table('users') ...
- 理解setTimeout和setInterval
setTimeout和setInterval,这两个js函数都是用来定时执行.setTimeout执行一次,setInterval执行多次. 问题出现在今天,使用setInterval是,设置执行速度 ...
- vue 报错 Uncaught (in promise) error
可尝试在then()后加上catch() ps:该图来自网络
- 【C#】清除webBrowser 缓存和Cookie的解决方案
试了很多方法,最后发现万剑大哥的方法管用,转载一下 转自:https://www.cnblogs.com/midcn/p/3527123.html 通过测试webBrowser与IE缓存和Cookie ...
- const define区别
可以使用defined()----检测常量是否设置 [问]在php中定义常量时,const与define的区别? [答]使用const使得代码简单易读,const本身就是一个语言结构,而define是 ...
- 洛谷P4332 [SHOI2014]三叉神经树(LCT)
传送门 FlashHu大佬太强啦%%% 首先,我们可以根据每一个点的权值为$1$的儿子的个数把每个点记为$0~3$,表示这一个点的点权 先考虑一下暴力的过程,假设从$0$变为$1$,先更改一个叶子结点 ...
- VUE中嵌套路由
官网地址:https://router.vuejs.org/zh-cn/essentials/nested-routes.html 路由嵌套一般使用在后台管理系统中 给一个比较简单的小案例 <! ...