1. 程式人生 > 程式設計 >詳解springboot中使用非同步的常用兩種方式及其比較

詳解springboot中使用非同步的常用兩種方式及其比較

一般對於業務複雜的流程,會有一些處理邏輯不需要及時返回,甚至不需要返回值,但是如果充斥在主流程中,佔用大量時間來處理,就可以通過非同步的方式來優化。

實現非同步的常用方法遠不止兩種,但是個人經驗常用的,好用的,這裡我就說兩種,最好用的是第二種。

  1. spring的註解方式@Async org.springframework.scheduling.annotation.Async
  2. jdk1.8後的CompletableFuture java.util.concurrent.CompletableFuture

其中第一種的使用注意事項比較多

1.不要在本類中非同步呼叫。即一個方法是非同步方法,然後用另一個方法呼叫這個非同步方法。

2.不要有返回值,使用void

3.不能使用本類的私有方法或者非介面化加註@Async,因為代理不到失效

4.非同步方法不能使用static修飾

5.非同步類需使用@Component註解,不然將導致spring無法掃描到非同步類

6.SpringBoot框架必須在啟動類中增加@EnableAsync註解

7.非同步方法不要和事物註解同時存在。可以在事物的方法中呼叫另外一個類中的非同步方法。在呼叫Async方法的方法上標註@Transactional是管理呼叫方法的事務的,在Async方法上標註@Transactional是管理非同步方法的事務,事務因執行緒隔離

@Async的使用方式

1.pom引入依賴。只要依賴中存在spring-context包即可。


在這裡插入圖片描述

2.需要在springboot啟動類上加上開啟非同步的註解@EnableAsync

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@EnableHystrix
@EnableFeignClients
@SpringBootApplication
public class OrderServerApplication extends SpringBootServletInitializer {

 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  return application.sources(OrderServerApplication.class);
 }

 public static void main(String[] args) {
  SpringApplication.run(OrderServerApplication.class,args);
 }
}

3.可以專門封裝一個非同步類。然後那個方法需要用到非同步的操作。在這個類中封裝,然後在類中引入非同步類即可

在這裡插入圖片描述

在使用非同步的地方引入呼叫即可。

在這裡插入圖片描述

CompletableFuture中的非同步方法是在一個正常的方法體內使用即可。

 private ExecutorService pool = Executors.newFixedThreadPool(2);

 @Override
 public void synOrder(String orderId,OrderSourceEnum type,Integer status) {
  try {
   CompletableFuture.runAsync(() -> {
   		 //下面是一些業務操作。
    PageVO pageVO = new PageVO();
    Set<String> orderIds = new HashSet<>();
    orderIds.add(orderId);
    pageVO.setIds(orderIds);
    pageVO.setOrderType(type);
    List<MaisOrderDTO> maisOrderDTOS = orderSourceService.batchGetDetails(pageVO);
    if (CollectionUtils.isEmpty(maisOrderDTOS)) {
     throw new RuntimeException("未找到id=" + orderId + "型別為:" + type.getDesc() + "的訂單");
    }
   },pool);
  } catch (Exception e) {
   log.info("同步單個訂單給群脈出現異常:{}",e);
  }
 }

參考部落格:[Java併發-15] CompletableFuture: 非同步程式設計

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。