【发布时间】:2020-01-30 11:34:00
【问题描述】:
我正在尝试并行使用 WebClient 发出请求,但我不知道该怎么做, 因为无论我做什么,代码都不会等待请求完成。如果我只执行一个请求(注释片段),一切正常。有人可以帮我吗?
@RequestMapping(method = [RequestMethod.POST], path = ["/upload/{batchId}"])
fun uploadFile(@RequestPart("file") file: Mono<FilePart>,
@PathVariable("batchId") batchId:String,
@RequestHeader("FILE-SIZE") fileSize:Int): Mono<ServiceResponse> {
val webClient = WebClient.create(commandEndpoint)
// return webClient.put().uri(seriesPath).retrieve().bodyToMono(String::class.java).map { ServiceResponse(it,0) }
return file.map{it.transferTo(Paths.get(storagePath,"excel"))}
.map{excelWorkbookToMetadata(WorkbookFactory.create(Paths.get(storagePath,"excel").toFile()))}
.flatMapMany{Flux.fromIterable(it)}
.flatMap {
it.transactionId = batchId
when (it) {
is SeriesMetadata -> webClient.put().uri(seriesPath,it.id)
.body(BodyInserters.fromObject(it))
.retrieve()
.onStatus({ it == HttpStatus.BAD_REQUEST },{
println("ERROR")
Mono.error(RuntimeException("blah")) }).toMono()
else -> Mono.error(NotImplementedError(""))
}
}
.collectList()
.map {ServiceResponse(batchId, it.size*2) }
}
【问题讨论】:
-
在最后尝试 .block() 让它等待,如果你愿意,你也可以在块中指定一个持续时间
-
不能在 netty 线程中使用块。也许我应该在单独的线程中执行该逻辑?
-
永远不要在响应式应用程序中使用阻塞,阻塞会阻碍执行并占用线程。非常糟糕@eduPeeth
-
@quirell,你检查过
Mono的zip&zipWhenprojectreactor.io/docs/core/release/api/reactor/core/publisher/… projectreactor.io/docs/core/release/api/reactor/core/publisher/…
标签: spring kotlin spring-webflux project-reactor reactor-netty