spring – 使用Reactive MongoDB和取消操作的流程正在取消

问题出在Project Reactor和Reactive MongoDB(
Spring Data)之间.

执行包含(按以下顺序)的流时:

>对Reactive MongoDB进行操作的方法非常快
>超过30秒的方法

流被取消(查看下面的代码和日志)

@GetMapping("/test/{msg}")
public Mono<SomeObject> test(@PathVariable String msg) {
    return repository.findByMessage(msg).log("1")
          .map(someObj -> delaySeconds(someObj, 35)).log("2");
}

正如您所看到的,30秒后流被取消,但是在另外5秒(超时为35秒)之后,会执行onNext事件.

12:59:18.556 [Thread-9] INFO  com.why.temp.TempController - Saved:SomeObject(id=5b604106ef301746a86665f3, message=WHY)
12:59:18.591 [http-nio-8080-exec-2] INFO  1 - | onSubscribe([Fuseable] MonoFlatMap.FlatMapMain)
12:59:18.592 [http-nio-8080-exec-2] INFO  2 - | onSubscribe([Fuseable] FluxMapFuseable.MapFuseableSubscriber)
12:59:18.593 [http-nio-8080-exec-2] INFO  2 - | request(unbounded)
12:59:18.593 [http-nio-8080-exec-2] INFO  1 - | request(unbounded)
12:59:18.612 [Thread-8] INFO  1 - | onNext(SomeObject(id=5b604106ef301746a86665f3, message=WHY))
12:59:49.116 [http-nio-8080-exec-3] INFO  2 - | cancel()
12:59:49.117 [http-nio-8080-exec-3] INFO  1 - | cancel()
12:59:53.612 [Thread-8] INFO  2 - | onNext(SomeObject(id=5b604106ef301746a86665f3, message=WHY))

你能解释一下为什么流被取消了,我该如何处理?

是否有任何超时应该增加,或者我是否以错误的方式使用Project Reactor Stream API和MongoDB?

这是我的MongoDB配置

@Bean
public ReactiveMongoTemplate reactiveMongoTemplate() {
    ConnectionString str = new ConnectionString(env.getMongoUri());
    return new ReactiveMongoTemplate(MongoClients.create(str), str.getDatabase());
}

任何的想法?如果您遇到类似问题,请提出此问题.

解决方法很简单,但不是那么优雅:

@GetMapping("/test/{msg}")
public Mono<SomeObject> test(@PathVariable String msg) {
    SomeObj someObj = repository.findByMessage(msg).block();
    return Mono.just(someObj).log("1")
        .map(someObj -> delaySeconds(someObj, 35)).log("2");
}

最佳答案 我有一个类似的问题,当反应性操作链超过魔法30秒.在我的情况下,它是Spring MVC请求超时,这是slution:

Recurring AsyncRequestTimeoutException in Spring Boot Admin log

spring.mvc.async.request-timeout的dafault值为30秒.

我相信它会有所帮助:).

干杯!

点赞