第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

彈簧通量和異步注釋

彈簧通量和異步注釋

茅侃侃 2022-08-17 16:49:35
我有一個(gè)Spring Flux應(yīng)用程序,在某些時(shí)候我需要在后臺(tái)執(zhí)行一些繁重的任務(wù),調(diào)用方(HTTP請求)不需要等到該任務(wù)完成。如果沒有反應(yīng)器,我可能會(huì)使用異步注釋,在不同的線程上執(zhí)行該方法。對(duì)于反應(yīng)堆,我不確定我是否應(yīng)該繼續(xù)這種方法,或者是否已經(jīng)有一個(gè)內(nèi)置的機(jī)制允許我實(shí)現(xiàn)這一目標(biāo)。例如,給定一個(gè)接受 Resource 對(duì)象的控制器:@PostMapping("/create")public Mono<Resource> create(@Valid @RequestBody Resource r) {    processor.run(r); // the caller should not wait for the resource to be processed    return repository.save(r);}和處理器類:@Asyncvoid run(Resource r) {     WebClient webClient = WebClient.create("http://localhost:8080");    Mono<String> result = webClient.get()                                   .retrieve()                                   .bodyToMono(String.class);    String response = result.block(); //block for now}的 HTTP 調(diào)用方不需要等到方法完成。/createrun
查看完整描述

2 回答

?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果你正在尋找“一勞永逸”模式的實(shí)現(xiàn),你可以訂閱你的發(fā)布者


@PostMapping("/create")

public Mono<Resource> create(@Valid @RequestBody Resource r) {

    run(r).subscribe();

    return repository.save(r);

}


Mono<Void> run(Resource r) {

    WebClient webClient = WebClient.create("http://localhost:8080");

    return webClient.get()

            .retrieve()

            .bodyToMono(String.class)

            .then();

}

如果發(fā)布者執(zhí)行阻塞操作,則應(yīng)在具有彈性或并行計(jì)劃程序的其他線程上訂閱該操作。


查看完整回答
反對(duì) 回復(fù) 2022-08-17
?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

我做了一些測試,我認(rèn)為即使使用fire and forget也會(huì)等待請求完成,然后再將答案返回給web瀏覽器或REST客戶端(至少在我的簡單測試中,它看起來像這樣)。因此,您必須執(zhí)行與@Async類似的操作,創(chuàng)建另一個(gè)線程:subscribe()


@PostMapping("/create")

public Mono<Resource> create(@Valid @RequestBody Resource r) {

    return processor.run(r)

    .subscribeOn(Schedulers.elastic()) // put eveything above this line on another thread

    .doOnNext(string -> repository.save(r)); // persist "r", not changing it, though


}

和處理器類:


Mono<String> run(Resource r) { 

    WebClient webClient = WebClient.create("http://localhost:8080");

    return webClient.get()

           .retrieve()

           .bodyToMono(String.class);

}


查看完整回答
反對(duì) 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 122 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)