SpringBoot异步调用方法实现场景代码实例
                                            一、背景

成都创新互联公司从2013年成立,先为江夏等服务建站,江夏等地企业,进行企业商务咨询服务。为江夏企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
项目中肯定会遇到异步调用其他方法的场景,比如有个计算过程,需要计算很多个指标的值,但是每个指标计算的效率快慢不同,如果采用同步执行的方式,运行这一个过程的时间是计算所有指标的时间之和。比如:
方法A:计算指标x,指标y,指标z的值,其中计算指标x需要1s,计算指标y需要2s,指标z需要3s。最终执行完方法A就是5s。
现在用异步的方式优化一下
方法A异步调用方法B,方法C,方法D,方法B,方法C,方法D分别计算指标x,指标y,指标z的值,那么最终执行完方法A的时间则是3s。
还有一种用途是当一个业务里面需要多个请求时,这时候异步并发请求所得到的回报远远是物有所值的。因为他是异步执行的,话不多说,一下是在springBoot里面使用并发请求;
二、spring boot中异步并发使用
2.1、appllication.yml
#****************集成Async线程池开始******************* async: # Async线程池 配置 executor: corepoolsize: 20 maxpoolsize: 25 queuecapacity: 40 keepaliveseconds: 200 threadnameprefix: appasync awaitterminationseconds: 60 #*****************集成Async线程池结束******************
2.2、配置线程池
@Configuration
@EnableAsync
public class ExecutorConfig {
  @Value("${async.executor.corepoolsize}")
  private Integer corePoolSize;
  @Value("${async.executor.maxpoolsize}")
  private Integer maxPoolSize;
  @Value("${async.executor.queuecapacity}")
  private Integer queueCapacity;
  @Value("${async.executor.keepaliveseconds}")
  private Integer keepAliveSeconds;
  @Value("${async.executor.threadnameprefix}")
  private String threadNamePrefix;
  @Value("${async.executor.awaitterminationseconds}")
  private Integer awaitTerminationSeconds;
  /**
   * 线程池
   *
   * @return
   */
  @Bean(name = "asyncExecutor")
  public Executor asyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 基础线程数 corePoolSize: 10
    executor.setCorePoolSize(corePoolSize);
    // 最大线程数 maxPoolSize: 15
    executor.setMaxPoolSize(maxPoolSize);
    // 队列长度 queueCapacity: 25
    executor.setQueueCapacity(queueCapacity);
    // 线程池维护线程所允许的空闲时间,单位为秒 keepAliveSeconds: 200
    executor.setKeepAliveSeconds(keepAliveSeconds);
    // 线程名字 threadNamePrefix: appasync
    executor.setThreadNamePrefix(threadNamePrefix);
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    // 等待所有任务都完成再继续销毁其他的Bean
    executor.setWaitForTasksToCompleteOnShutdown(true);
    // 线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住
    executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
    executor.initialize();
    return executor;
  }
}2.3、线程池监控(这个可有可无,主要是为了对线程池参数及时的调优)
@RestController
@Slf4j
@RequestMapping("/pubapi/asyncExecutor")
public class AsyncExecutorController extends BaseController {
  @Resource(name = "asyncExecutor")
  private Executor asyncExecutor;
  @PostMapping("/monitor")public ResultBean2.4、代码中使用
public void getMap(){
    /**
     * 先将耗时的、相互之间无依赖的操作先执行,由于其执行结果暂时不是特别关注,所以
     */
    Future futureA = functionA();
    Future futureB = functionB();
    /**
     * 执行其他的操作,其实functionA(),functionB()也在工作
     */
    aaa();
    /**
     * 获取异步的结果,然后计算
     */
    try {
      String resultA =futureA.get();
      String resuleB = futureB.get();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      e.printStackTrace();
    }
    
  }
  public Future functionA (){
    Future future = null;
    try {
      Thread.sleep(5000);
      future = new AsyncResult("functionA");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return future;
  }
  public Future functionB (){
    Future future = null;
    try {
      Thread.sleep(3000);
      future = new AsyncResult("functionB");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return future;
  }
  public void aaa(){
    System.out.println("我是");
  }        以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
当前题目:SpringBoot异步调用方法实现场景代码实例
本文链接:http://www.scyingshan.cn/article/pjooih.html


 咨询
咨询
 建站咨询
建站咨询 
 