好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解springboot使用异步注解@Async获取执行结果的坑

一、引言

在java后端开发中经常会碰到处理多个任务的情况,比如一个方法中要调用多个请求,然后把多个请求的结果合并后统一返回,一般情况下调用其他的请求一般都是同步的,也就是每个请求都是阻塞的,那么这个处理时间必定是很长的,有没有一种方法可以让多个请求异步处理那,答案是有的。

springboot中提供了很便利的方式可以解决上面的问题,那就是异步注解@Async。正确的使用该注解可以使你的程序飞起,相反如果使用不当那么并不会取到理想的效果。

二、获取异步执行结果

1、环境介绍

下面是我的controller,SyncController.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

package com.atssg.controller;

 

import com.atssg.service.MySyncService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

 

 

@Slf4j

@RestController

@RequestMapping ( "/sync" )

public class SyncController {

     @Autowired

     private MySyncService syncService;

 

     @GetMapping (value = "/test" )

 

     public String test() {

         String str= null ;

         try {

 

             log.info( "start" );

             str = syncService.asyncMethod();

             log.info( "str:{}" , str);

             return str;

         } catch (Exception e) {

             e.printStackTrace();

         }

 

         return str;

     }

}

在controller中就是调用下层的方法并返回,再看service层的类MySyncService.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

package com.atssg.service;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Future;

 

@Service

public class MySyncService {

     @Autowired

     private SyncService syncService;

 

     /**

      * 异步方法

      *

      * @return

      * @throws InterruptedException

      * @throws ExecutionException

      */

     public String asyncMethod() throws InterruptedException, ExecutionException {

 

         Future<String> result1 = syncService.method1( "I" );

         Future<String> result2 = syncService.method2( "love" );

         Future<String> result3 = syncService.method3( "async" );

 

         String str = result1.get();

         String str2 = result2.get();

         String str3 = result3.get();

 

         String result = str + str2 + str3;

 

         return result;

     }

 

     /**

      * 同步方法

      *

      * @return

      * @throws InterruptedException

      * @throws ExecutionException

      */

     public String syncMethod() throws InterruptedException, ExecutionException {

         /*同步写法*/

         String str = syncService.method1( "I" ).get();

         String str2 = syncService.method2( "love" ).get();

         String str3 = syncService.method3( "async" ).get();

         return str + str2 + str3;

     }

}

上面便是service类,仅仅是调用下次异步层的方法,并取得返回值。上面类中有两个方法,其写法也类似但结果却大不相同,后面详说。

下面是异步层的方法,SyncService.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

package com.atssg.service;

 

import org.springframework.scheduling.annotation.Async;

import org.springframework.scheduling.annotation.AsyncResult;

import org.springframework.stereotype.Service;

 

import java.util.concurrent.Future;

 

@Service

@Async

public class SyncService {

 

     //@Async

     public Future<String> method1(String str) throws InterruptedException {

        Thread.sleep( 1000 * 10 );

         return new AsyncResult<>( str);

     }

     //@Async

     public Future<String> method2(String str) throws InterruptedException {

         Thread.sleep( 1000 * 5 );

         return new AsyncResult<>(str);

     }

    // @Async

     public Future<String> method3(String str) throws InterruptedException {

         Thread.sleep( 1000 * 15 );

         return new AsyncResult<>(str);

     }

}

该类使用@Async注解,表明该类中所有的方法都是异步执行的,其中@Async可修饰类也可以修饰方法。

这便是所有的环境。

2、错误的方式

在MySyncService中有两个方法,先看其中一个方法

?

1

2

3

4

5

6

7

public String syncMethod() throws InterruptedException, ExecutionException {

         /*同步写法*/

         String str = syncService.method1( "I" ).get();

         String str2 = syncService.method2( "love" ).get();

         String str3 = syncService.method3( "async" ).get();

         return str + str2 + str3;

     }

这种写法是调用异步方法后立即调用get()方法,即获取结果,下面看测试结果,在controllor中调用该方法,下面看执行结果

2021-08-21 11:06:28.612  INFO 3584 --- [nio-8080-exec-1] com.atssg.controller.SyncController      : start
2021-08-21 11:06:58.651  INFO 3584 --- [nio-8080-exec-1] com.atssg.controller.SyncController      : str:Iloveasync

可以看到共执行了30s,在异步层的方法中的三个方法如下,

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

//@Async

     public Future<String> method1(String str) throws InterruptedException {

        Thread.sleep( 1000 * 10 );

         return new AsyncResult<>( str);

     }

     //@Async

     public Future<String> method2(String str) throws InterruptedException {

         Thread.sleep( 1000 * 5 );

         return new AsyncResult<>(str);

     }

    // @Async

     public Future<String> method3(String str) throws InterruptedException {

         Thread.sleep( 1000 * 15 );

         return new AsyncResult<>(str);

     }

可以看到这三个方法分别是睡眠10s、5s、15s,这就很好理解了syncMethod()方法中的写法是同步的,未达到异步的目的,切记调用完异步方法进接着调用get()方法不是异步的方式,而是同步的。

3、正确方式

上面看了错误的用法,下面看正确的方式,

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public String asyncMethod() throws InterruptedException, ExecutionException {

 

        Future<String> result1 = syncService.method1( "I" );

        Future<String> result2 = syncService.method2( "love" );

        Future<String> result3 = syncService.method3( "async" );

 

        String str = result1.get();

        String str2 = result2.get();

        String str3 = result3.get();

 

        String result = str + str2 + str3;

 

        return result;

    }

这种方式是首先调用异步方法,然后分别调用get()方法,取得执行结果。下面看测试结果

2021-08-21 11:17:23.516  INFO 3248 --- [nio-8080-exec-1] com.atssg.controller.SyncController      : start
2021-08-21 11:17:38.535  INFO 3248 --- [nio-8080-exec-1] com.atssg.controller.SyncController      : str:Iloveasync

执行时间未15s,这就很好解释了,异步层的三个方法,分别睡眠的时间是10s、5s、15s,既然是异步执行的,那么总的执行时间肯定是三个方法中最长的那个,符合测试结果。这才@Async正确的打开姿势。

三、异步执行@Async注解

@Async注解的定义如下,

?

1

2

3

4

5

6

@Target ({ElementType.TYPE, ElementType.METHOD})

@Retention (RetentionPolicy.RUNTIME)

@Documented

public @interface Async {

     String value() default "" ;

}

可以看到该注解可以用在类及方法上,用在类上表示类中的所有方法都是异步的,用在方法上表示该方法是异步的。

四、总结

今天的文章分享到这里,主要分享了关于@Async注解在获取执行结果的时候的坑,一定要先调用异步方法,然后再调用get()方法,获取结果,其中get方法还有一个重载的,可以设置超时时间,即超过设置的超时时间便返回,不再等待,各位小伙伴可以自己试验。

?

1

2

3

V get( long timeout, TimeUnit unit)

         throws InterruptedException, ExecutionException, TimeoutException;

}

下次继续分享有关@Async注解使用的一些小细节,欢迎持续关注。

到此这篇关于详解springboot使用异步注解@Async获取执行结果的坑的文章就介绍到这了,更多相关springboot使用异步注解@Async 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/teach/p/15169153.html

查看更多关于详解springboot使用异步注解@Async获取执行结果的坑的详细内容...

  阅读:56次