我有Flux<URL>
。如何为每个URL创建多个并发void请求(例如myWebClient.refresh(URL)
),然后(在完成所有请求之后)从数据库中读取数据并返回Flux<MyAnyEntity>
(例如repo.findAll()
)?
答案 0 :(得分:3)
You can achieve that using Flux
/Mono
operators:
// get the URIs from somewhere
Flux<URI> uris = //...
Flux<MyAnyEntity> entities = uris
// map each URI to a HTTP client call and do nothing with the response
.flatMap(uri -> webClient.get().uri(uri).exchange().then())
// chain that call with a call on your repository
.thenMany(repo.findAll());
Update:
This code is naturally asynchronous, non-blocking so all operations in the flatMap
operator will be executed concurrently, according to the demand communicated by the consumer (this is the backpressure we're talking about).
If the Reactive Streams Subscriber
request(N)
elements, then N
requests might be executed concurrently. I don't think this is not something you want to deal with directly, although you can influence things using windowing operators for micro-bacthing operations.
Using .subscribeOn(Schedulers.parallel())
will not improve concurrency in this case - as stated in the reference documentation you should only use that for CPU-bound work.