一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 在Spring Boot2中使用CompletableFuture的方法教程

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

2021-06-26 14:07jdon Java教程

這篇文章主要給大家介紹了關于在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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青草国产青春综合久久 | 交换余生在线播放免费 | 日韩一二三| 我半夜摸妺妺的奶C了她 | 国产欧美日韩在线不卡第一页 | 婷婷色综合网 | 3p文两男一女办公室高h | 古装床戏做爰无遮挡三级 | av排名| 好大好硬好深好爽想要小雪 | 亚洲国产在线99视频 | 欧美精品一区二区在线观看 | 糖心在线观看网 | 色吧导航 | 苍井空色欲迷墙 | 欧美成人中文字幕在线看 | 性夜影院午夜看片 | 四虎b7s22c0m | 色婷婷激婷婷深爱五月老司机 | 欧美成人精品福利在线视频 | 天天做日日做 | 狠狠色96视频 | 久草在线草a免费线看 | 亚洲热图 | bt岛www| 国产成人综合一区人人 | 深夜国产在线 | 秋霞理论一级在线观看手机版 | www.四虎com| 国产乱人乱精一区二区视频密 | 精品国产成人 | 亚洲小视频在线 | 99免费在线视频 | 成人日批视频 | 肉文小说 | 半挠脚心半黄的网站 | 欧美vpswindows动物 | 天天色资料 | 国产精品日本一区二区不卡视频 | 91高清免费国产自产 | 51国产午夜精品免费视频 |