Mar*_*vic 8 java reactive-programming project-reactor spring-webflux webtestclient
以下功能:
private Boolean canDoIt(Parameter param) {
return myService
.getMyObjectInReactiveWay(param)
.map(myObject -> myService.checkMyObjectInImperativeWay(myObject))
.block();
}
Run Code Online (Sandbox Code Playgroud)
在运行时工作正常,但是当使用它测试使用它的流程时,WebTestClient
出现以下错误:
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) ~[reactor-core-3.4.1.jar:3.4.1]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.MonoFlatMap] :
reactor.core.publisher.Mono.flatMap
Run Code Online (Sandbox Code Playgroud)
我知道我不应该使用block()
,但我别无选择:该函数必须返回 a Boolean
(而不是 a Mono<Boolean>
)。也许有另一种不使用block()
.
有没有办法让我WebTestClient
不抛出该错误?
使用Reactor Core版本3.4.6
。
我验证我的评论。block()
检查调用线程是否与阻塞代码兼容(反应器外部的线程,或特定反应器调度程序的线程,例如Schedulers.boundedElastic()
)。
有两种方法可以处理反应式堆栈中间的阻塞调用:
block()
在阻塞兼容调度程序上执行调用。请注意,不应在直接调用 的发布者上调用此运算符,而应在将“包装”块调用的发布者上调用此运算符(请参见下面的示例)。scheduleOn
publishOn
block()
一些参考:
一个最小的可重现示例(在 v3.4.6 上测试)给出了以下输出:
Ok context: not running from reactor Threads
value is true
Problematic stack: working with scheduler not compatible with blocking call
ERROR: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-2
Bad way to subscribe on a blocking compatible scheduler
ERROR: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-4
Bad way to publish on blocking compatible scheduler
ERROR: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-6
Possible workaround: share the reactive stream before blocking on it
It worked
Right way to subscribe on blocking compatible scheduler
It worked
Right way to publish on blocking compatible scheduler
It worked
Run Code Online (Sandbox Code Playgroud)
代码如下:
Ok context: not running from reactor Threads
value is true
Problematic stack: working with scheduler not compatible with blocking call
ERROR: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-2
Bad way to subscribe on a blocking compatible scheduler
ERROR: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-4
Bad way to publish on blocking compatible scheduler
ERROR: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-6
Possible workaround: share the reactive stream before blocking on it
It worked
Right way to subscribe on blocking compatible scheduler
It worked
Right way to publish on blocking compatible scheduler
It worked
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10963 次 |
最近记录: |