好得很程序员自学网

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

在Spring Boot2中使用CompletableFuture的方法教程

前言

在spring boot中有一个注释@async,可以帮助开发人员开发并发应用程序。但使用此功能非常棘手。在本博客中,我们将了解如何将此功能与completablefuture一起使用。我认为你已经知道关于completablefuture的基础,所以我不会在这里重复这个概念。

首先,您需要使用@enableasync来注释您的应用程序类,这个注释告诉spring查找使用@async注释的方法并在单独的执行程序中运行它们。

?

1

2

3

4

5

6

7

8

@springbootapplication

@enableasync

public class app {

  resttemplate

  public static void main(string[] args) {

   springapplication.run(app. class , args);

  }

}

如果您查看有关使用completablefuture和@async的 spring boot示例 ,您会注意到他们使用此功能的方式基于rest请求,在我看来,我相信,它有点受限,它不会给你在其他情况下如何使用此功能的线索。例如,如果你有一个长期运行的任务,你会怎么做?

?

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

// source : https://spring.io/guides/gs/async-method/

package hello;

 

import org.slf4j.logger;

import org.slf4j.loggerfactory;

import org.springframework.boot.web.client.resttemplatebuilder;

import org.springframework.scheduling.annotation.async;

import org.springframework.stereotype.service;

import org.springframework.web.client.resttemplate;

 

import java.util.concurrent.completablefuture;

 

@service

public class githublookupservice {

 

   private static final logger logger = loggerfactory.getlogger(githublookupservice. class );

 

   private final resttemplate resttemplate;

 

   public githublookupservice(resttemplatebuilder resttemplatebuilder) {

     this .resttemplate = resttemplatebuilder.build();

   }

 

   @async

   public completablefuture<user> finduser(string user) throws interruptedexception {

     logger.info( "looking up " + user);

     string url = string.format( "https://api.github.com/users/%s" , user);

     user results = resttemplate.getforobject(url, user. class );

     // artificial delay of 1s for demonstration purposes

     thread.sleep(1000l);

     return completablefuture.completedfuture(results);

   }

 

}

在finduser(string user)中,它在主线程中使用completablefuture,此方法的主要任务是使用resttemplate从github获取数据,功能是[执行http请求的同步客户端]。如何使用长时间运行的任务,如调用网络功能,如从rest端点ping服务器?在这种情况下,您需要定制completablefuture。你不能简单地调用:

?

1

return completablefuture.completedfuture(results);

如何使用completablefuture

要在代码中使用@async,您的方法必须返回future或completablefuture,看一下下面的例子:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@async

   public completablefuture< boolean > isserveralive(string ip) {

     completablefuture< boolean > future = new completablefuture< boolean >(){

       @override

       public boolean get() throws interruptedexception, executionexception {

         inetaddress address = null ;

         try {

           address = inetaddress.getbyname(ip);

           return address.isreachable( 1000 );

         } catch (unknownhostexception e) {

           e.printstacktrace();

           return false ;

         } catch (ioexception e) {

           e.printstacktrace();

           return false ;

         }

       }

     };

     return future;

}

在这个例子中,我重写了get()方法并返回completablefuture而没有任何线程执行器,事实上我们要求spring在不同的线程中执行@async方法,但是我们不提供任何线程执行器,只有后台工作者中运行就足够了。

download source code from github

注意: 在这个例子中,我决定在spring boot中使用一个网络函数,仅仅是为了一个参数。但最好不要在rest端点中直接使用网络功能,特别是当您希望立即获得结果时。原因是:网络功能是阻塞的,这意味着,如果你调用这个rest端点,您必须在端点等待获取结果。强烈建议使用其他方法(如queue或push方法)(例如websocket)来调用阻塞函数。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://www.jdon.com/51288

查看更多关于在Spring Boot2中使用CompletableFuture的方法教程的详细内容...

  阅读:9次