我是 webflux 的新手,无法找到合适的 Material 来继续实现。
我想发出请求并异步处理响应。在这种情况下,服务调用大约需要 8-10 毫秒才能响应,因此我们发出请求并继续做其他工作,并在需要进一步处理时查找响应。
Mono<Map<String,Price>> resp = webClient.post()
.uri("/{type}",isCustomerPricing ? "customer" : "profile")
.body(Mono.just(priceDetailsRequest),PriceDetailsRequest.class)
.retrieve().bodyToMono(customerPriceDetailsType);
我们如何使这个调用在不同的线程上异步执行。(我尝试使用 Schedulers.single/Scheuldes.parallel 来订阅者On),但在调用 Mono.block() 之前没有看到调用被执行。
我们如何实现?
- 我们希望此调用在单独的线程上并行执行,因此 当前线程可以继续其他工作
- 处理完成后,将响应设置为上下文
- 当当前线程寻找响应时,如果服务还没有 完成,阻塞直到调用完成
最佳答案
您不需要阻止消费响应。只需分配一个运算符来消耗同一链中的响应即可。下面给出一个例子。
Mono<Map<String,Price>> resp = webClient.post()
.uri("/{type}",isCustomerPricing ? "customer" : "profile")
.body(Mono.just(priceDetailsRequest),PriceDetailsRequest.class)
.retrieve()
.bodyToMono(CustomerPriceDetailsType.class)
.map(processor::responseToDatabaseEntity) // Create a persistable entity from the response
.map(priceRepository::save) // Save the entity to the database
.subscribe(); //This is to ensure that the flux is triggered.
或者,您可以提供消费者作为 subscribe()
方法的参数。
关于reactive-programming - Webflux WebClient异步请求和处理Mono,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54117103/