一般对于业务复杂的流程,会有一些处理逻辑不需要及时返回,甚至不需要返回值,但是如果充斥在主流程中,占用大量时间来处理,就可以通过异步的方式来优化。

实现异步的常用方法远不止两种,但是个人经验常用的,好用的,这里我就说两种,最好用的是第二种。


spring的注解方式@Async org.springframework.scheduling.annotation.Async

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包即可。

<ignore_js_op>springboot中使用异步的常用两种方式及其比较_spring

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.可以专门封装一个异步类。然后那个方法需要用到异步的操作。在这个类中封装,然后在类中引入异步类即可。

<ignore_js_op>springboot中使用异步的常用两种方式及其比较_ide_02

在使用异步的地方引入调用即可。

<ignore_js_op>springboot中使用异步的常用两种方式及其比较_异步方法_03

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);

        }

    }