zoukankan      html  css  js  c++  java
  • springboot之异步调用@Async

    引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3.x之后,就已经内置了@Async来完美解决这个问题,本文将介绍在springboot中如何使用@Async。

    1、pom.xml中导入必要的依赖:

      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
        </parent>
    
        <dependencies>
            <!-- SpringBoot 核心组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </dependency>
        </dependencies>

    2、写一个springboot的启动类:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.scheduling.annotation.EnableAsync;
    
    @ComponentScan(basePackages = { "com.xwj.controller", "com.xwj.service" })
    @EnableAsync //开启异步调用
    @EnableAutoConfiguration
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
    }

    注意在这里一定要加上@EnableAsync注解开启异步调用

    3、建一个controller包,然后新建一个IndexController类,用来获取请求

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.xwj.service.UserService;
    
    @RestController
    public class IndexController {
        
        @Autowired
        private UserService userService;
        
        @RequestMapping("/async")
        public String async(){
            System.out.println("####IndexController####   1");
            userService.sendSms();
            System.out.println("####IndexController####   4");
            return "success";
        }
        
    }

    4、建一个service包,然后新建一个UserService类:

    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
    
        @Async
        public void sendSms(){
            System.out.println("####sendSms####   2");
            IntStream.range(0, 5).forEach(d -> {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            System.out.println("####sendSms####   3");
        }
    
    }

    先注掉@EnableAsync和@Async两个注解,看下同步调用执行的效果。执行结果如下:

    ####IndexController####   1
    ####sendSms####   2
    ####sendSms####   3
    ####IndexController####   4

    对于sendSms方法,我们并不关注它什么时候执行完,所以可以采用异步的方式去执行。放开@EnableAsync和@Async两个注解,执行结果如下:

    ####IndexController####   1
    ####IndexController####   4
    ####sendSms####   2
    ####sendSms####   3

    bingo!达到了我们预期的效果

    总结:

      使用了@Async的方法,会被当成是一个子线程,所有整个sendSms方法,会在主线程执行完了之后执行

           同一个类中,一个方法调用另外一个有@Async的方法,注解是不会生效的

  • 相关阅读:
    ABAP方法的exporting类型参数,需要在方法实现最开始显式初始化么
    Hybris里类似ABAP Netweaver的DDIC
    Hybris里类似ABAP Netweaver的DDIC
    SAP Cloud for Customer的CTI呼叫中心解决方案
    如何处理SAP HANA Web-Based Development Workbench的403 Forbidden错误
    Jerry's spark demo application
    SAP CRM WebClient UI和Hybris Commerce里的跨组件跳转
    SAP Cloud for Customer和Hybris Commerce的session保护机制
    SAP ABAP SQL的execution plan和cache
    SAP CRM呼叫中心里的事件注册机制
  • 原文地址:https://www.cnblogs.com/xuwenjin/p/8858050.html
Copyright © 2011-2022 走看看