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ì)劃程序的其他線程上訂閱該操作。
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);
}
添加回答
舉報(bào)
